The `postgres-migrations` package provides a robust database migration solution for PostgreSQL, directly inspired by Stack Overflow's deployment methodology. Currently at version 5.3.0, it offers a stable and opinionated approach to managing database schema changes. Key differentiators include its strict sequential SQL file-based ordering, which explicitly avoids timestamp-based naming to ensure consistent execution order across all environments. The library deliberately eschews 'down' migrations, advocating for a 'roll forward' philosophy where issues are addressed by applying new incremental migrations. It enforces database integrity by performing hash checks on previously applied migration files, preventing accidental or unauthorized modifications. The package maintains a hidden `migrations` table to track applied scripts and requires Node.js 10.17.0+ and PostgreSQL 9.4+.
npm install postgres-migrationsVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to apply migrations using an existing `pg.Client` instance, ensuring proper connection handling and error reporting. It uses environment variables for database credentials.
Plan for all schema changes to be additive or corrective via new migration files, rather than relying on reversions.
Never alter migration files after they have been committed and applied to any environment. Create a new migration file to introduce any necessary changes or corrections.
Ensure all migration files have unique, sequential prefixes. Use `loadMigrationFiles` or the `pg-validate-migrations` bin script to check for conflicts early in development.
Explicitly set `ensureDatabaseExists: true` if you want the library to create the target database for you, or ensure the database exists manually before running migrations.
Upgrade your Node.js runtime and PostgreSQL database to meet the minimum version requirements.
Revert the changes to the migration file `X_my-migration.sql` to its original state, or ensure no applied migration files are modified. All subsequent changes should be in new migration files.
Rename one of the conflicting migration files (e.g., `X_migration2.sql` to `Y_migration2.sql` where `Y` is the next available sequence number) to ensure all migration files have unique, sequential prefixes.
Ensure you are passing an instantiated `pg.Client`, `pg.Pool`, or `pg.PoolClient` instance that has already called `.connect()` (for `Client`) or is ready for use (for `Pool`). Alternatively, pass the database connection config directly.