NodeJS / psql: Getting error "connection terminated unexpectedly"

Viewed 569

I am trying to set up a node js application with a psql database. When I run node App.js I get the error "Error: Connection terminated unexpectedly at Connection.con.once (/home/bismarck/Documents/node_modules/pg/lib/client.js:132:73)"

I have confirmed that the credentials I am using in my application are correct and that I can query the mydb database using those creentials. I also confirm that the sql service is running using systemctl service start. Here is the relevant Code.

const config = {
 21         host: 'localhost',
 22         user: username,
 23         password: password,
 24         database: 'mydb',
 25         port: port,
 26 }
 27 
 28 const client = new Client(config);
 29 client.connect(err => {
 30         if (err) throw err;
 31         else {
 32                 getData();
 33         }
 34 });

1 Answers

I had this problem too, in my case it was cause i tried to connect db in production.

so, I added ssl to Pool, like this:

const pool = new Pool({
    connectionString: connectionString,
    ssl: {rejectUnauthorized: false},
});

Hope it helps you too...

Related