从 Redshift 查询时无法识别配置参数“standard_conforming_strings”

问题描述 投票:0回答:4

我正在尝试在 Linux 环境中使用 Sqlalchemy 连接到我的 redshift 集群,但我面临以下问题。

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 .windows下pyscopg2的版本是2.9.3,linux下pyscopg2-binary的版本是2.9.6

  1. Sqlalchemy在windows中的版本是1.4.39,在linux中是2.0.16
python linux sqlalchemy amazon-redshift psycopg2
4个回答
5
投票

使用 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'
  )

对于 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)

2
投票

我也有同样的问题

File "/home/ec2-user/miniconda3/envs/test_env/lib/python3.10/site-packages/sqlalchemy/engine/base.py", line 2339, in _handle_dbapi_exception
    raise sqlalchemy_exception.with_traceback(exc_info[2]) from e
  File "/home/ec2-user/miniconda3/envs/test_env/lib/python3.10/site-packages/sqlalchemy/engine/base.py", line 1965, in _exec_single_context
    self.dialect.do_execute(
  File "/home/ec2-user/miniconda3/envs/test_env/lib/python3.10/site-packages/sqlalchemy/engine/default.py", line 921, in do_execute
    cursor.execute(statement, parameters)
sqlalchemy.exc.ProgrammingError: (psycopg2.errors.UndefinedObject) unrecognized configuration parameter "standard_conforming_strings"

通过安装解决了它

sqlalchemy-redshift
https://pypi.org/project/sqlalchemy-redshift/

它做了什么

 pip install sqlalchemy-redshift
Collecting sqlalchemy-redshift
  Downloading sqlalchemy_redshift-0.8.14-py2.py3-none-any.whl (38 kB)
Requirement already satisfied: packaging in /home/ec2-user/miniconda3/envs/test_env/lib/python3.10/site-packages (from sqlalchemy-redshift) (23.1)
Collecting SQLAlchemy<2.0.0,>=0.9.2
  Downloading SQLAlchemy-1.4.49-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.6 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.6/1.6 MB 73.1 MB/s eta 0:00:00
Requirement already satisfied: greenlet!=0.4.17 in /home/ec2-user/miniconda3/envs/test_env/lib/python3.10/site-packages (from SQLAlchemy<2.0.0,>=0.9.2->sqlalchemy-redshift) (2.0.2)
Installing collected packages: SQLAlchemy, sqlalchemy-redshift
  Attempting uninstall: SQLAlchemy
    Found existing installation: SQLAlchemy 2.0.19
    Uninstalling SQLAlchemy-2.0.19:
      Successfully uninstalled SQLAlchemy-2.0.19
Successfully installed SQLAlchemy-1.4.49 sqlalchemy-redshift-0.8.14

0
投票

我已经根据@Adrian 的评论找到了答案。刚刚将 linux 环境中的 Sql alchemy 版本更改为我在 Windows 上的版本,现在它可以工作了。


0
投票

sqlalchemy-redshift 固定到 sqlalchemy <2.0.0 (https://github.com/sqlalchemy-redshift/sqlalchemy-redshift/blob/main/setup.py#L24)

我遇到这个错误是因为我安装了更高版本的 sqlalchemy。降级至 <2.0.0 fixed the error.

© www.soinside.com 2019 - 2024. All rights reserved.