Skip to main content
 首页 » 编程设计

python之使用 psycopg2 的 aws localstack redshift 连接问题

2025年02月15日9haluo1

使用 localstack 模拟 AWS 服务。 正在尝试使用 psycopg2 连接到本地 redshift 实例。但是连接超时。使用 boto3 的连接已成功完成。

client = boto3.client('redshift', endpoint_url='http://127.0.0.1:5439/') 
print client.describe_clusters() 
con = psycopg2.connect(dbname='test123', host='127.0.0.1', port=5439, user='user', password='password', connect_timeout=10) 

输出:

{u'Clusters': [{u'NumberOfNodes': 1, u'AvailabilityZone': 'localhost', u'NodeType': 'ds1.large', u'PubliclyAccessible': True, u'MasterUsername': 'user', u'ClusterParameterGroups': [{u'ParameterGroupName': 'default.redshift-1.0', u'ParameterApplyStatus': 'in-sync'}], u'Encrypted': True, u'ClusterSecurityGroups': [{u'Status': 'active', u'ClusterSecurityGroupName': 'Default'}], u'AllowVersionUpgrade': False, u'VpcSecurityGroups': [], u'ClusterSubnetGroupName': 'testgroup', u'AutomatedSnapshotRetentionPeriod': 1, u'ClusterStatus': 'creating', u'ClusterIdentifier': 'test123', u'DBName': 'test123', u'PreferredMaintenanceWindow': 'Mon:03:00-Mon:03:30', u'ClusterVersion': '1.0'}], 'ResponseMetadata': {'RetryAttempts': 0, 'HTTPStatusCode': 200, 'RequestId': '384ac68d-3775-11df-8963-01868b7c937a', 'HTTPHeaders': {'date': 'Thu, 28 Sep 2017 05:54:46 GMT', 'content-length': '1264', 'content-type': 'text/html; charset=utf-8', 'server': 'amazon.com'}}} 
Traceback (most recent call last): 
  File "/home/shrishinde/PycharmProjects/Test/redshift_test.py", line 46, in <module> 
    con = psycopg2.connect(dbname='test123', host='127.0.0.1', port=5439, user='user', password='password', connect_timeout=10) 
  File "/usr/local/lib/python2.7/dist-packages/psycopg2/__init__.py", line 130, in connect 
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync) 
psycopg2.OperationalError: timeout expired 

请提供有关问题所在或如何调试的意见。

请您参考如下方法:

我也在四处寻找如何连接到 localstack 的 redshift 服务器。根据这个 github 问题,它看起来并不是真正的完整 redshift 服务器,并且目前不可能以这种方式连接(截至 2017 年 3 月,显然情况仍然如此):https://github.com/localstack/localstack/issues/28

Basic support for Redshift is added in #29 . Note that the mock functionality only covers the basic Redshift API methods (a subset of https://docs.aws.amazon.com/redshift/latest/APIReference/). The actual query capabilities (i.e., accessing Redshift via JDBC) are not supported here (at this stage it is not feasible to mock the entire query engine).