Skip to content
Agents tracked: 506 Releases (7d): 634 Agent status: 4 with issuesSince last limit reset: Claude 15d · Codex 0d For AI agents: Markdown Updated Nov 26, 2026

pg

PostgreSQL client for Node.js with pure JavaScript and libpq API compatibility.

Databases, ORMs and clientsNPM

GitHubWebsite

1. License

License from npm.

github.com/brianc/node-postgres

From the package registry and GitHub, checked daily. Tap a verdict to see its source.How we checkedReport an error

What pg does

pg is a deliberately thin PostgreSQL client that ships a pure-JavaScript driver and optional native libpq bindings behind the same API, and leaves the higher-level abstractions to you.

pg popularity

81 / 100
Gid Score, #81 overall
106M
downloads a week, npm
13.2K
GitHub stars
298
agent projects use it
pg: downloads per weeknpm, pg 106,311,497
pg: GitHub starsbrianc/node-postgres 13,242

Gid Score measures public popularity and activity, not quality. How the Gid Score is calculated

How pg is used

  • pg is a dependency in 298 of the 6,258 public GitHub projects set up for AI coding agents that we scanned (6.1% of the 4,867 JavaScript/TypeScript ones), #1 of 120 in databases, orms and clients.
  • Counting the projects our scan samples by dependency (Solana 3,067, Ethereum and EVM 1,774, 3D and games 3,130, agent frameworks and SDKs 13,750), it appears in 878; those are left out of every share on this page.
  • The most used alternative in our scan is Drizzle ORM (253 projects).
  • Projects that use it most often also use dotenv (62.1%), tsx (52.7%), Next.js (53.4%).
  • It is more common in projects set up for Devin Desktop (8.2%) than for GitHub Copilot (5.0%).

By coding agent

173 projects, 5.3%
153 projects, 5.0%
130 projects, 5.3%
33 projects, 7.0%
26 projects, 5.5%
23 projects, 8.2%
20 projects, 5.8%
17 projects, 5.1%

Often used with

In 62.1% of its projects
In 52.7% of its projects
In 53.4% of its projects
In 62.8% of its projects
In 55.7% of its projects
In 91.6% of its projects
In 69.5% of its projects
In 69.1% of its projects

pg in practice

Strengths

Parameters travel apart from the SQL

The FAQ calls parameterized query support "first class". Values are sent to the server separately from the query text. Its example passes <code>"'; DROP TABLE user;"</code> as a parameter and states that this will not inject SQL.

JavaScript or libpq, same calls

You can run the pure-JavaScript client or the optional libpq bindings. The maintainer writes that "they both work equally" and have full feature parity, which he credits to "the extensive overlapping test suite".

PostgreSQL-specific features exposed

Beyond plain queries, the README lists named statements with query plan caching, <code>LISTEN/NOTIFY</code> notifications, and bulk import and export with <code>COPY</code>.

Old servers and several runtimes

The docs say pg supports every PostgreSQL version from 8.x to the latest. They say it runs on current and LTS Node releases, and on bun, deno and Cloudflare Workers.

Watch out for

Few abstractions on purpose

Higher-level helpers are not in the core. The README points to a wiki list of extra modules "to complete the picture."

The pool won't end your transaction

According to the FAQ, nothing happens to an open transaction when a client goes back to the pool. You must issue <code>COMMIT</code> or <code>ROLLBACK</code> yourself.

pg-native on Windows needs a local PostgreSQL

The FAQ documents <code>npm install pg-native</code> failing on Windows while trying to load <code>binding.gyp</code>. The fix is to install PostgreSQL and add its bin directory, where <code>pg_config</code> lives, to PATH.

Connect, run a parameterized query, and clean up

import { Client } from 'pg'
const client = await new Client (). connect ()
try {
  const res = await client. query ( 'SELECT $1::text as message' , [ 'Hello world!' ])
  console. log (res.rows[ 0 ].message) // Hello world!
} catch (err) {
  console. error (err)
} finally {
  await client. end ()
}

More about pg

Funding
  • GitHub Sponsors: https://github.com/brianc
  • 17 GitHub sponsors

See all

Head to head
Packages around it
  • dotenv: Loads .env config
  • zod: Schema validation
  • tsx: Runs TypeScript files
  • class-variance-authority: Component style variants
  • lucide-react: React icons
  • tailwind-merge: Merges Tailwind classes
More in Databases, ORMs and clients

Alternatives

pg This page

MITGid 81106M/wk

Drizzle ORM

TypeScript ORM for SQL databases with type safety and zero-runtime overhead.

Apache-2.0Gid 8649.6M/wk

Prisma

Modern ORM with intuitive schema, migrations, and type-safe database queries.

Apache-2.0Gid 6426M/wk

Neon Serverless Driver

PostgreSQL driver optimized for serverless environments from Neon.

MITGid 608.8M/wk

mysql2

Fast MySQL driver with native protocol implementation, prepared statements and SSL.

MITGid 6625.6M/wk

sqlite3

Asynchronous, non-blocking SQLite3 database bindings for Node.js.

BSD-3-ClauseGid 582.9M/wk

Compare pg with another tool

Get started

Install it.

npm install pg

Rules for your agent

Paste into AGENTS.md or CLAUDE.md
## pg (node-postgres)
- Pass values as parameters (`$1`, `$2`, ...) in the second argument of `query()`. The FAQ says parameters are sent separately from the SQL.
- If you must build an `IN (...)` list, generate only the `$n` placeholders and pass the values as the parameter array, as the FAQ example does.
- Use a `Pool` in most applications. The docs say "In most applications you’ll want to use a connection pool".
- After a transaction on a pooled client, run `COMMIT` or `ROLLBACK` before releasing it. The pool does nothing to open transactions.
- Wrap queries in `try/catch` and call `client.end()` in `finally`. The docs' short examples assume success.
- To match a list of values, pass a JavaScript array to `= ANY ($1)`.
- A named prepared statement is parsed once. Later calls with the same name reuse it regardless of the query text, so don't reuse a name for different SQL.