Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connect to AWS RDS Postgres database with python

I have an existing postgres table in RDS with a database name my-rds-table-name

I've connected to it using pgAdmin4 with the following configs of a read-only user:

host_name = "my-rds-table-name.123456.us-east-1.rds.amazonaws.com"
user_name = "my_user_name"
password = "abc123def345"

I have verified that I can query against the table.

However, I cannot connect to it using python:

SQLAlchemy==1.2.16
psycopg2-binary==2.7.6.1
mysqlclient==1.4.1

With:

import psycopg2
engine = psycopg2.connect(
    database="my-rds-table-name",
    user="my_user_name",
    password="abc123def345",
    host="my-rds-table-name.123456.us-east-1.rds.amazonaws.com",
    port='5432'
)

It fails with

psycopg2.OperationalError: FATAL:  database "my-rds-table-name" does not exist

Similarly, if I try to connect to it with sqlalchemy:

sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) FATAL:  database "my-rds-table-name" does not exist

What am I missing?

like image 785
Roman Avatar asked Jan 22 '19 02:01

Roman


People also ask

How do I connect to AWS RDS postgres?

Sign in to the AWS Management Console and open the Amazon RDS console at https://console.aws.amazon.com/rds/ . Open the RDS console and then choose Databases to display a list of your DB instances. Choose the PostgreSQL DB instance name to display its details. On the Connectivity & security tab, copy the endpoint.

How does Python connect to local postgres database?

To establish connection with the PostgreSQL database, make sure that you have installed it properly in your system. Open the PostgreSQL shell prompt and pass details like Server, Database, username, and password. If all the details you have given are appropriate, a connection is established with PostgreSQL database.


Video Answer


1 Answers

Thank's John Rotenstein for your comment.

As he pointed out, my-rds-table-name is the database instance name, not the database name, the default database name is postgres.

import psycopg2
engine = psycopg2.connect(
    database="postgres",
    user="my_user_name",
    password="abc123def345",
    host="my-rds-table-name.123456.us-east-1.rds.amazonaws.com",
    port='5432'
)
like image 83
Roman Avatar answered Oct 12 '22 12:10

Roman