postgres
Postgres.js is the fastest full-featured PostgreSQL client for Node.js and Deno, using tagged template literals for safe, readable queries. The library provides…
Installation
npm install postgres
yarn add postgres
pnpm add postgres
Import
import postgres from 'postgres';
Quick Example
import postgres from 'postgres';
const sql = postgres(process.env.DATABASE_URL);
const users = await sql`SELECT * FROM users WHERE active = true`;
const [user] = await sql`
INSERT INTO users (name, email)
VALUES (${'Alice'}, ${'[email protected]'})
RETURNING *
`;About postgres
Postgres.js is the fastest full-featured PostgreSQL client for Node.js and Deno, using tagged template literals for safe, readable queries. The library provides a simple API where the connection is also the query function: const users = await sql`SELECT * FROM users WHERE id = ${id}`. This tagged template approach automatically handles parameter binding, type serialization, and SQL injection prevention while keeping queries readable as plain SQL. Postgres.js supports connection pooling, prepared statements, realtime notifications (LISTEN/NOTIFY), transactions, savepoints, large objects, COPY for bulk operations, and dynamic query building through sql() fragments that can be composed safely. The library handles automatic type conversion between PostgreSQL and JavaScript types, including JSON, arrays, timestamps, and custom types. Postgres.js supports both ESM and CommonJS imports and provides full TypeScript types with generic query result typing. The library achieves exceptional performance through its custom protocol implementation and efficient memory management. Postgres.js is gaining rapid adoption as a modern alternative to pg, particularly for new projects that appreciate its cleaner API and use of tagged template literals for safe SQL composition.
Quick Facts
| Package | postgres |
| Category | Database |
| Weekly Downloads | 300K+ |
| License | Unlicense |
| Install | npm install postgres |
Related Packages
pg (node-postgres) is the standard PostgreSQL client for Node.js, providing low-level access to Post…
Drizzle ORM is a TypeScript ORM that provides a SQL-like query builder with full type safety, zero o…
Kysely is a type-safe TypeScript SQL query builder that provides autocompletion, type checking, and …
Slonik is a Node.js PostgreSQL client with strict types, detailed logging, and assertions that encou…
Knex.js is a batteries-included SQL query builder for Node.js that supports PostgreSQL, MySQL, Maria…
Browse npm Packages by Category
Explore our reference of 200 popular npm packages with install commands, examples, and quick-start guides.