Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

307
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda