Registry / database / pg-diff-api

pg-diff-api

JSON →
library1.5.3jsnpmunverified

PostgreSQL migration strategy for NodeJS that provides APIs to diff and migrate PostgreSQL schemas and data. Version 1.5.3, released under MIT license. It offers programmatic schema comparison and migration generation, unlike CLI-based alternatives. Features include comparison of database schemas, generation of migration scripts, and support for complex schema objects. Suitable for Node.js >= 8.11.1. Active maintenance with recent releases.

npm install pg-diff-api
INSTALL
IMPORT
SIG · PG-DIFF-API
P
pg-diff-api
databasejavascriptv1.5.3
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

PgDiffApi
import { PgDiffApi } from 'pg-diff-api'
const PgDiffApi = require('pg-diff-api').PgDiffApi
ESM default export available since v1.5, but named export also works.
compareSchemas
import { compareSchemas } from 'pg-diff-api'
import { compare } from 'pg-diff-api'
Function exported directly; do not confuse with other functions.
generateMigration
import { generateMigration } from 'pg-diff-api'
import { createMigration } from 'pg-diff-api'
Exact function name as per documentation.

Connects to two PostgreSQL databases, compares their schemas, and generates a migration SQL script.

import { PgDiffApi } from 'pg-diff-api'; import { Client } from 'pg'; const sourceClient = new Client({ host: 'localhost', port: 5432, database: 'source_db', user: 'user', password: process.env.DB_PASS ?? '', }); const targetClient = new Client({ host: 'localhost', port: 5432, database: 'target_db', user: 'user', password: process.env.DB_PASS ?? '', }); async function run() { await sourceClient.connect(); await targetClient.connect(); const api = new PgDiffApi({ client: sourceClient }); const diff = await api.compareSchemas(targetClient); console.log('Differences:', JSON.stringify(diff, null, 2)); const migration = await api.generateMigration(diff); console.log('Migration SQL:', migration); await sourceClient.end(); await targetClient.end(); } run().catch(console.error);
Debug
Known issues
breakingIn version 1.5.0, the API changed from callback-based to Promise-based. Old code using callbacks will break.
fix
Update to use async/await or .then() syntax.
affects: <1.5.0
deprecatedDirect use of pg connection strings in PgDiffApi constructor is deprecated. Use pg Client instance instead.
fix
Pass a pg Client object instead of a connection string.
affects: >=1.4.0
gotchaThe compareSchemas method requires both source and target clients; defaulting to same client may produce empty diff.
fix
Ensure you pass a different target client (even if same database, create a new connection).
affects: all
breakingIn version 1.3.0, the output format of compareSchemas changed from an array to an object.
fix
Update code to handle object with properties like 'tables', 'columns', etc.
affects: <1.3.0
Errors
Common errors & fixes
TypeError: PgDiffApi is not a constructor
Importing incorrectly (e.g., default vs named import).
fix
Use 'import { PgDiffApi } from 'pg-diff-api'' instead of 'import PgDiffApi from ...'
Error: Cannot find module 'pg'
Missing or incorrect version of pg dependency.
fix
Run 'npm install pg' and ensure it's the correct version (compatible with pg-diff-api).
TypeError: sourceClient.connect is not a function
Passing a connection string instead of a pg Client instance.
fix
Create a new Client object and connect before passing to PgDiffApi.
Upgrade
Version history
1.5.3latest on npm
Audit
Dependencies
pgrequiredPostgreSQL client for database connections
Agent activity
13 hits · last 30 days
node
10
Amazon
1
Resources
pg-diff-api — npm install pg-diff-api · libregistry