连接nodejs程序时出现错误:connect ECONNREFUSED 127.0.0.1:5432

12

我在一台CentOS 8机器上设置了一个PostgreSQL服务器,并且有一个JS程序在同一台机器上运行,使用pg库。

const { Pool } = require('pg')

const pool = new Pool({
    user: process.env.PG_USER,      //postgres user
    host: process.env.PG_ENDPOINT,  //localhost (I also tried 127.0.0.1)
    database: process.env.PG_DB,    //database name to connect to
    password: process.env.PG_PASS,  //postgres user password
    port: process.env.PG_PORT       //5432
});

我正确地设置了pg池,如果我想将其连接到我的AWS postgres服务器(当然,使用正确的端点等),它就可以正常工作。但是,当我尝试连接到我在同一台机器上设置的postgres服务器时,就会出现以下错误:
Error: connect ECONNREFUSED 127.0.0.1:5432
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1146:16) {
  errno: -111,
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '127.0.0.1',
  port: 5432
}

我遵循了设置pg_hba.conf文件的指示,以便允许密码登录,而不是ident:

# "local" is for Unix domain socket connections only
local   all             all                                     peer
# IPv4 local connections:
host    all             all             127.0.0.1/32            ident
host    all             all             127.0.0.1/32            md5
# IPv6 local connections:
host    all             all             ::1/128                 ident
host    all             all             ::1/128                 md5

# Allow replication connections from localhost, by a user with the
# replication privilege.
local   replication     all                                     peer
host    replication     all             127.0.0.1/32            ident
host    replication     all             127.0.0.1/32            md5
host    replication     all             ::1/128                 ident
host    replication     all             ::1/128                 md5

我在postgresql.conf文件中取消了listen_addressesport行的注释:

# - Connection Settings -

listen_addresses = 'localhost'          # what IP address(es) to listen on;
                                        # comma-separated list of addresses;
                                        # defaults to 'localhost'; use '*' for all
                                        # (change requires restart)
port = 5432                             # (change requires restart)
max_connections = 100                   # (change requires restart)
#superuser_reserved_connections = 3     # (change requires restart)
#unix_socket_directories = '/var/run/postgresql, /tmp'  # comma-separated list of directories

需要帮助,请赐教。

netstat -na的结果如下:

Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN
tcp        0      0 ###.##.43.124:22        ###.##.122.136:59611   ESTABLISHED
tcp        0     64 ###.##.43.124:22        ###.##.122.136:59445   ESTABLISHED
tcp6       0      0 :::22                   :::*                    LISTEN
udp        0      0 127.0.0.1:323           0.0.0.0:*
udp        0      0 0.0.0.0:68              0.0.0.0:*
udp6       0      0 ::1:323                 :::*
raw6       0      0 :::58                   :::*                    7

(# 用于隐藏 IP)


3
尝试使用 lsof -t -i tcp:5432 | xargs kill -9 来关闭端口 5432,然后重新连接。 - Tobiah Rex
@RandyCasburn,那并没有真正帮到我,但我确实尝试了像评论中某人所做的netstat命令。我将结果添加到了我的原始问题中。 - K. Sutherland
@TobiahRex 当使用lsof -t -i tcp:5432 | xargs kill -9时,它会显示“参数不足”。 - K. Sutherland
1
@K.Sutherland 尝试不加上 -9,例如 lsof -t -i tcp:5432 | xargs kill - Tobiah Rex
1个回答

2

netstat没有显示任何在5432端口监听的内容,而你已经告诉PostgreSQL要监听这个端口。如果它不在监听,那么你就无法连接它。为什么它不在监听?也许它没有运行——你应该检查它的日志文件并查看发生了什么。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接