pg
pg (node-postgres) is the standard PostgreSQL client for Node.js, providing low-level access to PostgreSQL databases with connection pooling, parameterized quer…
Installation
npm install pg
yarn add pg
pnpm add pg
Import
import pg from 'pg';
Quick Example
import pg from 'pg';
const { Pool } = pg;
const pool = new Pool({ connectionString: process.env.DATABASE_URL });
const { rows } = await pool.query(
'SELECT * FROM users WHERE id = $1',
[1]
);
console.log(rows[0]);About pg
pg (node-postgres) is the standard PostgreSQL client for Node.js, providing low-level access to PostgreSQL databases with connection pooling, parameterized queries, prepared statements, and streaming support. The library consists of two packages: pg for the pure JavaScript client and pg-native for an optional native C binding that provides better performance. pg provides a Pool class for connection pooling that manages multiple client connections efficiently, automatically handling connection creation, reuse, and cleanup. Queries use parameterized syntax ($1, $2) for safe parameter binding that prevents SQL injection. The library supports async/await, callback, and stream-based query interfaces, LISTEN/NOTIFY for real-time notifications, COPY for bulk data import/export, large object support, and SSL/TLS connections. pg includes type parsing that converts PostgreSQL types to JavaScript types (timestamps to Date, json to objects, arrays to arrays) with customizable type parsers. The library is the foundation used by ORMs and query builders including Prisma, Drizzle, Knex, Sequelize, and TypeORM for their PostgreSQL connections, making it one of the most critical infrastructure packages in the Node.js ecosystem.
Quick Facts
| Package | pg |
| Category | Database |
| Weekly Downloads | 8M+ |
| License | MIT |
| Install | npm install pg |
Related Packages
Postgres.js is the fastest full-featured PostgreSQL client for Node.js and Deno, using tagged templa…
Prisma is a next-generation Node.js and TypeScript ORM that provides a declarative data modeling lan…
Knex.js is a batteries-included SQL query builder for Node.js that supports PostgreSQL, MySQL, Maria…
Sequelize is a promise-based Node.js ORM that supports PostgreSQL, MySQL, MariaDB, SQLite, and Micro…
Drizzle ORM is a TypeScript ORM that provides a SQL-like query builder with full type safety, zero o…
Browse npm Packages by Category
Explore our reference of 200 popular npm packages with install commands, examples, and quick-start guides.