pg-pool
A connection pool for node-postgres
install
npm i pg-pool pg
use
create
to use pg-pool you must first create an instance of a pool
const Pool = require('pg-pool')
const pool = new Pool()
const pool2 = new Pool({
database: 'postgres',
user: 'brianc',
password: 'secret!',
port: 5432,
ssl: true,
max: 20,
min: 4,
idleTimeoutMillis: 1000
})
const NativeClient = require('pg').native.Client
const nativePool = new Pool({ Client: NativeClient })
const PgNativeClient = require('pg-native')
const pgNativePool = new Pool({ Client: PgNativeClient })
acquire clients with a promise
pg-pool supports a fully promise-based api for acquiring clients
const pool = new Pool()
pool.connect().then(client => {
client.query('select $1::text as name', ['pg-pool']).then(res => {
client.release()
console.log('hello from', res.rows[0].name)
})
.catch(e => {
client.release()
console.error('query error', e.message, e.stack)
})
})
plays nice with async/await
this ends up looking much nicer if you're using co or async/await:
(async () => {
const pool = new Pool()
const client = await pool.connect()
try {
const result = await client.query('select $1::text as name', ['brianc'])
console.log('hello from', result.rows[0])
} finally {
client.release()
}
})().catch(e => console.error(e.message, e.stack))
co(function * () {
const client = yield pool.connect()
try {
const result = yield client.query('select $1::text as name', ['brianc'])
console.log('hello from', result.rows[0])
} finally {
client.release()
}
}).catch(e => console.error(e.message, e.stack))
your new favorite helper method
because its so common to just run a query and return the client to the pool afterward pg-pool has this built-in:
const pool = new Pool()
const time = await pool.query('SELECT NOW()')
const name = await pool.query('select $1::text as name', ['brianc'])
console.log(name.rows[0].name, 'says hello at', time.rows[0].name)
you can also use a callback here if you'd like:
const pool = new Pool()
pool.query('SELECT $1::text as name', ['brianc'], function (err, res) {
console.log(res.rows[0].name)
})
pro tip: unless you need to run a transaction (which requires a single client for multiple queries) or you
have some other edge case like streaming rows or using a cursor
you should almost always just use pool.query
. Its easy, it does the right thing :tm:, and wont ever forget to return
clients back to the pool after the query is done.
drop-in backwards compatible
pg-pool still and will always support the traditional callback api for acquiring a client. This is the exact API node-postgres has shipped with for years:
const pool = new Pool()
pool.connect((err, client, done) => {
if (err) return done(err)
client.query('SELECT $1::text as name', ['pg-pool'], (err, res) => {
done()
if (err) {
return console.error('query error', e.message, e.stack)
}
console.log('hello from', res.rows[0].name)
})
})
shut it down
When you are finished with the pool if all the clients are idle the pool will close them after config.idleTimeoutMillis
and your app
will shutdown gracefully. If you don't want to wait for the timeout you can end the pool as follows:
const pool = new Pool()
const client = await pool.connect()
console.log(await client.query('select now()'))
client.release()
await pool.end()
a note on instances
The pool should be a long-lived object in your application. Generally you'll want to instantiate one pool when your app starts up and use the same instance of the pool throughout the lifetime of your application. If you are frequently creating a new pool within your code you likely don't have your pool initialization code in the correct place. Example:
// assume this is a file in your program at ./your-app/lib/db.js
// correct usage: create the pool and let it live
// 'globally' here, controlling access to it through exported methods
const pool = new pg.Pool()
// this is the right way to export the query method
module.exports.query = (text, values) => {
console.log('query:', text, values)
return pool.query(text, values)
}
// this would be the WRONG way to export the connect method
module.exports.connect = () => {
// notice how we would be creating a pool instance here
// every time we called 'connect' to get a new client?
// that's a bad thing & results in creating an unbounded
// number of pools & therefore connections
const aPool = new pg.Pool()
return aPool.connect()
}
events
Every instance of a Pool
is an event emitter. These instances emit the following events:
error
Emitted whenever an idle client in the pool encounters an error. This is common when your PostgreSQL server shuts down, reboots, or a network partition otherwise causes it to become unavailable while your pool has connected clients.
Example:
var pg = require('pg')
var pool = new pg.Pool()
pool.on('error', function(error, client) {
})
connect
Fired whenever the pool creates a new pg.Client
instance and successfully connects it to the backend.
Example:
var pg = require('pg')
var pool = new pg.Pool()
var count = 0
pool.on('connect', client => {
client.count = count++
})
pool
.connect()
.then(client => {
return client
.query('SELECT $1::int AS "clientCount', [client.count])
.then(res => console.log(res.rows[0].clientCount))
.then(() => client)
}))
.then(client => client.release())
This allows you to do custom bootstrapping and manipulation of clients after they have been successfully connected to the PostgreSQL backend, but before any queries have been issued.
environment variables
pg-pool & node-postgres support some of the same environment variables as psql
supports. The most common are:
PGDATABASE=my_db
PGUSER=username
PGPASSWORD="my awesome password"
PGPORT=5432
PGSSLMODE=require
Usually I will export these into my local environment via a .env
file with environment settings or export them in ~/.bash_profile
or something similar. This way I get configurability which works with both the postgres suite of tools (psql
, pg_dump
, pg_restore
) and node, I can vary the environment variables locally and in production, and it supports the concept of a 12-factor app out of the box.
tests
To run tests clone the repo, npm i
in the working dir, and then run npm test
contributions
I love contributions. Please make sure they have tests, and submit a PR. If you're not sure if the issue is worth it or will be accepted it never hurts to open an issue to begin the conversation. If you're interested in keeping up with node-postgres releated stuff, you can follow me on twitter at @briancarlson - I generally announce any noteworthy updates there.
license
The MIT License (MIT)
Copyright (c) 2016 Brian M. Carlson
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.