You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 19:12:11