# Postgres.js — Fastest Full-Featured PostgreSQL Client for Node.js > A high-performance PostgreSQL client for Node.js and Deno with tagged template literals and zero dependencies. ## Install Save as a script file and run: # Postgres.js — Fastest Full-Featured PostgreSQL Client for Node.js ## Quick Use ```bash npm install postgres ``` ```js import postgres from "postgres"; const sql = postgres("postgres://user:pass@localhost/db"); const users = await sql`SELECT * FROM users WHERE age > ${30}`; ``` ## Introduction Postgres.js is a PostgreSQL client for Node.js and Deno built for speed and developer ergonomics. It uses tagged template literals for safe, parameterized queries and delivers top-tier performance through pipelining, binary protocol support, and connection pooling — all with zero external dependencies. ## What Postgres.js Does - Executes parameterized SQL queries using tagged template literals - Manages connection pools automatically with configurable limits - Supports real-time LISTEN/NOTIFY for event-driven architectures - Handles transactions, savepoints, and prepared statements natively - Provides built-in support for PostgreSQL-specific types like arrays, JSON, and ranges ## Architecture Overview Postgres.js communicates with PostgreSQL using the binary wire protocol for reduced serialization overhead. It maintains an internal connection pool and pipelines multiple queries over a single connection when possible. Query parameters from tagged templates are automatically escaped and sent as protocol-level parameters, preventing SQL injection by design. ## Self-Hosting & Configuration - Install via npm or import directly in Deno with the URL import pattern - Configure connection via URL string or an options object with host, port, database, and SSL settings - Set max connections, idle timeout, and connect timeout through the pool options - Enable SSL by passing ssl: true or providing custom certificate paths - Use the onnotice and debug callbacks for logging and diagnostics ## Key Features - Tagged template queries prevent SQL injection at the protocol level - Connection pipelining sends multiple queries without waiting for responses - Realtime subscriptions via PostgreSQL LISTEN/NOTIFY - Automatic type serialization for dates, JSON, arrays, bytea, and custom types - Dynamic query building with the sql() helper for safe column and table references ## Comparison with Similar Tools - **node-postgres (pg)** — pg is callback-based by default; Postgres.js is async-first with a simpler API - **Knex.js** — Knex is a query builder; Postgres.js uses raw SQL via tagged templates - **Prisma** — Prisma is a full ORM with schema migrations; Postgres.js is a lightweight driver - **Drizzle ORM** — Drizzle adds a type-safe query layer; Postgres.js is the raw connection layer - **pg-promise** — pg-promise adds a promise wrapper over pg; Postgres.js is a standalone implementation ## FAQ **Q: Is Postgres.js safe from SQL injection?** A: Yes. Tagged template literals send user values as parameterized inputs at the protocol level, never interpolated into the query string. **Q: Does it work with Deno and Bun?** A: Yes. Postgres.js supports Node.js, Deno, Bun, and Cloudflare Workers. **Q: Can I use connection pooling?** A: Yes. Postgres.js includes built-in connection pooling. Set the max option to control the pool size. **Q: Does it support TypeScript?** A: Yes. Postgres.js ships with TypeScript type definitions and supports generic row typing for query results. ## Sources - https://github.com/porsager/postgres - https://github.com/porsager/postgres#readme --- Source: https://tokrepo.com/en/workflows/asset-793a425f Author: Script Depot