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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 00:33:44  来源:igfitidea点击:

Postgresql : Connection refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections

postgresql

提问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 localhostport 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_addressesin postgresql.conf)

  • PostgreSQL is only listening on IPv4 (0.0.0.0or 127.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 iptablesrule blocking loopback connections

  • PostgreSQL 没有运行

  • PostgreSQL 不侦听 TCP/IP 连接 ( listen_addressesin postgresql.conf)

  • PostgreSQL 仅侦听 IPv4 (0.0.0.0127.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 postgresshould list postgresprocesses

  • sudo lsof -n -u postgres |grep LISTENor sudo netstat -ltnp | grep postgresshould 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?