Postgresql:连接被拒绝。检查主机名和端口是否正确以及 postmaster 是否接受 TCP/IP 连接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20825734/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Postgresql : Connection refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections
提问by Rose18
I am trying to connect postgresql but I am getting this error.
我正在尝试连接 postgresql,但出现此错误。
org.postgresql.util.PSQLException: Connection refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
My pg_hba.conf file is like this.
我的 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
I would be much obliged if anyone please be so kind enough to explain whats hoing on here and how should I correct it.
如果有人能如此友好地解释这里发生的事情以及我应该如何纠正它,我将不胜感激。
回答by Craig Ringer
The error you quote has nothing to do with pg_hba.conf
; it's failing to connect, not failing to authorize the connection.
您引用的错误与此无关pg_hba.conf
;它无法连接,而不是无法授权连接。
Do what the error message says:
执行错误消息所说的操作:
Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections
检查主机名和端口是否正确以及 postmaster 是否接受 TCP/IP 连接
You haven't shown the command that produces the error. Assuming you're connecting on localhost
port 5432
(the defaults for a standard PostgreSQL install), then either:
您尚未显示产生错误的命令。假设您在localhost
端口上连接5432
(标准 PostgreSQL 安装的默认设置),则:
PostgreSQL isn't running
PostgreSQL isn't listening for TCP/IP connections (
listen_addresses
inpostgresql.conf
)PostgreSQL is only listening on IPv4 (
0.0.0.0
or127.0.0.1
) and you're connecting on IPv6 (::1
) or vice versa. This seems to be an issue on some older Mac OS X versions that have weird IPv6 socket behaviour, and on some older Windows versions.PostgreSQL is listening on a different port to the one you're connecting on
(unlikely) there's an
iptables
rule blocking loopback connections
PostgreSQL 没有运行
PostgreSQL 不侦听 TCP/IP 连接 (
listen_addresses
inpostgresql.conf
)PostgreSQL 仅侦听 IPv4 (
0.0.0.0
或127.0.0.1
),而您正在连接 IPv6 (::1
),反之亦然。这似乎是某些具有奇怪 IPv6 套接字行为的较旧 Mac OS X 版本以及某些较旧 Windows 版本上的问题。PostgreSQL 正在侦听与您正在连接的端口不同的端口
(不太可能)有
iptables
阻止环回连接的规则
(If you are notconnecting on localhost
, it may also be a network firewall that's blocking TCP/IP connections, but I'm guessing you're using the defaults since you didn't say).
(如果您没有连接到localhost
,它也可能是阻止 TCP/IP 连接的网络防火墙,但我猜您正在使用默认值,因为您没有说)。
So ... check those:
所以......检查那些:
ps -f -u postgres
should listpostgres
processessudo lsof -n -u postgres |grep LISTEN
orsudo netstat -ltnp | grep postgres
should show the TCP/IP addresses and ports PostgreSQL is listening on
ps -f -u postgres
应该列出postgres
进程sudo lsof -n -u postgres |grep LISTEN
或者sudo netstat -ltnp | grep postgres
应该显示 PostgreSQL 正在监听的 TCP/IP 地址和端口
BTW, I think you must be on an old version. On my 9.3 install, the error is rather more detailed:
顺便说一句,我认为您必须使用旧版本。在我的 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?