无法从其他计算机通过sqlalchemy连接到postgresql

2024-03-29 12:18:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我想我不太明白sqlalchemy是如何工作的,我试图从本地计算机连接到运行在某个云服务器上的postgresql:

db = create_engine('postgresql://ubuntu@172.23.160.212:5432/dbname')

但这会导致错误

^{pr2}$

我已经检查了端口和主机是否也存在。在

我想我应该先用ssh连接主机:

with SSHTunnelForwarder((172.23.160.212, 22), ssh_username='ubuntu', remote_bind_address=(127.0.0.1, 3306)) as server:
    db = create_engine('postgresql://postgres@127.0.0.1:5432/dbname')

但这没用。在


Tags: 端口服务器dbsqlalchemypostgresqlubuntu计算机错误
2条回答

我已经部分解决了这个问题

如果在bash(ssh ubuntu@172.23.160.212 -L 5432:localhost:5432 -N -n -f)中打开ssh连接,那么可以通过python打开db:

db = create_engine('postgresql://tissuemaps@localhost:5432/dbname')

如果我理解正确的话,直接与postgres的连接也应该起作用,为什么不起作用,我不知道。在

我觉得问题是TCP连接没有启用,必须修改你的pg_hba.conf文件以允许连接。在配置文件中添加行以允许连接

host    all             all              0.0.0.0/0                       md5
host    all             all              ::/0                            md5

除此之外,您还可以检查postgresql.conf(/etc/postgresql/9.3/main)/postgresql.conf)检查其他postgres配置是否符合您的预期,如端口号等。在配置文件的下面一行添加以接受所有连接

^{pr2}$

您需要重新启动postgres服务以获取更改

sudo service postgresql restart

https://www.postgresql.org/docs/9.1/static/auth-pg-hba-conf.htmlhttps://www.postgresql.org/docs/9.1/static/runtime-config-connection.html

相关问题 更多 >