无法使用psycopg2从Amazon Redshift读取数据

时间:2014-04-27 12:18:23

标签: python-2.7 psycopg2 amazon-redshift

您好我正在尝试使用Python代码从Amazon Redshift表中读取一些数据。

我正在使用psycopg2库。

以下是我正在使用的代码:

import psycopg2
try:
    conn = psycopg2.connect("dbname='testdb' port='5439' user='user' host='us-west.redshift.amazonaws.com' password='pass'");
except:
    print "I am unable to connect to the database"
cur = conn.cursor()
conn.set_isolation_level( psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT )
try:
    cursor.execute( 'begin' )
    cur.execute("""SELECT * from employee""")
    cursor.execute( 'commit' )
except:
    print "Unable to select from test database!"
rows = cur.fetchall()

但由于无法从测试数据库中进行选择,因此无法读取数据。

请建议。

1 个答案:

答案 0 :(得分:0)

我相信您需要输入redshift实例的全名,该实例比'us-west.redshift.amazonaws.com'更长:

conn = psycopg2.connect(“dbname ='testdb'port ='5439'user ='user'host =' REDSHIFT_INSTANCE_NAME.redshift.amazonaws.com 'password ='pass'”) ;

例如,主机类似于'tst.cryibvg33r9d.ap-southeast-1.redshift.amazonaws.com'