# pg — card for AI agents

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

Category: Databases, ORMs and clients · npm package `pg` · latest version 8.23.1 (checked 2026-11-01)
Install: `npm install pg`
License: MIT (permissive)

In your own product (generator, app, hosted service): **yes**. Permissive license: ship it inside your own product, closed or open source, and keep its license and copyright notice.

## Rules for AI agents (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.

## What it is
- pg is the core module of node-postgres, a PostgreSQL client for Node.js. The README describes it as a "Non-blocking PostgreSQL client for Node.js (and bun, deno, cloudflare, etc...)". The GitHub repository was created on 2010-10-15. It is a monorepo that also holds `pg-pool`, `pg-native`, `pg-cursor`, `pg-query-stream` and a few other modules.
- You write SQL strings and pass values as separate parameters. Rows come back as JavaScript objects on `result.rows`. You can open a single `Client` or use a `Pool`. The docs list support for callbacks, promises, async/await, prepared statements, cursors and streaming results.
- It fits developers who want to send SQL to PostgreSQL themselves. The README says the library is "by design pretty light on abstractions".

## Caveats
- 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 `COMMIT` or `ROLLBACK` yourself.
- pg-native on Windows needs a local PostgreSQL: The FAQ documents `npm install pg-native` failing on Windows while trying to load `binding.gyp`. The fix is to install PostgreSQL and add its bin directory, where `pg_config` lives, to PATH.
- Doc examples skip error handling: The docs say most of their snippets assume the methods succeed. Real code needs its own error handling.
- Complex arrays fall back to dynamic SQL: For arrays the driver isn't coping with, such as arrays of composite types, the FAQ says you can generate an array literal with dynamic SQL. It warns you to "be extremely careful of SQL injection" when you do.

## Numbers (AgentGid data)
- Gid Score: 80.9 (#81 of all tools, 3 of 120 in its category)
- Used in 298 public projects set up for AI coding agents (4.8%)
- Downloads last week: 106,311,497 (as of 2026-11-24), +55.4% over 4 weeks
- GitHub stars: 13,242 (as of 2026-11-24)
- Last push: 2026-10-23

## Often used with
dotenv 62.1%, tsx 52.7%, Next.js 53.4%, Zod 62.8%, Lucide React 55.7%, TypeScript 91.6%

## Checked alternatives (same job)
- Drizzle ORM: https://agentgid.com/tools/drizzle-orm/agent.md
- Prisma: https://agentgid.com/tools/prisma/agent.md
- Neon Serverless Driver: https://agentgid.com/tools/neondatabase/agent.md
- mysql2: https://agentgid.com/tools/mysql2/agent.md
- sqlite3: https://agentgid.com/tools/sqlite3/agent.md

## Links
- AgentGid page: https://agentgid.com/tools/pg/
- Homepage: https://github.com/brianc/node-postgres
- Repository: https://github.com/brianc/node-postgres
- Source: https://github.com/brianc/node-postgres#readme
- Source: https://github.com/brianc/node-postgres
- Source: https://www.npmjs.com/package/pg
- Source: https://node-postgres.com
- Source: https://github.com/brianc/node-postgres/wiki/FAQ

---
AgentGid's catalog is curated: every entry was checked against the maker's official pages (registry, repository, license and pricing pages) on the date shown; a license value shows the quote it stands on wherever AgentGid has one. Usage numbers come from AgentGid's own scan of public repositories set up for AI coding agents. Claims by makers are reported, not tested by AgentGid.
