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
- Use postgres-pool instead of node-postgres This fixes various issues with node-postgres, specifically that the connection pooling implementation does not work well: https://github.com/brianc/node-po... — committed to proofcarryingdata/zupass by robknight a year ago
- Use postgres-pool instead of node-postgres (#438) This fixes various issues with node-postgres, specifically that the connection pooling implementation does not work well: https://github.com/bria... — committed to proofcarryingdata/zupass by robknight a year ago
- Use postgres-pool instead of node-postgres (#438) This fixes various issues with node-postgres, specifically that the connection pooling implementation does not work well: https://github.com/bria... — committed to proofcarryingdata/zupass by robknight a year ago
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:
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:
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 newClient
outside of the pool.node pg
Client
andPool
instances extendEventEmitter
node event emitter docs state:
So if you don’t want your node process to exit - register an error handler. eg:
client.on('error', handleError)
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?