繁体   English   中英

Postgres 远程连接失败,'psql:错误:连接到 {server} 上的服务器,端口 5432 失败:连接超时'

[英]Postgres remote connection fails, 'psql: error: connection to server at {server}, port 5432 failed: Connection timed out'

我的 PC 上目前有一个 postgres 数据库。 我正在将使用所述数据库的烧瓶应用程序部署到 linux 服务器上,并且需要从 linux 机器远程连接到我的数据库。 我在 linux 机器上使用的命令是

psql -h 12.345.678.901 -p 5432 -U postgres

其中 12.345.678.901 是我的本地 PC ip 地址。 当我这样做时,我得到了错误

psql: error: connection to server at "12.345.678.901", port 5432 failed: Connection timed out
    Is the server running on that host and accepting TCP/IP connections?

我想强调的是连接没有被“拒绝”,它只是超时(不像许多与该主题相关的问题)。 我不确定这是否有助于确定根本问题。 我知道这是一个非常普遍的问题,但没有任何解决方案对我有用。 这些解决方案包括更新 pg_hba.conf、postgresql.conf、防火墙配置等。 我已经这样做了。 我的 pg_hba.conf 文件看起来像这样

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     scram-sha-256
# IPv4 local connections:
host    all             all             127.0.0.1/32            scram-sha-256
host    all             all             0.0.0.0/0               trust
# IPv6 local connections:
host    all             all             ::1/128                 scram-sha-256
host    all             all             ::0/0                   md5
# Allow replication connections from localhost, by a user with the
# replication privilege.
local   replication     all                                     scram-sha-256
host    replication     all             127.0.0.1/32            scram-sha-256
host    replication     all             ::1/128                 scram-sha-256

host all all 0.0.0.0/0 md5

我的 postgresql.conf 看起来像这样

# - Connection Settings -

listen_addresses = '*'          # what IP address(es) to listen on;
                                # comma-separated list of addresses;
                                # defaults to 'localhost'; use '*' for all
                                # (change requires restart)
port = 5432                     # (change requires restart)
max_connections = 100           # (change requires restart)

这些文件位于 C:\Program Files\PostgreSQL\14\data 中。 我手动检查了这些更改是否已保存并使用 psql shell 实现。 在对这些文件进行所有更改后,我还重新启动了 postgres。

我实施的其他修复:

  • 在本地 PC 上设置防火墙规则以打开端口 5432 以与 Windows Defender 防火墙进行入站和出站 TCP/IP 连接

  • 将远程 linux PC 防火墙设置为允许通过端口 5432 与线路进行连接

    'sudo ufw 允许 5432/tcp' & 'sudo ufw 允许 postgres/tcp'

  • 尝试了本地 PC IPv4 地址和默认网关地址(老实说,我不确定该使用哪一个)

  • 为我的物理路由器设置规则以允许连接到端口 5432

我想不出这个来挽救我的生命。 任何帮助将不胜感激。

对于为这个问题苦苦挣扎的其他人,经过数周的挖掘,我终于找到了解决方案。 您必须在 postgres 配置文件中使用的 IP 地址是路由器的 IP 地址。 我尝试了我计算机上的每个 IP 地址,但没有一个有效。 只有当我进入我的互联网提供商应用程序并找到我的路由器 IP 时,我才终于能够连接。 最大的说明是,当我通过 ssh 连接到我的远程服务器时,它会说“来自 {router ip address} 的连接”。 希望这可以帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM