python - 无法从不同计算机通过 sqlalchemy 连接到 postgresql

标签 python postgresql sqlalchemy

我认为没有正确理解 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/

相关文章:

php - Heroku 找不到 PDOServiceProvider

python - 具有三个相互关联的表的多对多(SqlAlchemy)

python - 获取 SQLAlchemy ORM 类的模式名称

python - Pandas - 选择给定距离的多行

ruby-on-rails - 按模型与特定属性的关联计数排序

node.js - PostgreSQL 中跨多个时区的总时差

mysql - Flask + MySQL + PHP + Docker-Compose = 痛苦

python - 一张表格共享数据分成多行

python - pipenv 和 bash 别名

python - 如何打印 4 个字节作为整数