要解决AWS Lambda与Postgres数据库在时间间隔上的连接问题,可以使用以下方法:
import psycopg2
def lambda_handler(event, context):
# 连接到Postgres数据库
conn = psycopg2.connect(
dbname='your-database-name',
user='your-username',
password='your-password',
host='your-hostname',
port='your-port'
)
# 执行查询
cursor = conn.cursor()
cursor.execute('SELECT * FROM your-table')
rows = cursor.fetchall()
# 处理查询结果
for row in rows:
# 在这里添加你的处理逻辑
print(row)
# 关闭数据库连接
conn.close()
这样,AWS Lambda函数将按照指定的时间间隔连接到Postgres数据库并执行查询。
请确保在Lambda函数的执行角色中,具有访问Postgres数据库的适当权限。