Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

304
Views
Cannot connect local PostgreSQL DB in Nodejs

I connect my PostgreSQL local database in Node.js server using pg.

All worked fine, but suddenly it stopped to work.

This is my code:

const {Pool} = require('pg')
const connectionString = process.env.DATABASE_URL;
const ssl = process.env.NODE_ENV == 'production' ? '?ssl=true' : '';
const pool = new Pool({
    connectionString: connectionString + ssl
});

const db = {
    pool: pool,
    test: async () => {
        try {
            console.log('before connection');
            let result = await pool.query('SELECT $1::text as status', ['connected']);
            console.log('connected')
            return result.rows[0].status;
        } catch (error) {
            console.error('Unable to connect to the database:', error);
            return null;
        }
    }
}

module.exports = db;

Here I call it:

const express = require('express')
const Router = express.Router();
const db = require('./database')

Router.get('/testdb', async (req, res) => {
    try {
        let result = await db.test();
        res.status(200).send(result);
    } catch (error) {
        res.status(500).send(error);
    }
})

It stuck on pool.query. 'before connection' is logged, no error is thrown.

The very same code works fine with my remote db, and I can connect the local db with pgAdmin.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

This was an incompatibility issue between pg and the node.js 14 version. See also the following GitHub issues where this was tracked and fixed:

  • https://github.com/brianc/node-postgres/issues/2180
  • Pull request: https://github.com/brianc/node-postgres/pull/2171

tl;dr: Upgrade pg to a version larger than 8.0.3 (pg>=8.0.3).

over 4 years ago · Santiago Trujillo Report

0

I found my problem in node version - some days ago I updated it to 14.5.0.

revise it to 12.18.2 LTS solve the problem.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!