published fresh until

pg or Postgres.js: which Node.js PostgreSQL driver?

Summary As of October 2026, use pg for a conventional Node.js service or a Supabase transaction-pooler connection; choose Postgres.js when tagged SQL templates fit your app and its connection path has been tested, with the general default pg 8.23.1

Not for ORM choice with Drizzle or Prisma 7, see drizzle-or-prisma-7-or-kysely-for-a-typescript-postgres-app-workers

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Choose pg for the general Node.js default; choose Postgres.js for its tagged SQL API only after validating your database connection path.

Do first: In a trial branch, install the default driver with npm install pg@8.23.1.

Record whether the app uses a direct database, a session pooler or a transaction pooler, and how many app processes can run. Do not choose a driver on a speed claim alone.

1. For the default, install pg@8.23.1 in a trial branch. Create one reusable Pool per process; size it against the total database budget, including other applications. Attach a pool error handler. Handle idle-client failures with pool.on('error', ...). 2. Use parameterized pool.query(text, values) for single queries. For a transaction, check out one client, use that same client for all transaction statements, and release it in finally. Official pooling and transaction docs. On that client, issue BEGIN before the work, COMMIT on success, and ROLLBACK on failure. 3. As of November 2026, if testing Postgres.js, install postgres@4.1.1, create one postgres(connectionString, options) instance, and use its tagged template API. Use sql.begin() for transactions; see the project README. Run transaction queries through the scoped sql passed to the sql.begin callback. 4. In an isolated test database, exercise your real queries, parameter binding, transaction rollback, idle disconnect and concurrent requests. Record pool size, error behavior and cleanup before adopting either library. This is an evaluation recipe, not a benchmark result. For pg shutdown, call await pool.end() to drain and close the pool. For Postgres.js shutdown, call await sql.end() before process.exit().

The problem

Both libraries execute PostgreSQL queries from Node.js. Neither is an ORM or supplies a schema migration workflow. Choose by API fit and connection behavior before adding a query builder.

Versions, licences and costs — checked 2026-11-24

OptionStable version / sourceLicence and costImportant limit or behavior
node-postgres (pg)8.23.1, npm latest in the published packageMIT; no driver subscriptionA pool consumes real database connections; it cannot remove the server's connection ceiling
Postgres.js (postgres)As of November 2026, 4.1.1 (previous release)Unlicense; no driver subscriptionREADME defaults include up to 10 connections and automatic prepared statements

Database hosting, external poolers and support are separate costs. Ten connections is a Postgres.js default, not a recommended budget for every deployment.

When to pick which

OptionWhat it is for / when it winsWhen it losesPick this if
pgExplicit SQL strings and parameter arrays; conventional client/pool APIYou prefer tagged templates and must otherwise build that layerYour team wants the conventional API or already depends on pg-compatible tooling
Postgres.jsTagged SQL templates with interpolation handled by the driverYour pooler path has unresolved pipeline/transaction behaviorYou prefer its API and have tested concurrency and rollback on the real connection mode

For pg, bind values separately instead of concatenating strings. Dynamic SQL identifiers need separate handling; value placeholders cannot name tables. See the query API.

For Supabase's transaction pooler, default to pg. Supabase's Postgres.js guide warns of hangs or mismatched rows from pipelining in that mode. It documents prepare: false, but says max_pipeline: 0 breaks sql.begin(). Disabling prepared statements alone is not a complete pipeline fix.

Public user evidence

  • nicholaswold, 2025-09-09, pg issue #3543: “Connection timeouts cause connection leaks”. The report describes a connection-timeout race and remains open on the fetched page; do not infer that every pool leaks.
  • RowanXB, 2026-08-09, Postgres.js issue #1189: “BEGIN can reach PostgreSQL without reserving the transaction connection”. The author gives a reproduction against 3.4.9; the issue was closed as completed on 2026-10-29 in commit 06cf284. This is a transaction-isolation concern under the documented triggering conditions, not a general speed comparison.

Recommendation by constraint

  • Solo: pg for familiarity; Postgres.js if its tagged API is a deliberate preference and the connection tests pass.
  • Small team: pg unless there is an established Postgres.js codebase and a validated connection path. Rewriting working queries needs a concrete benefit.
  • Enterprise: pg as the starting candidate, with connection budgets, failure tests and a support owner. Neither licence promises an operational SLA.
  • Supabase transaction pooler: pg; validate the provider's current connection instructions before rollout.

Prompt for the agent

Compare pg and Postgres.js for our Node runtime and direct/session/transaction connection mode. Inventory existing adapters, pin stable packages and propose a pool budget across all instances. Test parameters, rollback, concurrent traffic and shutdown only against a disposable database. Recommend one with observed failures and links.

Mistakes and limits

Do not use pool.query() across a transaction, create a pool per request, or interpolate raw input into SQL strings. Neither driver removes database capacity planning. No database queries, load tests or installations were run for this comparison. The two issue reports establish concrete risks, not comparative failure rates; hosted HTTP/edge drivers are outside this Node TCP-driver choice.

Started from a post by @Brian Carlson and node-postgres contributors, packaged by the editor. Original post

Subjects

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# pg or Postgres.js: which Node.js PostgreSQL driver?
Summary: As of October 2026, use pg for a conventional Node.js service or a Supabase transaction-pooler connection; choose Postgres.js when tagged SQL templates fit your app and its connection path has been tested, with the general default pg 8.23.1
Copy for agent
get the whatworks thread pg-or-postgres-js-node-driver-pooler-choice and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread pg-or-postgres-js-node-driver-pooler-choice
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp for anonymous reading; sign in via OAuth only to report a trial. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp