PostgreSQL:连接被拒绝.检查主机名和端口是否正确以及邮局主管正在接受TCP/IP连接 [英] Postgresql : Connection refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections
问题描述
我正在尝试连接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:
检查主机名和端口是否正确以及邮局主管正在接受TCP/IP连接
Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections
您尚未显示产生错误的命令.假设您正在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.0
或127.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).
所以...检查那些:
-
ps -f -u postgres
应该列出postgres
进程
sudo lsof -n -u postgres |grep LISTEN
或sudo 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:连接被拒绝.检查主机名和端口是否正确以及邮局主管正在接受TCP/IP连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!