postgresql:连接被拒绝检查主机名和端口是否正确,以及邮局主管是否接受tcp/ip连接

5rgfhyps  于 2021-05-16  发布在  Spark
关注(0)|答案(1)|浏览(1317)

我试图连接postgresql,但我得到这个错误。

org.postgresql.util.PSQLException: Connection refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.

我的pg_hba.conf文件是这样的。

TYPE  DATABASE        USER            CIDR-ADDRESS            METHOD

# IPv4 local connections:

host    all             all             127.0.0.1/32            md5

# IPv6 local connections:

host    all             all             ::1/128                 md5

如果有人能解释一下这里有什么问题,我将不胜感激。

tcbh2hod

tcbh2hod1#

你所引用的错误与此无关 pg_hba.conf ; 这是连接失败,而不是授权连接失败。
执行错误消息中所述的操作:
检查主机名和端口是否正确,以及邮局主管是否接受tcp/ip连接
您尚未显示产生错误的命令。假设你正在连接 localhost 港口 5432 (标准postgresql安装的默认设置),然后:
postgresql没有运行
postgresql没有侦听tcp/ip连接( listen_addressespostgresql.conf )
postgresql只监听ipv4( 0.0.0.0 或者 127.0.0.1 )你正在ipv6上连接( ::1 )反之亦然。在一些具有奇怪ipv6套接字行为的旧macosx版本和一些旧windows版本上,这似乎是一个问题。
postgresql正在另一个端口上侦听您正在连接的端口
(不太可能)有一个 iptables 规则阻止环回连接
(如果您没有连接到 localhost ,它也可能是一个阻止tcp/ip连接的网络防火墙,但我猜你使用的是默认值,因为你没有说)。
所以。。。检查这些: ps -f -u postgres 应列出 postgres 过程 sudo lsof -n -u postgres |grep LISTEN 或者 sudo netstat -ltnp | grep postgres 应该显示postgresql正在侦听的tcp/ip地址和端口
顺便说一句,我想你一定是老版本了。在我的9.3安装中,错误更为详细:

$ psql -h localhost -p 12345
psql: could not connect to server: Connection refused
        Is the server running on host "localhost" (::1) and accepting
        TCP/IP connections on port 12345?

相关问题