Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

postgres-pool

postgres-pool14.2kMIT10.1.29TypeScript support: included

Node postgres connection pool implementation for node-pg

node, postgres, pg, pool, connection, pooling

readme

postgres-pool

NPM version node version Known Vulnerabilities

Connection pool implementation for pg. Compatible with pg-pool options and syntax.

Why?

Getting Started

import { Pool } from 'postgres-pool';

const pool = new Pool({
  connectionString: 'postgres://username:pwd@127.0.0.1/db_name',
});

const userId = 42;
const results = await pool.query('SELECT * from "users" where id=$1', [userId]);

console.log('user:', results.rows[0]);

Using named parameters in the query

import { Pool } from 'postgres-pool';

const pool = new Pool({
  connectionString: 'postgres://username:pwd@127.0.0.1/db_name',
});

const userId = 42;
const results = await pool.query('SELECT * from "users" where id=@id', {
  id: userId,
});

console.log('user:', results.rows[0]);
import { Pool } from 'postgres-pool';

const pool = new Pool({
  connectionString: 'postgres://username:pwd@127.0.0.1/db_name',
});

const userId = 42;
const connection = await pool.connect();
try {
  const results = await connection.query('SELECT * from "users" where id=$1', [userId]);
  console.log('user:', results.rows[0]);
} finally {
  // NOTE: You MUST call connection.release() to return the connection back to the pool
  await connection.release();
}

Handle errors from connections in the pool

import { Pool } from 'postgres-pool';

const pool = new Pool({
  connectionString: 'postgres://username:pwd@127.0.0.1/db_name',
});

pool.on('error', (err) => {
  console.error('Unexpected error on idle client', err);
  process.exit(-1);
});

Graceful shutdown

import { Pool } from 'postgres-pool';

const pool = new Pool({
  connectionString: 'postgres://username:pwd@127.0.0.1/db_name',
});

await pool.end();

Explicit connection details instead of a connection string

import { Pool } from 'postgres-pool';

const pool = new Pool({
  host: '127.0.0.1',
  database: 'db_name',
  user: 'foo',
  password: 'bar',
  port: 1234,
});

AWS RDS specific TLS settings for connections

Setting ssl='aws-rds' will:

  • configure the AWS root certificate
  • reject any connection which is not authorized with the list of supplied CAs.
  • attempt to use TLSv1.2 as the minimum TLS version.

It is the same as:

import { Pool } from 'postgres-pool';

const pool = new Pool({
  ssl: {
    rejectUnauthorized: true,
    ca: fs.readFileSync('./certs/rds-global-bundle.pem'),
    minVersion: 'TLSv1.2',
  },
});
import { Pool } from 'postgres-pool';

const pool = new Pool({
  connectionString: 'postgres://username:pwd@127.0.0.1/db_name',
  ssl: 'aws-rds',
});

TLS details for a connection

import { Pool } from 'postgres-pool';

const pool = new Pool({
  host: '127.0.0.1',
  database: 'db_name',
  user: 'foo',
  password: 'bar',
  port: 1234,
  ssl: {
    rejectUnauthorized: false,
    ca: fs.readFileSync('/path/to/server-certificates/root.crt').toString(),
    key: fs.readFileSync('/path/to/client-key/postgresql.key').toString(),
    cert: fs.readFileSync('/path/to/client-certificates/postgresql.crt').toString(),
  },
});

Change size of the pool

import { Pool } from 'postgres-pool';

const pool = new Pool({
  connectionString: 'postgres://username:pwd@127.0.0.1/db_name',
  poolSize: 10, // Default is 10 connections
});

Change retry on error settings

import { Pool } from 'postgres-pool';

const pool = new Pool({
  connectionString: 'postgres://username:pwd@127.0.0.1/db_name',
  // Number of retries to attempt when there's an error matching `retryConnectionErrorCodes`. A value of 0 will disable connection retry.
  retryConnectionMaxRetries: 5,
  // Milliseconds to wait between retry connection attempts after receiving a connection error with code that matches `retryConnectionErrorCodes`. A value of 0 will try reconnecting immediately.
  retryConnectionWaitMillis: 100,
  // Error codes to trigger a connection retry.
  retryConnectionErrorCodes: ['ENOTFOUND', 'EAI_AGAIN'],
});

Change timeout thresholds

import { Pool } from 'postgres-pool';

