【发布时间】:2022-10-22 10:28:35
【问题描述】:
我有一个 postgres 数据库,目前正在我的 PC 上运行。我正在将使用所述数据库的烧瓶应用程序部署到 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 上设置防火墙规则以使用 Windows Defender 防火墙打开端口 5432 以进行入站和出站 TCP/IP 连接
-
设置远程 linux PC 防火墙以允许通过端口 5432 与线路连接
'sudo ufw 允许 5432/tcp' & 'sudo ufw 允许 postgres/tcp'
-
尝试了本地 PC IPv4 地址和默认网关地址(老实说,我不确定使用哪一个)
-
为我的物理路由器设置规则以允许连接到端口 5432
我想不出这个来挽救我的生命。任何帮助将不胜感激。
【问题讨论】:
-
@jjanes我如何访问/更改它?
-
这个试过了,还是不行
-
如果您仍然遇到超时,那么这是一个网络问题,而不是数据库本身的问题。要么您配置错误的路由器,要么无论路由器尝试做什么,ISP 本身都会阻止连接,或者类似的事情。 Stackoverflow 可能不是解决网络问题的正确论坛。
标签: linux postgresql tcp remote-access