Error: connect ECONNREFUSED 127.0.0.1:5432 when connecting with nodejs program [duplicate]
Asked Answered
W

1

17

I have a postgresql server set up on a CentOS 8 machine, and a js program running on the same machine, using the pg library.

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
});

I set up the pg Pool correctly, and it works just fine if I want to connect it to my AWS postgres server (with the correct endpoint and such, of course), but when I try to get it to connect to the postgres server I have set up on the same machine, I get the following error:

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
}

I followed the directions to set up the pg_hba.conf file to supposedly allow for password login instead of 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

and in the postgresql.conf I uncommented the listen_addresses and port lines:

# - 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

Any help is appreciated.

Result of 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

(# used to hide IP)

Warrantable answered 4/2, 2021 at 1:33 Comment(4)
Try killing port 5432 with lsof -t -i tcp:5432 | xargs kill -9 then re-connecting.Rebekah
@RandyCasburn that doesn't really help, but I did try the netstat command like someone did in the comments. I added the result to my original question.Warrantable
@TobiahRex It's saying "not enough arguments" when using lsof -t -i tcp:5432 | xargs kill -9Warrantable
@K.Sutherland Try without the -9 as in lsof -t -i tcp:5432 | xargs killRebekah
D
6

The netstat doesn't show anything listening at 5432, which is where you've told PostgreSQL to listen. If it's not listening, you can't connect to it. Why isn't it listening? Maybe it's not running -- you should check its log files and see what happened.

Dominate answered 4/2, 2021 at 2:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.