Amazon RDS 上的 PostgresQL

PostgresSQL on Amazon RDS

我已经在 Amazon RDS 上配置了一个 PostgresSQL 数据库实例,并想从 python 脚本中使用它。我正在为此使用 awswrangler。

下面是 python 脚本。

import awswrangler as wr

df1 = pd.DataFrame({
    "id": [1, 2],
    "name": ["foo", "boo"]
})

eng_postgresql = wr.db.get_engine(db_type="postgresql", host=ENDPOINT, port=5432, database=DBNAME, user=USR, password=pass)

wr.db.to_sql(df1, eng_postgresql, schema="public", name="tutorial", if_exists="replace", index=False)  # PostgreSQL

我收到以下错误:

sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) could not connect to server: Operation timed out
    Is the server running on host "*************.rds.amazonaws.com" (172.31.23.226) and accepting
    TCP/IP connections on port 5432?

根据评论。

由于 RDS 不是 public,连接超时。随后,通过 Internet 的连接将失败。

这个问题一般有三种解决方法;

  1. Modify the RDS instance 拥有 public ip。为此,RDS 必须位于 public 子网中,其安全组允许来自 AWS 外部的连接。
  2. 设置堡垒主机并使用 ssh 隧道,通过 Internet 隧道连接到私有数据库实例。
  3. 在 home/work 和 RDS 实例所在的 VPC 之间设置 VPN 连接