[英]Connecting to Redshift Data Using Python - Error: current transaction is aborted, commands ignored until end of transaction block
I am trying to connect the Redshift data using Python (Jupyter Notebook).我正在尝试使用 Python(Jupyter Notebook)连接 Redshift 数据。 after running the last line, am getting this error:运行最后一行后,出现此错误:
InFailedSqlTransaction: current transaction is aborted, commands ignored until end of transaction block InFailedSqlTransaction:当前事务被中止,命令被忽略直到事务块结束
could you please help me here.你能帮帮我吗?
import psycopg2
con=psycopg2.connect(dbname= 'TBD', host='TBD',
port= 'TBD', user= 'TBD', password= 'TBD')
cur = con.cursor()
cur.execute("SELECT site_id FROM dfa_std")
As well as psycopg2
you need have sqlalchemy
and ipython-sql
installed in the Notebook server's Python.除了psycopg2
,您还需要在笔记本服务器的 Python 中安装sqlalchemy
和ipython-sql
。
Create a file with your Redshift endpoint and credentials:使用您的 Redshift 端点和凭据创建一个文件:
echo "{
\"user_name\": \"my_user\",
\"password\": \"my_pswd\",
\"host_name\": \"my_cluster_endpoint\",
\"port_num\": \"5439\",
\"db_name\": \"my_db\"
}" > my.creds
Then try this in your notebook:然后在你的笔记本上试试这个:
import sqlalchemy
import psycopg2
import simplejson
%reload_ext sql
%config SqlMagic.displaylimit = 25
with open("my.creds") as fh:
creds = simplejson.loads(fh.read())
connect_to_db = "postgresql+psycopg2://" + \
creds["user_name"] +':'+ creds["password"] +'@'+ \
creds["host_name"] +':'+ creds["port_num"] +'/'+ creds["db_name"];
%sql $connect_to_db
%sql SELECT current_user, version();
Expected output:预期 output:
'Connected: my_user@my_db'
current_user | version
my_user | PostgreSQL 8.0.2 on i686-pc-linux-gnu, compiled by GCC gcc (GCC) 3.4.2 20041017 (Red Hat 3.4.2-6.fc3), Redshift 1.0.24238
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.