🗄️
Database8M+/wkMIT

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
npm install pg
yarn
yarn add pg
pnpm
pnpm add pg

Import

ESM
import pg from 'pg';

Quick Example

usage
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

Packagepg
CategoryDatabase
Weekly Downloads8M+
LicenseMIT
Installnpm install pg

Related Packages

Browse npm Packages by Category

Explore our reference of 200 popular npm packages with install commands, examples, and quick-start guides.