Dbmate: Database Migration Tool

2.32.0 · active · verified Sun Apr 19

Dbmate is a standalone, framework-agnostic command-line tool designed for managing database schema migrations. It supports a variety of databases including MySQL, PostgreSQL, SQLite, ClickHouse, BigQuery, and Spanner. The tool uses plain SQL for writing migrations, employs timestamp-versioning to prevent conflicts in collaborative environments, and runs migrations atomically within transactions. Beyond basic migration, it can also create and drop databases (useful for development/testing workflows) and export a `schema.sql` file to easily track schema changes in version control. Currently at version 2.32.0, Dbmate maintains an active and consistent release cadence, frequently releasing patch updates for bug fixes and dependency bumps, with minor versions arriving every few weeks to months. Its primary differentiator is its independence from any specific programming language or framework, making it an ideal choice for polyglot microservice architectures where a consistent database migration strategy across different technology stacks is desired.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates the core workflow of Dbmate, including installation as a development dependency, initializing the migration setup, creating new SQL migration files, applying pending migrations, rolling back the last migration, and dumping the schema. It highlights the crucial role of the `DATABASE_URL` environment variable.

npm install --save-dev dbmate

# Set your database connection string as an environment variable.
# Replace with your actual database details.
# For PostgreSQL:
export DATABASE_URL="postgres://user:password@localhost:5432/mydatabase?sslmode=disable"
# For SQLite:
# export DATABASE_URL="sqlite://./data/application.db"

# Initialize dbmate: Creates a 'migrations' directory and an initial schema file.
# This should typically be run once per project.
npx dbmate init

# Create a new, timestamped migration file for a new table.
# This generates an empty SQL file in the 'migrations' directory.
npx dbmate new create_products_table

# Edit the generated migration file (e.g., migrations/20260419123456_create_products_table.sql).
# Add your 'UP' and 'DOWN' SQL statements:
# -- MIGRATION UP --
# CREATE TABLE products (
#   id SERIAL PRIMARY KEY,
#   name VARCHAR(255) NOT NULL,
#   price DECIMAL(10, 2) NOT NULL
# );
# -- MIGRATION DOWN --
# DROP TABLE products;

# Apply all pending migrations to your database.
npx dbmate up

# To roll back the very last applied migration:
npx dbmate down

# To dump the current database schema to 'schema.sql' for version control:
npx dbmate dump-schema

view raw JSON →