【问题标题】:Get All table names in Redshift获取 Redshift 中的所有表名
【发布时间】:2019-05-23 12:30:34
【问题描述】:

我正在尝试获取 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

【问题讨论】:

  • 您可以在问题中添加error 吗?
  • @RedBoy 我没有收到任何错误。它只是打印无。相应地编辑了问题

标签: aws-lambda amazon-redshift


【解决方案1】:

我猜你在查询执行后遗漏了最重要的几行 -

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-02
    • 1970-01-01
    • 1970-01-01
    • 2011-08-04
    • 1970-01-01
    • 2021-07-15
    • 2015-09-13
    • 1970-01-01
    相关资源
    最近更新 更多