Pra*_*lli 6 python linux sqlalchemy psycopg2 amazon-redshift
我正在尝试在 Linux 环境中使用 Sqlalchemy 连接到我的 redshift 集群,但我面临以下问题。
Run Code Online (Sandbox Code Playgroud)from sqlalchemy import create_engine import pandas as pd conn = create_engine('postgresql://connection string') data_frame = pd.read_sql_query("SELECT * FROM schema.table", conn)
sqlalchemy.exc.ProgrammingError:(psycopg2.errors.UndefinedObject)无法识别的配置参数“standard_conforming_strings”
我真的不明白这是什么问题。它在 Windows 中运行得非常好。
PS:不确定这是否有什么区别,但我在 Linux 机器上安装了 psycopg2-binary,与 Windows 上的 psycopg2 形成鲜明对比。
编辑
1、pyscopg2在windows下的版本是2.9.3,在linux下pyscopg2-binary的版本是2.9.6
使用 pyscopg2 连接到 redshift 现在已经过时了。
对于直接连接,请使用 redshift_connector - 请参阅https://docs.aws.amazon.com/redshift/latest/mgmt/python-connect-examples.html
import redshift_connector
conn = redshift_connector.connect(
host='examplecluster.abc123xyz789.us-west-1.redshift.amazonaws.com',
database='dev',
port=5439,
user='awsuser',
password='my_password'
)
Run Code Online (Sandbox Code Playgroud)
对于 sqlAlchemy 使用 redshift_connect 和 sqlalchemy-redshift - 请参阅https://aws.amazon.com/blogs/big-data/use-the-amazon-redshift-sqlalchemy-dialect-to-interact-with-amazon-redshift/
import sqlalchemy as sa
from sqlalchemy.engine.url import URL
# build the sqlalchemy URL
url = URL.create(
drivername='redshift+redshift_connector', # indicate redshift_connector driver and dialect will be used
host='<clusterid>.xxxxxx.<aws-region>.redshift.amazonaws.com', # Amazon Redshift host
port=5439, # Amazon Redshift port
database='dev', # Amazon Redshift database
username='awsuser', # Amazon Redshift username
password='<pwd>' # Amazon Redshift password
)
engine = sa.create_engine(url)
Run Code Online (Sandbox Code Playgroud)
我根据@Adrian 的评论找到了答案。刚刚将 Linux 环境中的 Sql alchemy 版本更改为我在 Windows 上的版本,现在它可以工作了。
| 归档时间: |
|
| 查看次数: |
6424 次 |
| 最近记录: |