我认为没有正确理解 sqlalchemy 是如何工作的,我尝试从本地计算机连接到在某些云服务器上运行的 postgresql:
db = create_engine('postgresql://<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="36435443584243760701041804051807000618040704" rel="noreferrer noopener nofollow">[email protected]</a>:5432/dbname')
但这会导致错误:
Is the server running on host "172.23.160.212" and accepting
TCP/IP connections on port 5432?
我已经检查了端口和主机也存在。
我认为我应该首先使用 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://<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cebea1bdbaa9bcabbd8efffcf9e0fee0fee0ff" rel="noreferrer noopener nofollow">[email protected]</a>:5432/dbname')
但这并没有帮助。
最佳答案
我已经部分解决了问题,
如果在 bash ( ssh <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0b7e697e657f7e4b3a3c39253938253a3d3b25393a39" rel="noreferrer noopener nofollow">[email protected]</a> -L 5432:localhost:5432 -N -n -f
) 中打开 ssh 连接,则可以通过 python 打开数据库:
db = create_engine('postgresql://tissuemaps@localhost:5432/dbname')
如果我理解正确的话,直接连接到 postgres 应该也可以工作,但为什么不行,我不知道。
关于python - 无法从不同计算机通过 sqlalchemy 连接到 postgresql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52098712/