【发布时间】:2021-09-25 21:53:10
【问题描述】:
我创建了与 PostgreSQL 兼容的 Amazon Aurora,并且知道我想连接该数据库并插入和获取数据,但我无法使用 python 连接我的 AWS Aurora PostgreSQL...
我的代码:
def connection_maker(self):
try:
ENDPOINT = "assetsintel.cluster-cjeiapuahe69.us-east-1.rds.amazonaws.com"
PORT = 5432
USR = "postgres"
REGION = "us-east-1"
DBNAME = "assetsintel"
PASS= "ScottRocks2020!*"
# gets the credentials from .aws/credentials
session = boto3.Session(profile_name='default')
client = session.client('rds')
print(f'Client: {client}')
token = client.generate_db_auth_token(DBHostname=ENDPOINT, Port=PORT, DBUsername=USR, Region=REGION)
print(f'Token: {token}')
try:
conn = psycopg2.connect(host=ENDPOINT, port=PORT, database=DBNAME, user=USR, password=token,
sslmode='prefer', sslrootcert="/home/mobin/Downloads/boston_mobin_lus_.pem")
cur = conn.cursor()
cur.execute("""SELECT now()""")
query_results = cur.fetchall()
print(f'Query results: {query_results}')
except Exception as e:
print("Database connection failed due to {}".format(e))
return None
except Exception as error:
logging.error(f'in Creating connection ! {error}')
toker 打印后卡住了:
错误:
Database connection failed due to could not connect to server: Connection timed out
Is the server running on host "assetsintel.cluster-cjeiapuahe69.us-east-1.rds.amazonaws.com" (172.31.10.129) and accepting
TCP/IP connections on port 5432?
【问题讨论】:
-
您必须转到 rds 的安全组并允许来自应用程序的传入连接。你做了那部分吗?你在哪里运行 python 代码。
-
@ArunK 我在 ubuntu 上的本地机器上运行我的代码,是的,我已经完成了允许入站和出站流量...
-
这不是python程序的问题。这是一个网络问题。这就是我为您发布答案的原因。从 rds 实例开始。检查 rds 实例是否配置为可公开访问。然后进入子网,看看子网是否有通往互联网的路由。
标签: python postgresql amazon-web-services aws-aurora-serverless