简体   繁体   English

使用 Python 连接到 Redshift 数据 - 错误:当前事务被中止,命令被忽略,直到事务块结束

[英]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 中安装sqlalchemyipython-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.

相关问题 InternalError:当前事务中止,命令被忽略,直到事务块结束 - InternalError: current transaction is aborted, commands ignored until end of transaction block DatabaseError:当前事务被中止,在事务块结束之前忽略命令? - DatabaseError: current transaction is aborted, commands ignored until end of transaction block? django-当前事务中止,命令被忽略,直到事务块结束 - django - current transaction is aborted, commands ignored until end of transaction block 错误:当前事务中止,创建新记录时命令被忽略,直到事务块结束 - Error: current transaction is aborted, commands ignored until end of transaction block while creating a new record DatabaseError:当前事务中止,命令被忽略,直到事务块结束-在隐身模式下,但正常情况下没有错误 - DatabaseError: current transaction is aborted, commands ignored until end of transaction block - in incognite mode but no error in normal 如何调试:内部错误当前事务被中止,命令被忽略直到事务块结束 - How to debug: Internal Error current transaction is aborted, commands ignored until end of transaction block django reg extension-当前事务中止,命令被忽略,直到事务块结束 - django reg extend - current transaction is aborted, commands ignored until end of transaction block psycopg2.errors.InFailedSqlTransaction:当前事务被中止,命令被忽略直到事务块结束 - psycopg2.errors.InFailedSqlTransaction: current transaction is aborted, commands ignored until end of transaction block SQLAlchemy + postgres:(InternalError)当前事务中止,命令被忽略,直到事务块结束 - SQLAlchemy + postgres : (InternalError) current transaction is aborted, commands ignored until end of transaction block Django:通过添加m2m导致“当前事务中止,命令被忽略,直到事务块结束” - Django: Added m2m via through causes 'current transaction is aborted, commands ignored until end of transaction block'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM