我正在try 连接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

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

推荐答案

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

按照错误消息所说的做:

判断主机名和端口是否正确,邮政局长是否接受TCP/IP连接

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

  • PostgreSQL没有运行

  • PostgreSQL没有监听TCP/IP连接(postgresql.conf中为listen_addresses)

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

  • PostgreSQL正在侦听与您连接的端口不同的端口

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

(如果你是notlocalhost上连接,它也可能是一个阻止TCP/IP连接的网络防火墙,但我猜你使用的是默认设置,因为你没有说).

所以判断一下:

  • ps -f -u postgres应该列出postgres个过程

  • sudo lsof -n -u postgres |grep LISTENsudo 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?

Postgresql相关问答推荐

PostgreSQL按描述请求

可以向判断枚举值的SQL列添加约束吗?

PostgreSQL\d命令:有办法只 Select 一列吗?

使用Helm设置PostgreSQL配置

Npgsql中准备好的语句和参数化查询有什么区别?

气流数据库如何定期管理?

PostgreSQL - 如何获得前一个月和一周的价值?

JPA findBy 字段忽略大小写

从具有加权行概率的 PostgreSQL 表中 Select 随机行

获取 OperationalError: FATAL: sorry, too many clients already using psycopg2

更详细地解释 JOIN 与 LEFT JOIN 和 WHERE 条件性能建议

带有双引号的 postgresql COPY 和 CSV 数据

在函数中返回字段作为插入结果

Postgres UPDATE with ORDER BY,怎么做?

如何只允许一个表的一行?

Postgres 外键on update和on delete选项如何工作?

如何启动 Postgres 服务器?

PGError:错误: column of relation does not exist

PGAdmin 编辑数据

在 Select (PostgreSQL/pgAdmin) 中将布尔值返回为 TRUE 或 FALSE