本文介绍了Postgresql:连接被拒绝.检查主机名和端口是否正确以及 postmaster 是否正在接受 TCP/IP 连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试连接 postgresql,但出现此错误.

I am trying to connect postgresql but I am getting this error.

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 文件是这样的.

My pg_hba.conf file is like this.

 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.

推荐答案

你引用的错误与pg_hba.conf无关;是连接失败,不是连接授权失败.

The error you quote has nothing to do with pg_hba.conf; it's failing to connect, not failing to authorize the connection.

按照错误消息的说明进行操作:

Do what the error message says:

检查主机名和端口是否正确以及 postmaster 是否接受 TCP/IP 连接

您尚未显示产生错误的命令.假设您正在连接 localhost 端口 5432(标准 PostgreSQL 安装的默认设置),然后:

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:

  • PostgreSQL 没有运行

  • PostgreSQL isn't running

PostgreSQL 没有侦听 TCP/IP 连接(postgresql.conf 中的listen_addresses)

PostgreSQL isn't listening for TCP/IP connections (listen_addresses in postgresql.conf)

PostgreSQL 仅侦听 IPv4(0.0.0.0127.0.0.1),而您正在连接 IPv6(::1) 反之亦然.在某些具有奇怪 IPv6 套接字行为的较旧 Mac OS X 版本和某些较旧的 Windows 版本上,这似乎是一个问题.

PostgreSQL is only listening on IPv4 (0.0.0.0 or 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 正在侦听与您正在连接的端口不同的端口

PostgreSQL is listening on a different port to the one you're connecting on

(不太可能)有一个 iptables 规则阻止环回连接

(unlikely) there's an iptables rule blocking loopback connections

(如果您没有连接localhost,它也可能是阻止TCP/IP连接的网络防火墙,但我猜您正在使用默认,因为你没有说).

(If you are not connecting 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).

所以......检查那些:

So ... check those:

  • ps -f -u postgres 应该列出 postgres 进程

sudo lsof -n -u postgres |grep LISTENsudo netstat -ltnp |grep postgres 应该显示 PostgreSQL 正在监听的 TCP/IP 地址和端口

sudo lsof -n -u postgres |grep LISTEN or sudo netstat -ltnp | grep postgres should show the TCP/IP addresses and ports PostgreSQL is listening on

顺便说一句,我认为您使用的是旧版本.在我的 9.3 安装中,错误更详细:

BTW, I think you must be on an old version. On my 9.3 install, the error is rather more detailed:

$ 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?

这篇关于Postgresql:连接被拒绝.检查主机名和端口是否正确以及 postmaster 是否正在接受 TCP/IP 连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 21:16