node-postgres: Connection terminated unexpectedly

Im trying to connecto to remote database. I can connect using psql client. But i get “Connection terminated unexpectedly” error while trying to run this (with same connection string as in psql clinet):

const { Pool, Client } = require('pg')
const connectionString = '...'

const pool = new Pool({
  connectionString: connectionString,
})

pool.query('SELECT NOW()', (err, res) => {
  console.log(err, res)
  pool.end()
})

const client = new Client({
  connectionString: connectionString,
})
client.connect()

client.query('SELECT NOW()', (err, res) => {
  console.log(err, res)
  client.end()
})

ive been trying to connect within sequelize ORM, but got same error.

About this issue

  • Original URL
  • State: open
  • Created 6 years ago
  • Comments: 16 (2 by maintainers)

Commits related to this issue

Most upvoted comments

I guess you’re not waiting for db to connect and making requests to early.

Although I’m not using pools, for the benefit of anyone else here from Google I had this problem when the connection sat idle for a while and was disconnected. The problem is that the exception that is thrown as a result of this unexpected disconnection cannot be caught and causes Node to terminate. I fixed the problem by adding an error handler so that the exception was never thrown in the first place:

db = new pg.Client();
db.on('error', e => {
  console.error('Database error', e);
  db = null;
});

I then just make sure the db object is valid before I use it, and reconnect to the DB first if not.

Using native mode fixed problem for client query

const { Pool, Client } = require('pg').native

@Upperfoot The connection to the DB seems to be OK, but the server cannot automatically recover from this error and must be restarted. After the restart, it will reconnect to the DB without problem.

For listen/notify connections I use this pattern:

function randomBetween(min, max) {
  return Math.floor(Math.random() * (max - min + 1) + min);
}

function *backoff() {
  let sleep = initialDelay;
  do {
    yield sleep;
    sleep = Math.min(maxDelay, randomBetween(initialDelay, sleep * 2));
  } while (true)
}



async function reconnect() {
  if (client) {
    cleanupSomehow(client);
  }

  for (const nextDelay of backoff()) {
    try {
      client = await connectSomehow();
    } catch {
      await new Promise(res => setTimeout(res, nextDelay));
    }
  }

  client.on('error', reconnect);

  client.connection.stream.setTimeout(heartbeatDealy);

  client.connection.stream.on('timeout', async function ()  {
    const timeoutId = setTimeout(reconnect, heartbeatTimeout);
    try {
      await client.query('SELECT 1');
    } catch (err) {
      reconnect();
    } finally {
      clearTimeout(timeoutId);
    }
  });

  setupListenNotify(client);
}

This executes a heartbeat query, after a set amount of inactivity on the connection. If the query times out or fails for whatever reason, I create and setup a new connection.

You could use the pool.connect() to get a client for this purpose, or just create a new Client outside of the pool.

node pg Client and Pool instances extend EventEmitter

node event emitter docs state:

If an EventEmitter does not have at least one listener registered for the ‘error’ event, and an ‘error’ event is emitted, the error is thrown, a stack trace is printed, and the Node.js process exits.

So if you don’t want your node process to exit - register an error handler. eg: client.on('error', handleError)

@Upperfoot and how did you solve this setup in NestJS TypeORM?

Hey @tocosastalo I just installed the pg-native npm library which solved it, it’s automatically used if detected, and to be honest it’s comparatively faster than the normal pg npm library anyway

@Upperfoot Thanks for the quick reply. So I added "pg-native": "^3.0.0", to my package.json and tried following test:

  • rebuild server docker container
  • start server and postgres containers
  • kill postgres container using docker stop
  • server crashed with:
events.js:187
throw er; // Unhandled 'error' event
       ^
Error: server closed the connection unexpectedly
        This probably means the server terminated abnormally
        before or while processing the request.

    at module.exports.Client._readError (/usr/src/cvd/node_modules/pg-native/index.js:154:13)
    at module.exports.Client._read (/usr/src/cvd/node_modules/pg-native/index.js:203:17)
    at PQ.emit (events.js:210:5)
    at PQ.EventEmitter.emit (domain.js:476:20)
Emitted 'error' event on  instance at:
     at module.exports.<anonymous> (/usr/src/cvd/node_modules/pg/lib/native/client.js:112:14)
     at module.exports.emit (events.js:210:5)
     at module.exports.EventEmitter.emit (domain.js:476:20)
     at module.exports.Client._readError (/usr/src/cvd/node_modules/pg-native/index.js:155:8)
     at module.exports.Client._read (/usr/src/cvd/node_modules/pg-native/index.js:203:17)
     at PQ.emit (events.js:210:5)
    at PQ.EventEmitter.emit (domain.js:476:20)

Didn’t you encounter this error?

No, I didn’t get that error, looks like the connection attempt was blocked, can you check connectivity via https://www.postgresql.org/docs/9.3/app-pg-isready.html or something similar to narrow the issue down a bit more?