Scripts2026年5月21日·1 分钟阅读

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.

Agent 就绪

这个资产可以被 Agent 直接读取和安装

TokRepo 同时提供通用 CLI 命令、安装契约、metadata JSON、按适配器生成的安装计划和原始内容链接,方便 Agent 判断适配度、风险和下一步动作。

Stage only · 29/100Stage only
Agent 入口
任意 MCP/CLI Agent
类型
CLI Tool
安装
Single
信任
信任等级:Established
入口
Postgres.js Overview
通用 CLI 安装命令
npx tokrepo install 793a425f-5551-11f1-9bc6-00163e2b0d79

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

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产