I’m trying to connect to my postgres (v16.3) database using a nodejs app (v20.15.0), both running locally but it throws an error.
import pg from 'pg';
const db = new pg.Client({
user: "postgres",
host: "localhost",
database: "world",
password: "123456",
port: 5432
});
db.connect();
let quiz = [];
db.query("SELECT * FROM capitals", (err, res) => {
if (err)
console.log("error executing query", err.stack);
else
quiz = res.rows;
})
db.end()
node:internal/process/promises:391
triggerUncaughtException(err, true /* fromPromise */);
^
Error: connect ECONNREFUSED 127.0.0.1:5432
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1606:16) {
errno: -111,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 5432
}
I checked database configuration and authorized firewall for all tcp traffic on that port, I tried connecting to the database using psql
and PgAdmin
both work just fine.
I cannot understand what’s causing this error.
New contributor
Djahid Hnf is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.