使用node-postgres通过LAN域名连PostgreSQL被拒,pgAdmin可正常连接
问题:pgAdmin4可正常连接PostgreSQL,但NPM pg包连接失败
现象
- pgAdmin 4通过本地域名
machineName.local能顺利连接PostgreSQL - 使用NPM的
pg包采用完全相同的客户端配置连接时,触发连接拒绝错误
错误详情
ConnectErr connect EHOSTUNREACH fe80::xx:xxxx:xxxx:xxxx:5432 - Local (fe80::xxxx:xxx:xxxx:xxx%en5:54602)
客户端配置(两者一致)
host: machineName.local port: 5432 user: "userName" password: "password" database: "postgres"
临时解决方案
将配置中的主机名替换为局域网IPv4地址,即可成功连接:
host: 192.168.x.x
排查怀疑点
推测问题出在NPM的pg包将本地域名machineName.local解析为IPv6地址,进而导致连接被拒绝。
已验证的pg_hba.conf配置
服务器端pg_hba.conf已配置支持IPv4和IPv6连接,配置内容如下:
# TYPE DATABASE USER ADDRESS METHOD # "local" is for Unix domain socket connections only local all all peer # IPv4 local connections: host all all 0.0.0.0/0 md5 # IPv6 local connections: host all all ::0/0 md5 # Allow replication connections from localhost, by a user with the replication privilege. local replication all md5 host replication all 127.0.0.1/32 md5 host replication all ::1/128 md5
内容的提问来源于stack exchange,提问作者WhatsYourFunction
相关产品推荐
相关产品推荐