const pool = new Pool({
  connectionString: 'postgres://username:pwd@127.0.0.1/db_name',
  // Time to keep a connection idle. Default is 10s
  idleTimeoutMillis: 10000,
  // Time to wait to obtain a connection from the pool. Default is 90s
  waitForAvailableConnectionTimeoutMillis: 90000,
  // Max time to connect to postgres. Default is 5s
  connectionTimeoutMillis: 5000,
});

Handle cluster failover gracefully

When a cluster has a failover event, promoting a read-replica to master, there can be a couple sets of errors that happen with already established connections in the pool as well as new connections before the cluster is available in a ready state.

By default, when making a new postgres connection and the server throws an error with a message like: the database system is starting up, the postgres-pool library will attempt to reconnect (with no delay between attempts) for a maximum of 90s.

Similarly, if a non-readonly query (create/update/delete/etc) is executed on a readonly connection, the server will throw an error with a message like: cannot execute UPDATE in a read-only transaction. This can occur when a connection to a db cluster is established and the cluster fails over before the connection is terminated, thus the connected server becomes a read-replica instead of the expected master. The postgres-pool library will attempt to reconnect (with no delay between attempts) for a maximum of 90s and will try to execute the query on the new connection.

Defaults can be overridden and this behavior can be disabled entirely by specifying different values for the pool options below:

import { Pool } from 'postgres-pool';

const pool = new Pool({
  connectionString: 'postgres://username:pwd@127.0.0.1/db_name',
  // Enable/disable reconnecting on "the database system is starting up" errors
  reconnectOnDatabaseIsStartingError: true,
  // Milliseconds to wait between retry connection attempts while the database is starting up
  waitForDatabaseStartupMillis: 0,
  // If connection attempts continually return "the database system is starting up", this is the total number of milliseconds to wait until an error is thrown.
  databaseStartupTimeoutMillis: 90000,
  // If the query should be retried when the database throws "cannot execute X in a read-only transaction"
  reconnectOnReadOnlyTransactionError: true,
  // Milliseconds to wait between retry queries while the connection is marked as read-only
  waitForReconnectReadOnlyTransactionMillis: 0,
  // If queries continually return "cannot execute X in a read-only transaction", this is the total number of milliseconds to wait until an error is thrown
  readOnlyTransactionReconnectTimeoutMillis: 90000,
  // If the query should be retried when the database throws "Client has encountered a connection error and is not queryable"
  reconnectOnConnectionError: true,
  // Milliseconds to wait between retry queries after receiving a connection error
  waitForReconnectConnectionMillis: 0,
  // If queries continually return "Client has encountered a connection error and is not queryable", this is the total number of milliseconds to wait until an error is thrown
  connectionReconnectTimeoutMillis: 90000,
});

Compatibility

  • Node.js v16 or above

License

MIT

changelog

10.1.29 (2025-09-22)

10.1.28 (2025-09-15)

10.1.27 (2025-09-10)

Bug Fixes

10.1.26 (2025-09-09)

10.1.25 (2025-09-08)

Bug Fixes

10.1.24 (2025-09-08)

10.1.23 (2025-08-25)

10.1.22 (2025-08-18)

10.1.21 (2025-08-11)

10.1.20 (2025-08-04)

10.1.19 (2025-07-28)

10.1.18 (2025-07-21)

10.1.17 (2025-07-14)

10.1.16 (2025-07-14)

10.1.15 (2025-07-07)

10.1.14 (2025-07-07)

10.1.13 (2025-07-07)

10.1.12 (2025-06-30)

10.1.11 (2025-06-30)

Bug Fixes

10.1.10 (2025-06-30)

10.1.9 (2025-06-26)

10.1.8 (2025-06-26)

Bug Fixes

10.1.7 (2025-06-09)

