使用python

时间:2019-01-22 02:06:38

标签: python-3.x postgresql amazon-web-services amazon-rds

我在RDS中有一个现有的postgres表,其数据库名称为my-rds-table-name

我已使用pgAdmin4和只读用户的以下配置进行连接:

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

我已确认可以查询该表。

但是,我无法使用python连接到它:

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

使用:

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'
)

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

类似地,如果我尝试使用sqlalchemy连接到它:

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

我想念什么?

2 个答案:

答案 0 :(得分:1)

感谢John Rotenstein的评论。

他指出,my-rds-table-name是数据库实例名称,而不是数据库名称,默认数据库名称是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'
)

答案 1 :(得分:0)

使用 sqlalchemy,您可以执行以下操作:

engine = create_engine('postgresql://postgres:postgres@<AWS_RDS_end-point>:5432/postgres')

然后您可以更新您的数据库。 例如:

df = pd.DataFrame({'A': [1,2], 'B':[3,4]})
df.to_sql('tablename', engine, schema='public', if_exists='append', index=False)