PostgreSQL Schema Migrations with Schema Support

7.0.2 · active · verified Wed Apr 22

Postgres-schema-migrations is a JavaScript/TypeScript library for managing database schema changes in PostgreSQL. It is a fork of `postgres-migrations` that specifically adds support for schema namespaces, allowing separate migrations to be tracked per schema, which is beneficial for multi-tenant applications or reusing database code. Currently at version 7.0.2, this library is actively maintained. It mandates SQL files for migration definitions, ordered numerically, and deliberately omits 'down' migrations, advocating for 'rolling forward' with new migrations to reverse changes. A key differentiator is its emphasis on atomic transactions for each migration and hash-based checks to ensure migration immutability, preventing accidental changes to already-applied migrations. It supports Node.js 10.17.0+ and PostgreSQL 9.4+.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to apply database migrations using a `pg` client, showing both default (public) and namespaced schema migrations. It highlights connection management and error handling.

import { migrate } from "postgres-schema-migrations";
import { Client } from 'pg';
import path from 'path';

async function runMigrations() {
  const migrationFilesPath = path.resolve(__dirname, 'migrations');

  // Using a direct pg client allows more control over connection lifecycle
  const dbConfig = {
    database: process.env.DB_DATABASE ?? 'database-name',
    user: process.env.DB_USER ?? 'postgres',
    password: process.env.DB_PASSWORD ?? 'password',
    host: process.env.DB_HOST ?? 'localhost',
    port: parseInt(process.env.DB_PORT ?? '5432', 10),
  };

  const client = new Client(dbConfig);
  await client.connect();

  try {
    console.log('Running migrations for public schema...');
    await migrate({ client }, migrationFilesPath); // Defaults to 'public' schema
    console.log('Public schema migrations complete.');

    // Example of running migrations for a specific schema
    console.log('Running migrations for custom_schema...');
    const schemaMigrationsPath = path.resolve(__dirname, 'schema_migrations');
    await migrate({ client }, schemaMigrationsPath, { schema: 'custom_schema' });
    console.log('Custom schema migrations complete.');

  } catch (error) {
    console.error('Migration failed:', error);
    process.exit(1);
  } finally {
    await client.end();
  }
}

// Dummy migration files setup for example to be runnable
// In a real project, these would be actual .sql files.
// Make sure these directories exist for the quickstart to fully run.
// Example: migrations/1_create_users_table.sql
// CREATE TABLE users (id SERIAL PRIMARY KEY, name VARCHAR(255) NOT NULL);
// Example: schema_migrations/1_create_products_table.sql
// CREATE SCHEMA IF NOT EXISTS custom_schema;
// CREATE TABLE custom_schema.products (id SERIAL PRIMARY KEY, name VARCHAR(255) NOT NULL);

runMigrations();

view raw JSON →