远程 PostgreSQL 连接无法与正确的监听地址和 pg_hba 行配合使用

远程 PostgreSQL 连接无法与正确的监听地址和 pg_hba 行配合使用

我已经多次这样做过并且没有任何问题,但由于某种原因,我无法连接到远程 PostgreSQL 服务器。

我的服务器是 Debian 10,从存储库安装了 PostgreSQL 11,并添加了 TimescaleDB 扩展。除此之外没什么特别的。

/etc/postgresql/11/main/postgresql.conf包含行listen_addresses='*',并且/etc/postgresql/11/main/pg_hba.conf包含

local   all             postgres                                peer
local   all             all                                     peer
host    all             all             127.0.0.1/32            md5
host    all             all             ::1/128                 md5
local   replication     all                                     peer
host    replication     all             127.0.0.1/32            md5
host    replication     all             ::1/128                 md5
host    all             all             0.0.0.0/0               md5

我把所有东西都打开,只是为了能够连接,我可以验证本地登录是否有效psql -U postgres -h localhost,但是当我远程尝试同样的操作时,我得到了

psql: could not connect to server: No route to host
    Is the server running on host "db.example.com" (10.0.0.150) and accepting
    TCP/IP connections on port 5432?

服务器正在监听我看到的内容ss

$ ss -anp | grep 5432
u_str LISTEN 0      128           /var/run/postgresql/.s.PGSQL.5432 158425                                                * 0                                                                                                                   
tcp   LISTEN 0      128                                     0.0.0.0:5432                                            0.0.0.0:*                                                                                                                   
tcp   LISTEN 0      128                                        [::]:5432                                               [::]:*

我暂时禁用了防火墙以进行故障排除。其他服务(SSH)按预期工作,所以我真的不确定为什么这个不行。我唯一不确定的是,这个主机使用的 IP 之前分配给不再存在的 IP,但 DNS 报告两个不同名称的 IP 相同。我认为直接使用 IP 地址psql会忽略这个问题,但我得到了同样的错误。

最后我tcpdump在服务器上运行,它看到了连接请求

$ sudo tcpdump -i ens192 -vvvnxXS port 5432
tcpdump: listening on ens192, link-type EN10MB (Ethernet), capture size 262144 bytes
09:27:46.118205 IP (tos 0x0, ttl 64, id 18195, offset 0, flags [DF], proto TCP (6), length 60)
    10.0.2.3.59478 > 10.0.0.150.5432: Flags [S], cksum 0xea81 (correct), seq 826121699, win 64240, options [mss 1460,sackOK,TS val 2129657941 ecr 0,nop,wscale 7], length 0
    0x0000:  4500 003c 4713 4000 4006 dd10 0a00 0203  E..<G.@.@.......
    0x0010:  0a00 0096 e856 1538 313d 9de3 0000 0000  .....V.81=......
    0x0020:  a002 faf0 ea81 0000 0204 05b4 0402 080a  ................
    0x0030:  7ef0 0055 0000 0000 0103 0307            ~..U........

但客户端什么也看不到。

我想知道这是否是两者之间的路由问题,但我真的不知道怎么回事,因为mtr针对 IP 地址运行只得到一个单跳结果。此外,我尝试从位于共享交换机的同一组虚拟主机上的虚拟机进行连接。

答案1

此问题确实与路由有关,是由重复使用 DNS 预留引起的。当我的 IT 部门更改服务器的 IP 地址时,它开始起作用。

相关内容