【问题标题】:Can't connect remotely to Postgres database无法远程连接到 Postgres 数据库
【发布时间】:2025-12-12 17:00:02
【问题描述】:

在 Ubuntu 16.04 上运行 Postgresql 9.5。

我的 /etc/postgresql/9.5/main/pg_hba.conf:

local   all             postgres                                peer

# TYPE  DATABASE        USER            ADDRESS                 METHOD

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

我的/etc/postgresql/9.5/main/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)
#superuser_reserved_connections = 3 # (change requires restart)
unix_socket_directories = '/var/run/postgresql' # comma-separated list of directories
                    # (change requires restart)
#unix_socket_group = ''         # (change requires restart)
#unix_socket_permissions = 0777     # begin with 0 to use octal notation
                    # (change requires restart)
#bonjour = off              # advertise server via Bonjour
                    # (change requires restart)
#bonjour_name = ''          # defaults to the computer name
                    # (change requires restart)

# - Security and Authentication -

#authentication_timeout = 1min      # 1s-600s
#ssl = true             # (change requires restart)
#ssl_ciphers = 'HIGH:MEDIUM:+3DES:!aNULL' # allowed SSL ciphers
                    # (change requires restart)

我的 netstat -nlp | grep 5432:

tcp        0      0 0.0.0.0:5432            0.0.0.0:*               LISTEN      22621/postgres  
tcp6       0      0 :::5432                 :::*                    LISTEN      22621/postgres  
unix  2      [ ACC ]     STREAM     LISTENING     10261951 22621/postgres      /var/run/postgresql/.s.PGSQL.5432

我可以在本地连接,但无法远程连接。当我尝试从另一台计算机连接时,命令超时。

【问题讨论】:

  • 您确定没有设备等阻止 TCP 5432?当您从远程计算机 telnet 到 TCP 5432 时会发生什么?
  • telnet 5432 --> Trying XXX.XXX.XX.XXX........连接超时
  • 我还按照@Z4-tier 的建议禁用了防火墙

标签: postgresql ubuntu-16.04 postgresql-9.5


【解决方案1】:

确保您没有在与阻止 5432 的数据库相同的主机上运行防火墙。我不是 Ubuntu 向导,但我认为您可以做到

ufw disable

关闭它。如果这不起作用,请尝试

service iptables stop

或者可能

iptables -F

请注意,这些(当然)会关闭您的防火墙,因此请注意这一点。最后一个将刷新您的防火墙规则,因此如果您想重新打开它,则需要重新读取您的规则集。

正如@Pancake 建议的那样,看看如果你只是从另一台主机远程登录到 5432(或使用 netcat 等)会发生什么。

【讨论】:

  • 抱歉,这是我唯一可以发表评论的地方,显然......您用来测试连接的机器是否与数据库服务器在同一子网上?您是否在可能会在测试服务器和数据库之间丢弃这些数据包的公司/企业/某事网络上?
  • 另外,由于您在问题中没有提到它:您是否确保在修改它们后让 postgres 重新读取这些配置文件(假设您进行了更改)?
  • 它不在同一个子网上——这可能是问题吗?当服务器和客户端连接在同一子网上时,此设置曾经有效。我重新启动了 postgres (sudo systemctl restart postgresql),它重新读取了配置文件。
  • 这让我感觉网络设备正在两台主机之间的某处丢弃数据包。值得一试的是尝试在不同的端口上启动数据库,看看它是否突然开始工作。也许尝试一个您知道打开的端口,例如端口 22(但在这种情况下您必须重新配置 sshd)或者更有条理并使用 nmap/netcat 检查打开的端口。这可能会引起您的网络管理员的愤怒。它可能不起作用,具体取决于他们检查和过滤流量的方式。可能更容易询问他们是否正在减少您的流量:)