Bug Fixes

  • deps: update dependency @types/pg to v8.15.3 (#120) (8d68baa)
  • deps: update dependency @types/pg to v8.15.4 (#121) (3d8c25a)

10.1.6 (2025-05-29)

Bug Fixes

  • deps: update dependency @types/pg to v8.15.2 (#108) (c44177d)

10.1.5 (2025-05-12)

Bug Fixes

  • deps: update all dependencies (#100) (06925f7)
  • deps: update all dependencies (#104) (cc3a11f)
  • deps: update dependency @types/pg to v8.15.0 (#103) (20618cb)
  • Update type definitions for pg PoolClient and for chai should assertions (#102) (bb81dca)

10.1.4 (2025-05-05)

Bug Fixes

  • deps: update all dependencies (#93) (f284c9a)
  • deps: update dependency @types/pg to v8.11.14 (#92) (4cbb186)

10.1.3 (2025-04-23)

Bug Fixes

10.1.2 (2025-04-22)

Bug Fixes

  • deps: update dependency pg to v8.15.1 (#89) (32d6441)

10.1.1 (2025-04-15)

Bug Fixes

  • deps: update all dependencies (#81) (a974ff6)
  • deps: update dependency @types/pg to v8.11.13 (#84) (2d7dd11)
  • optional scope for semantic release (#86) (9fac2a1)

10.1.0 (2025-04-07)

Bug Fixes

  • deps: pin dependencies (#75) (47785ab)
  • Fix lint issues with nullish coalescing operator (22bd45b)

Features

10.0.1 - 2024-03-11

  • Fix loading aws certs for ESM

10.0.0 - 2024-03-10

  • Drop support for Node.js 18
  • Update npms

9.0.6 - 2024-01-08

  • Update npms

9.0.5 - 2024-12-17

  • Update npms
  • Use node protocol for built-in modules

9.0.4 - 2024-11-29

  • Update npms

9.0.3 - 2024-09-30

  • Update npms

9.0.2 - 2024-08-26

  • Update npms

9.0.1 - 2024-07-02

  • Include certs in npm package

9.0.0 - 2024-07-02

  • Publish as CJS and ESM
  • Update npms

BREAKING CHANGES

  • ErrorWithCode renamed to PostgresPoolError

8.1.6 - 2024-05-13

  • Update npms

8.1.5 - 2024-04-08

  • Update npms

8.1.4 - 2024-03-11

  • Update npms

8.1.3 - 2024-02-07

  • Update npms

8.1.2 - 2024-01-05

  • Update npms

8.1.1 - 2023-11-28

  • Update npms

8.1.0 - 2023-10-27

  • Update npms
  • Update AWS TLS to use the updated global bundle for all regions.

    NOTE: certs/rds-ca-2019-root.pem is deprecated and will be removed in a future release.

8.0.0 - 2023-10-03

BREAKING CHANGES (8.0.0)

  • Drop support for Node.js 16

NON-BREAKING CHANGES (8.0.0)

  • Add drainIdleConnections(). Fix #67
  • Update npms

7.0.2 - 2023-07-18

  • Update npms

7.0.1 - 2023-04-17

  • Update npms

7.0.0 - 2023-02-07

BREAKING CHANGES (7.0.0)

  • Drop support for Node.js 14
  • Change connection timeout default from 30s to 5s

BUG FIXES (7.0.0)

  • Fix retry connection on timeout

MAINTENANCE (7.0.0)

  • Update npms

6.0.8 - 2023-01-13

  • Update npms

6.0.7 - 2022-12-06

  • Update npms

6.0.6 - 2022-11-03

  • Update npms

6.0.5 - 2022-10-11

  • Update npms

6.0.4 - 2022-08-30

  • Update npms

6.0.3 - 2022-06-29

  • Update npms

6.0.2 - 2022-06-15

  • Remove lodash dependency. Fix #63 Thanks @furiozo-ga!
  • Update npms

6.0.1 - 2022-06-05

  • Fix query with object. Fix #62 Thanks @furiozo-ga!
  • Update npms

6.0.0 - 2022-05-23

  • Drop support for Node.js 12
  • Update npms

5.0.15 - 2022-04-13

  • Update npms

5.0.14 - 2022-03-22

  • Update npms

5.0.13 - 2022-03-15

  • Update npms

5.0.12 - 2022-03-02

  • Restore any in query() definition

5.0.11 - 2022-03-02

  • Fix query incorrectly throwing error with empty parameter object. Thanks @tyler-neal!
  • Update npms

5.0.10 - 2022-02-22

  • Update npms

5.0.9 - 2021-12-27

  • Update npms

5.0.8 - 2021-11-16

  • Update npms

5.0.7 - 2021-10-29

  • Update npms
  • Format markdown files

5.0.6 - 2021-09-14

  • Update npms
  • Lint markdown files

5.0.5

  • Update npms
  • Set sourceRoot when transpiling Typescript, to help with sourcemap paths

5.0.4

  • Update npms

5.0.3

  • Update npms

5.0.2

5.0.1

  • Update npms
  • Add Node.js v16 to CI tests

5.0.0

  • Drop support for Node.js 10
  • Add parameters to connectionAddedToPool event

4.0.0

  • Make end() return a promise. #57 Thanks @Kristof-Mattei!
  • Add package-lock.json. #58 Thanks @Kristof-Mattei!

3.2.8

  • Fix end() not correctly closing multiple idle connections. Thanks @Kristof-Mattei!
  • Update npms

3.2.7

  • Update npms

3.2.6

  • Update npms

3.2.5

  • Update npms

3.2.4

  • Update npms

3.2.3

  • Update npms

3.2.2

  • Update npms
  • Add EAI_AGAIN as error code to trigger retry
  • For ssl: 'aws-rds', set ca to buffer instead of converting to string

3.2.1

  • Format code with prettier

3.2.0

  • Add options to retry connection on error (eg. ENOTFOUND)
  • Update npms

3.1.4

  • Update npms

3.1.3

  • Update npms

3.1.2

  • Update npms

3.1.1

  • Update Typescript npm

3.1.0

  • Update npms

3.0.0

  • Update AWS TLS to use rds-ca-2019-root.pem root certificate

2.1.1

  • Update npms

2.1.0

  • Add settings for connection errors during queries and ability to reconnect:
    • Pool options:
      • reconnectOnConnectionError - true
      • waitForReconnectConnectionMillis - 0ms
      • connectionReconnectTimeoutMillis - 90,000ms
    • Event: queryDeniedForConnectionError
  • Update npms

2.0.5

  • Fix a pool connection not being released when connect timed out
  • Include list of differences from pg-pool in README
  • Update npms

2.0.4

  • Support query_timeout and statement_timeout pg.Client config settings

2.0.3

  • Update npms
  • Make typescript lint rules more strict

2.0.2

  • Add ssl typings to pool constructor options
  • Add 'aws-rds' ssl shorthand for secure RDS TLS settings

2.0.1

2.0.0

  • Drop node 8 support
  • Update npms
  • Clear pool connection timeout timer immediately after getting connection from pool
  • Clear connection timeout timer immediately after successful connect()
  • Try to destroy socket connection before calling client.end() on connection error

1.4.1

  • Add wildcards to dependency versions

1.4.0

  • Updated to typescript 3.5
  • Updated npms
  • Include type definitions as "dependencies" instead of "devDependencies"

1.3.0

  • Updated to typescript 3.4
  • Updated dependencies
  • Added support for named parameters (namedParameterFindRegExp, getNamedParameterReplaceRegExp, and getNamedParameterName)

    Notes:

    • Named parameters are only used if an object is passed as the value parameter to the query function

      myPool.query('select * from foobar where id=@id', { id: '123' });
    • By default, the named parameter syntax is @parameterName.

1.2.0

  • Add reconnectOnDatabaseIsStartingError (default: true), waitForDatabaseStartupMillis (default: 0), and databaseStartupTimeoutMillis (default: 90000) options for connections in pool
  • Default reconnectOnReadOnlyTransactionError to true
  • Add waitForReconnectReadOnlyTransactionMillis (default: 0) and readOnlyTransactionReconnectTimeoutMillis (default: 90000)
  • Fix unhandled error when error was thrown while attempting to end a db connection

1.1.0

  • Add reconnectOnReadOnlyTransactionError option for connections in pool

1.0.7

  • Restore firing connectionRemovedFromPool event when _removeConnection() is called

1.0.6

  • Fix connectionRemovedFromPool to only fire when a valid connection is removed from the pool, not each time _removeConnection() is called.
  • Add event: connectionRemovedFromIdlePool

1.0.5

  • Rebuild with lint updates
  • Fix query test to correctly stub _createConnection()

1.0.4

  • Strongly type pool events
  • Add events: connectionAddedToPool, connectionRemovedFromPool, connectionIdle, idleConnectionActivated, connectionRequestQueued, connectionRequestDequeued
  • Cleanup internal connection event listeners

1.0.3

  • Fix creating one extra connection over poolSize

1.0.2

  • Fix opening too many connections during many concurrent requests
  • Reduce memory usage

1.0.1

  • Call removeConnection() when ending pool

1.0.0

  • Initial release