javascript - 与nodejs程序连接时出现错误: connect ECONNREFUSED 127. 0.0.1:5432

标签 javascript node-postgres postgresql-12 centos8

这个问题在这里已经有了答案:





Can't connect to Postgresql on port 5432

(15 个回答)


去年关闭。




我在 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_addresses 的注释。和 port行:
# - 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)

最佳答案

netstat在 5432 处没有显示任何正在收听的内容,这是您告诉 PostgreSQL 收听的地方。如果它不听,您将无法连接到它。为什么不听?也许它没有运行——你应该检查它的日志文件,看看发生了什么。

关于javascript - 与nodejs程序连接时出现错误: connect ECONNREFUSED 127. 0.0.1:5432,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66038165/

相关文章:

javascript - 测试后连接未关闭?

sql - PostgreSQL:是否可以确定数组中的任何元素是否与范围重叠?

c++ - 如何配置 pg_config/pgxs/make 以在 Makefile 中获取 CPPFLAGS 和 CFLAGS 以构建 Postgres C/C++ 扩展?

node.js - 使用 Sequelizejs 检索多行

PostgreSQL 12.3 : ERROR: out of memory for query result

javascript - 有没有办法将 Javascript 对象附加到选择选项

javascript - 无法让 knockout.js 依赖的可观察链接工作

javascript - req.body.name 是 Node js 中未定义的错误

javascript - 使用 JavaScript 禁用 html 按钮不会 POST

node.js - 将 postgres 与 nodejs 一起用于连接池