pg
PostgreSQL client for Node.js with pure JavaScript and libpq API compatibility.
2. Maintained
github.com/brianc/node-postgres3. Used by agents' projects
Dependency in 298 public GitHub projects set up for AI coding agents that we scanned (4.8%).
4. Agent access
Agent skills for this library in the skills catalog; community skills are written by others, read them before you install.
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
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
Often used with
pg in practice
Strengths
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.
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".
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>.
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
Higher-level helpers are not in the core. The README points to a wiki list of extra modules "to complete the picture."
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.
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
Head to head
- pg vs Neon Serverless Driver used together in 31 projects
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
TypeScript ORM for SQL databases with type safety and zero-runtime overhead.
Modern ORM with intuitive schema, migrations, and type-safe database queries.
PostgreSQL driver optimized for serverless environments from Neon.
Fast MySQL driver with native protocol implementation, prepared statements and SSL.
Asynchronous, non-blocking SQLite3 database bindings for Node.js.
Get started
Install it.
npm install pgRules for your agent
## 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.