我正在尝试获取Redshift数据库中的所有表。
import os
import psycopg2
import sys
import rds_config
def lambda_handler(event, context):
#for key in os.environ.keys():
# print(key)
#return 0
REDSHIFT_DATABASE = rds_config.db_name
REDSHIFT_USER = rds_config.db_username
REDSHIFT_PASSWD = rds_config.db_password
REDSHIFT_PORT = rds_config.db_port
REDSHIFT_ENDPOINT = rds_config.db_endpoint
QUERY = "SELECT DISTINCT tablename FROM pg_table_def WHERE schemaname = 'public' ORDER BY tablename;"
try:
conn = psycopg2.connect(
database = REDSHIFT_DATABASE,
user=REDSHIFT_USER,
password=REDSHIFT_PASSWD,
port=REDSHIFT_PORT,
host=REDSHIFT_ENDPOINT)
except Exception as ERROR:
print(ERROR)
sys.exit(1)
try:
cursor = conn.cursor()
print(QUERY)
print(cursor.execute(QUERY))
cursor.close()
conn.commit()
conn.close()
except Exception as ERROR:
print( ERROR)
sys.exit(1)
上面的查询正在RedshiftQuery Editor中运行,但是当我尝试使用lambda执行它时失败。
我只是想阅读Redshift中的所有表格。
我没有收到任何错误消息
print(cursor.execute(QUERY))
打印None
答案 0 :(得分:0)
我想您在查询执行后遗漏了最重要的几行-
rows = cursor.fetchall()
类似这样的
try:
# retrieving all tables in my search_path
cursor.execute("""select tablename from pg_table_def""")
except Exception as err:
print err.code,err
rows = cursor.fetchall()
for row in rows
print row
查看演示摘要: https://gist.github.com/jaychoo/4e3effdeed3672173b67