API Reference

node-pg-migrate

PostgreSQL migration tool offering a CLI and a programmatic runner with reversible migration builder helpers.

Official Documentation ↗
CompatibilityThis documentation covers version v9.0.0. Check the official docs for the latest API surface.

Installation

npmnpm install --save-dev node-pg-migrate pg
pnpmpnpm add -D node-pg-migrate pg

Methods

Overview

node-pg-migrate is a PostgreSQL migration tool with a CLI and a programmatic runner, plus reversible migration builder helpers. This reference lists the most frequently used APIs with accurate signatures and runnable snippets.

Core APIs

  • node-pg-migrate up — Applies pending migrations found in the migrations directory using the configured database URL.
  • pgm.createTable — Migration builder helper that emits a CREATE TABLE statement with the declared columns.
  • migrate (runner) — Programmatically runs migrations with explicit database URL, directory, and direction options.

Example

# apply all pending migrations
node-pg-migrate up

# roll back the last two migrations
node-pg-migrate down 2

Notes

Builder helpers derive the down migration automatically, but destructive operations still require care. Set a migrations table so applied names are tracked.

Verification

Apply a createTable migration, confirm the table exists, run down, and confirm the table is removed.

Errors

The official API reference declares the following error types:

  • TypeError (runner() 以 Promise reject 抛出(migrationsTable 缺失或非法时,在连接数据库前)) — official text: “An omitted or invalid value causes runner() to reject with a TypeError before connecting to the database or issuing queries.” (source: https://salsita.github.io/node-pg-migrate/api, fetched 2026-11-08T17:52:30+08:00)

Security

Official security statements captured from the package documentation:

  • CLI 使用与 pg.Client 相同的连接设置;要校验服务端证书与主机名需显式使用 sslrootcert + sslmode=verify-full — “The CLI accepts the same connection settings as pg.Client. To trust a private CA while checking the server certificate and hostname, use sslrootcert and sslmode=verify-full in DATABASE_URL.” (source: https://salsita.github.io/node-pg-migrate/cli, fetched 2026-11-08T17:52:30+08:00)

API reference evidence

Fetched from official sources on 2026-11-08T17:52:30+08:00 via WebFetch https://salsita.github.io/node-pg-migrate/cli.

FAQ

Where does node-pg-migrate store applied migration names?
In the migrationsTable, named pgmigrations by default, which records each applied file name.
How do I write a reversible migration?
Export both up and down functions; the builder helpers such as createTable auto-generate the down operation unless you override it.
How does it get the database URL?
By default it reads DATABASE_URL, or the environment variable selected by --database-url-var on the CLI.