node-pg-migrate vs postgres-migrations
Database Migration Tools for PostgreSQL Comparison
1 Year
node-pg-migratepostgres-migrations
What's Database Migration Tools for PostgreSQL?

Database migration tools are essential for managing changes to a database schema over time. They allow developers to version control their database schema, apply changes incrementally, and roll back changes if necessary. This is particularly important in team environments where multiple developers are working on the same codebase, as it ensures that everyone’s database schema stays in sync. node-pg-migrate is a popular choice for PostgreSQL migrations in Node.js applications, offering a simple and flexible API for creating and running migrations. It supports both up and down migrations, allows for custom migration scripts, and integrates well with existing Node.js workflows. postgres-migrations, on the other hand, is a lightweight migration tool that focuses on simplicity and ease of use. It provides a straightforward command-line interface for managing migrations and is designed to work seamlessly with PostgreSQL databases. While it may not have as many features as node-pg-migrate, its simplicity makes it a great choice for small projects or teams looking for a no-frills migration solution.

Package Weekly Downloads Trend
Github Stars Ranking
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
node-pg-migrate102,3011,380534 kB4618 days agoMIT
postgres-migrations19,87533043.1 kB38-MIT
Feature Comparison: node-pg-migrate vs postgres-migrations

Integration with Node.js

  • node-pg-migrate:

    node-pg-migrate is designed specifically for Node.js applications, making it easy to integrate into your existing workflow. It provides a JavaScript API for creating and running migrations, as well as a command-line interface for managing migrations from the terminal.

  • postgres-migrations:

    postgres-migrations is a standalone tool that can be used with any PostgreSQL database. While it does not have a Node.js-specific API, it can be easily integrated into Node.js applications using its command-line interface.

Customization

  • node-pg-migrate:

    node-pg-migrate offers a high degree of customization, allowing developers to create complex migrations using JavaScript. It supports both up and down migrations, and developers can write custom migration scripts as needed.

  • postgres-migrations:

    postgres-migrations is more limited in terms of customization, focusing on simple, straightforward migrations. It does not support complex migration logic or custom scripts, which may be a limitation for larger projects.

Simplicity

  • node-pg-migrate:

    node-pg-migrate provides a balance of features and simplicity, but its extensive capabilities may require a learning curve for new users. The documentation is thorough, and there are plenty of examples to help developers get started.

  • postgres-migrations:

    postgres-migrations is designed to be simple and easy to use, with minimal configuration required. Its straightforward command-line interface makes it accessible for developers of all skill levels.

Community and Support

  • node-pg-migrate:

    node-pg-migrate has a large and active community, with regular updates and a wealth of resources available online. This makes it easy to find help and support when needed.

  • postgres-migrations:

    postgres-migrations is a smaller project with a more limited community. While it is actively maintained, it may not have as many resources or third-party plugins available.

Code Example

  • node-pg-migrate:

    Creating a migration with node-pg-migrate

    // migrations/20230101_create_users_table.js
    exports.up = (pgm) => {
      pgm.createTable('users', {
        id: { type: 'serial', primaryKey: true },
        name: { type: 'varchar(100)', notNull: true },
        email: { type: 'varchar(100)', unique: true, notNull: true },
      });
    };
    
    exports.down = (pgm) => {
      pgm.dropTable('users');
    };
    
  • postgres-migrations:

    Creating a migration with postgres-migrations

    -- migrations/20230101_create_users_table.sql
    CREATE TABLE users (
      id SERIAL PRIMARY KEY,
      name VARCHAR(100) NOT NULL,
      email VARCHAR(100) UNIQUE NOT NULL
    );
    
    -- To roll back the migration
    DROP TABLE users;
    
How to Choose: node-pg-migrate vs postgres-migrations
  • node-pg-migrate:

    Choose node-pg-migrate if you need a feature-rich migration tool that integrates well with Node.js applications, supports complex migrations, and offers a high degree of customization.

  • postgres-migrations:

    Choose postgres-migrations if you prefer a lightweight, easy-to-use tool for managing migrations with a focus on simplicity and minimal configuration.

README for node-pg-migrate

node-pg-migrate

npm version npm downloads Continuous Integration Postgres Test Cockroach Test Licence

Node.js database migration management built exclusively for postgres. (But can also be used for other DBs conforming to SQL standard - e.g. CockroachDB.)
Started by Theo Ephraim, then handed over to Salsita Software and now maintained by @Shinigami92.

Preconditions

  • Node.js 20.11 or higher
  • PostgreSQL 13 or higher (lower versions may work but are not supported officially)

If you don't already have the pg library installed, you will need to add pg as either a direct or dev dependency

npm add pg

Installation

npm add --save-dev node-pg-migrate

Installing this module adds a runnable file into your node_modules/.bin directory. If installed globally (with the -g option), you can run node-pg-migrate and if not, you can run ./node_modules/.bin/node-pg-migrate.js

Quick Example

Add "migrate": "node-pg-migrate" to scripts section of your package.json so you are able to quickly run commands.

Run npm run migrate create my-first-migration. It will create file xxx_my-first-migration.js in migrations folder.
Open it and change contents to:

export const up = (pgm) => {
  pgm.createTable('users', {
    id: 'id',
    name: { type: 'varchar(1000)', notNull: true },
    createdAt: {
      type: 'timestamp',
      notNull: true,
      default: pgm.func('current_timestamp'),
    },
  });
  pgm.createTable('posts', {
    id: 'id',
    userId: {
      type: 'integer',
      notNull: true,
      references: '"users"',
      onDelete: 'CASCADE',
    },
    body: { type: 'text', notNull: true },
    createdAt: {
      type: 'timestamp',
      notNull: true,
      default: pgm.func('current_timestamp'),
    },
  });
  pgm.createIndex('posts', 'userId');
};

Save migration file.

Now you should put your DB connection string to DATABASE_URL environment variable and run npm run migrate up. (e.g. DATABASE_URL=postgres://test:test@localhost:5432/test npm run migrate up)

You should now have two tables in your DB :tada:

If you want to change your schema later, you can e.g. add lead paragraph to posts:

Run npm run migrate create posts_lead, edit xxx_posts_lead.js:

export const up = (pgm) => {
  pgm.addColumns('posts', {
    lead: { type: 'text', notNull: true },
  });
};

Run npm run migrate up and there will be a new column in posts table :tada:

Want to know more? Read docs:

Docs

Full docs are available at https://salsita.github.io/node-pg-migrate

Explanation & Goals

Why only Postgres? - By writing this migration tool specifically for postgres instead of accommodating many databases, we can actually provide a full featured tool that is much simpler to use and maintain. I was tired of using crippled database tools just in case one day we switch our database.

Async / Sync - Everything is async in node, and that's great, but a migration tool should really just be a fancy wrapper that generates SQL. Most other migration tools force you to bring in control flow libraries or wrap everything in callbacks as soon as you want to do more than a single operation in a migration. Plus by building up a stack of operations, we can automatically infer down migrations (sometimes) to save even more time.

Naming / Raw Sql - Many tools force you to use their constants to do things like specify data types. Again, this tool should be a fancy wrapper that generates SQL, so whenever possible, it should just pass through user values directly to the SQL. The hard part is remembering the syntax of the specific operation, not remembering how to type "timestamp"!

Contributing

GitHub repo Good Issues for newbies GitHub Help Wanted issues GitHub Help Wanted PRs GitHub repo Issues

👋 Welcome, new contributors!

Whether you're a seasoned developer or just getting started, your contributions are valuable to us. Don't hesitate to jump in, explore the project, and make an impact.

License

MIT