What is pg-pool?
The pg-pool npm package is a connection pool manager for PostgreSQL, built on top of the 'pg' library. It allows you to manage multiple database connections efficiently, providing features like connection pooling, transaction management, and error handling.
What are pg-pool's main functionalities?
Connection Pooling
This feature allows you to create a pool of connections to the PostgreSQL database. The pool manages the connections, reusing them for multiple queries to improve performance.
const { Pool } = require('pg');
const pool = new Pool({
user: 'dbuser',
host: 'database.server.com',
database: 'mydb',
password: 'secretpassword',
port: 5432,
});
pool.query('SELECT NOW()', (err, res) => {
console.log(err, res);
pool.end();
});
Transaction Management
This feature allows you to manage transactions, ensuring that a series of database operations either all succeed or all fail, maintaining data integrity.
const { Pool } = require('pg');
const pool = new Pool();
(async () => {
const client = await pool.connect();
try {
await client.query('BEGIN');
const res = await client.query('INSERT INTO users(name) VALUES($1) RETURNING id', ['brianc']);
const insertPhotoText = 'INSERT INTO photos(user_id, photo_url) VALUES ($1, $2)';
const insertPhotoValues = [res.rows[0].id, 's3.bucket.foo'];
await client.query(insertPhotoText, insertPhotoValues);
await client.query('COMMIT');
} catch (e) {
await client.query('ROLLBACK');
throw e;
} finally {
client.release();
}
})();
Error Handling
This feature provides robust error handling, allowing you to catch and handle errors that occur during query execution.
const { Pool } = require('pg');
const pool = new Pool();
pool.query('SELECT * FROM non_existent_table', (err, res) => {
if (err) {
console.error('Error executing query', err.stack);
} else {
console.log(res.rows);
}
pool.end();
});
Other packages similar to pg-pool
pg
The 'pg' package is the core PostgreSQL client for Node.js. It provides a simple interface for executing SQL queries and managing database connections. Unlike pg-pool, it does not include built-in connection pooling, but it can be used in conjunction with pg-pool for that purpose.
sequelize
Sequelize is a promise-based Node.js ORM for Postgres, MySQL, MariaDB, SQLite, and Microsoft SQL Server. It features solid transaction support, relations, eager and lazy loading, read replication, and more. While it provides connection pooling, it also offers a higher-level abstraction for database operations compared to pg-pool.
knex
Knex.js is a SQL query builder for PostgreSQL, MySQL, MariaDB, SQLite3, and Oracle. It features both traditional node-style callbacks as well as a promise interface for cleaner async flow. Knex.js includes built-in connection pooling and transaction management, similar to pg-pool, but also provides a more flexible query building experience.
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:
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()
}
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)
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 internally 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)
})
})
That means you can drop pg-pool into your app and 99% of the cases you wont even notice a difference. In fact, very soon I will be using pg-pool internally within node-postgres itself!
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()
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. Don't forget to 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.