Closed dman777 closed 7 years ago
pool.end();
No, documentation states to release clients in the pool first. So, I need a way to access the clients from the pool to release them.
You release clients into the pool, not from the pool. If you’re calling done()
correctly after acquiring any client as you should, pool.end()
will be enough. To make doing that easier, consider making use of pg-pool’s conveniences, like query
and the promise interface. Here’s a simplification of your script following that:
'use strict';
const colors = require('colors');
const pg = require('pg');
const [dbName, maxCon] =
process.env.DOLPHIN_TEST ?
['test_dolphin_db', 1] :
['dev_dolphin_db', 4];
const config = {
user: 'postgres',
database: dbName,
host: 'localhost',
max: maxCon,
idleTimeoutMillis: 30000,
};
const pool = new pg.Pool(config)
.on('error', err => {
console.error('idle client error', err.message, err.stack);
});
async function initDb() {
await pool.query('SELECT $1::int AS number', [1]);
console.log(colors.grey(
`Postgres is online using ${client.database} as user ${client.user}\n` +
`and is listening on ${client.host}`));
}
exports.pool = pool;
exports.initDb = initDb;
Thanks for the help on this @charmander - closing since it seems to just be a question which now has a nice answer. 😄
I am trying to gracefully stop my postgres db on process.on('SIGINT', handler) in my main index.js.
With Node Postgres, I am using a pool with the 4 clients. After reading the docs titled shut it down for closing a pool, I need to first release the clients.
From the exported pool itself(module.exports.pool = pool;) , how do I access the clients in a pool to do this?
This is how I initiate my postgres db, create the pool, and export it to other modules.