title: Node Postgres - release clients in a pool before closing pool
body: <p>I am trying to gracefully stop my postgres db on <code>process.on('SIGINT', handler)</code> in my main index.js.</p>
  
  <p>With Node Postgres, I am using a pool with the 4 clients. After reading the docs titled <a href="https://github.com/brianc/node-pg-pool#shut-it-down" rel="nofollow noreferrer">shut it down</a> for closing a pool, I need to first release the clients. </p>
  
  <p>From the exported <code>pool</code> itself(<code>module.exports.pool = pool;</code>)
  , 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.</p>
  
  <pre><code>const colors = require('colors');
  const pg = require('pg');
  
  if (process.env.DOLPHIN_TEST) {
    var dbName = 'test_dolphin_db';
    var maxCon = 1
  } else {
    var dbName = 'dev_dolphin_db';
    var maxCon = 4
  }
  
  const config = {
    user: 'postgres',
    database: dbName,
    host: 'localhost',
    max: maxCon,
    idleTimeoutMillis: 30000
  }
  
  const pool = new pg.Pool(config);
  
  function poolConnected(err, client, done, resolve) {
    if(err) {
      return promiseArgs.reject(`error fetching client from pool ${err}`);
    }
    client.query('SELECT $1::int AS number', ['1'], function(err, result) {
      //call `done()` to release the client back to the pool
      done();
  
      if(err) {
        return console.error('error running query', err);
      }
  
      console.log(colors.grey(
        `Postgres is online using ${client.database} as user ${client.user}\n` + 
        `and is listening on ${client.host}`));
      promiseArgs.resolve();
    });
  }
  
  var promiseArgs = { resolve: undefined, reject: undefined };  
  
  function initDb() {
    var promise = new Promise(function(resolve, reject) {
      promiseArgs.resolve = resolve;
      promiseArgs.reject = reject;
  
      pool.connect(poolConnected);
  
      pool.on('error', function (err, client) {
        // if an error is encountered by a client while it sits idle in the pool
        // the pool itself will emit an error event with both the error and
        // the client which emitted the original error
        // this is a rare occurrence but can happen if there is a network partition
        // between your application and the database, the database restarts, etc.
        // and so you might want to handle it and at least log it out
        console.error('idle client error', err.message, err.stack)
      });
    });
    return promise;
  }
  
  module.exports.pool = pool;
  module.exports.initDb = initDb;
  </code></pre>