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.
I guess you're not waiting for db to connect and making requests to early.
Using native mode fixed problem for client query
const { Pool, Client } = require('pg').native
@JohnsiJohn oh, that's interesting! Thank you for that discover, I will try to use it for sequelize
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.
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
dbobject is valid before I use it, and reconnect to the DB first if not.
I've just had a hell of a time debugging this from the NestJS TypeORM package, and came to the same conclusion, pg-native skirts around this problem entirely
@Upperfoot and how did you solve this setup in NestJS TypeORM?
@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 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:
docker stopevents.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?
@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 mypackage.jsonand 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?
@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.
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)
Most helpful comment
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
dbobject is valid before I use it, and reconnect to the DB first if not.