10

Connecting to postgres via pg8000 from SqlAlchemy worked fine until I enabled SSL on postgres.

db = create_engine('postgresql+pg8000://user:pass@hostname/dbname', echo=True).connect()

Now it seems to fail with:

File "/Library/Python/2.7/site-packages/pg8000/core.py", line 872, in __init__
raise InterfaceError("communication error", exc_info()[1])
sqlalchemy.exc.InterfaceError: (InterfaceError) ('communication error', error(61, 'Connection refused')) None None
4

3 回答 3

13

可能您需要添加 connect_args 字典:

db = create_engine('postgresql+pg8000://user:pass@hostname/dbname', connect_args={'sslmode':'require'}, echo=True).connect()
于 2014-08-02T13:42:39.037 回答
3

接受的答案不再有效,至少对于这些版本:

Python                   3.9
pg8000                   1.19.5
SQLAlchemy               1.4.12

pg8000文档描述了你必须做什么。利用

engine = create_engine('postgresql+pg8000://user:password@host/db', 
                       connect_args={'ssl_context': True})

它将ssl.create_default_context()的结果传递给连接创建者。如果需要自定义 SSL 上下文,请将其作为值传递,而不是True

import ssl

ssl_context = ssl.SSLContext()
# Set attributes as required  ...

engine = create_engine('postgresql+pg8000://user:password@host/db', 
                       connect_args={'ssl_context': ssl_context})
于 2021-05-22T08:23:40.313 回答
0

对我不起作用...我用过:

engine = create_engine('postgresql+pg8000://user:password@host/db', connect_args={'ssl': {'ssl-mode': 'required'}})
于 2021-10-17T17:24:11.763 回答