import psycopg2
def lambda_handler(event, context): try: conn = psycopg2.connect( dbname="mydb", user="myuser", password="mypassword", host="myhost", port="5432" ) return { 'statusCode': 200, 'body': 'Successfully connected to PostgreSQL database' } except Exception as e: print(e) return { 'statusCode': 500, 'body': 'Failed to connect to PostgreSQL database' }
import psycopg2
def lambda_handler(event, context): try: conn = psycopg2.connect( dbname="mydb", user="myuser", password="mypassword", host="myhost", port="5432" ) # do something with the database connection return { 'statusCode': 200, 'body': 'Successfully connected to PostgreSQL database' } except Exception as e: # log the error print(e) raise e
这样可以捕获并输出正在发生的异常,帮助确定问题的原因。