Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
D

dbmate

> 数据库
Open source

A lightweight, framework-agnostic database migration tool.

7.0K stars0 likes1 views
WebsiteGitHub

About

A lightweight, framework-agnostic database migration tool.

Dbmate

Dbmate is a database migration tool that will keep your database schema in sync across multiple developers and your production servers.

It is a standalone command line tool that can be used with Go, Node.js, Python, Ruby, PHP, Rust, C++, or any other language or framework you are using to write database-backed applications. This is especially helpful if you are writing multiple services in different languages, and want to maintain some sanity with consistent development tools.

For a comparison between dbmate and other popular database schema migration tools, please see Alternatives.

Table of Contents

  • Features
  • Installation
  • Commands
    • Command Line Options
  • Usage
    • Environment Variables
    • Connecting to the Database
      • PostgreSQL
      • MySQL
      • MariaDB
      • SQLite
      • ClickHouse
      • BigQuery
      • Spanner
    • Creating Migrations
    • Running Migrations
    • Rolling Back Migrations
    • Migration Options
    • Waiting For The Database
    • Exporting Schema File
  • Library
    • Use dbmate as a library
    • Embedding migrations
  • Concepts
    • Migration files
    • Schema file
    • Schema migrations table
  • Alternatives
  • Contributing

Features

  • Supports MySQL, MariaDB, PostgreSQL, SQLite, and ClickHouse
  • Uses plain SQL for writing schema migrations
  • Migrations are timestamp-versioned, to avoid version number conflicts with multiple developers
  • Migrations are run atomically inside a transaction
  • Supports creating and dropping databases (handy in development/test)
  • Supports saving a schema.sql file to easily diff schema changes in git
  • Database connection URL is defined using an environment variable (DATABASE_URL by default), or specified on the command line
  • Built-in support for reading environment variables from your .env file
  • Easy to distribute, single self-contained binary
  • Doesn't try to upsell you on a SaaS service

Installation

NPM

Install using NPM:

bash
npm install --save-dev dbmate
npx dbmate --help

macOS

Install using Homebrew:

bash
brew install dbmate
dbmate --help

Linux

Install the binary directly:

bash
sudo curl -fsSL -o /usr/local/bin/dbmate https://github.com/amacneil/dbmate/releases/latest/download/dbmate-linux-amd64
sudo chmod +x /usr/local/bin/dbmate
/usr/local/bin/dbmate --help

Windows

Install using Scoop

powershell
scoop install dbmate
dbmate --help

Docker

Docker images are published to GitHub Container Registry (ghcr.io/amacneil/dbmate).

Remember to set --network=host or see this comment for more tips on using dbmate with docker networking):

bash
docker run --rm -it --network=host ghcr.io/amacneil/dbmate --help

If you wish to create or apply migrations, you will need to use Docker's bind mount feature to make your local working directory (pwd) available inside the dbmate container:

bash
docker run --rm -it --network=host -v "$(pwd)/db:/db" ghcr.io/amacneil/dbmate new create_users_table

Commands

…

Command Line Options

The following options are available with all commands. You must use command line arguments in the order dbmate [global options] command [command options]. Most options can also be configured via environment variables (and loaded from your .env file, which is helpful to share configuration between team members).

  • --url, -u "protocol://host:port/dbname" - specify the database url directly. (env: DATABASE_URL)
  • --driver "driver_name" - specify the driver to use (if empty, the driver is derived from database URL scheme). (env: DBMATE_DRIVER)
  • --env, -e "DATABASE_URL" - specify an environment variable to read the database connection URL from.
  • --env-file ".env" - specify an alternate environment variables file(s) to load.
  • --migrations-dir, -d "./db/migrations" - where to keep the migration files. (env: DBMATE_MIGRATIONS_DIR)
  • --migrations-table "schema_migrations" - database table to record migrations in. (env: DBMATE_MIGRATIONS_TABLE)
  • --schema-file, -s "./db/schema.sql" - a path to keep the schema.sql file. (env: DBMATE_SCHEMA_FILE)
  • --no-dump-schema - don't auto-update the schema.sql file on migrate/rollback (env: DBMATE_NO_DUMP_SCHEMA)
  • --strict - fail if migrations would be applied out of order (env: DBMATE_STRICT)
  • --wait - wait for the db to become available before executing the subsequent command (env: DBMATE_WAIT)
  • --wait-timeout 60s - timeout for --wait flag (env: DBMATE_WAIT_TIMEOUT)
  • --wait-interval 1s - time to wait between connection attempts for --wait flag (env: DBMATE_WAIT_INTERVAL)

Usage

Environment Variables

Most dbmate settings can be configured with environment variables in addition to command line flags. This is useful for twelve-factor style deployments and for sharing local configuration via a .env file.

Which settings use which environment variables

Setting Environment variable Notes
Database URL DATABASE_URL (default) Overridden by --url / -u. Use --env / -e to read the URL from a different variable (for example TEST_DATABASE_URL).
Driver DBMATE_DRIVER Used when --driver is not set.
Migrations directory DBMATE_MIGRATIONS_DIR Corresponds to --migrations-dir / -d.
Migrations table DBMATE_MIGRATIONS_TABLE Corresponds to --migrations-table.
Schema file DBMATE_SCHEMA_FILE Corresponds to --schema-file / -s.
Disable schema dump DBMATE_NO_DUMP_SCHEMA Corresponds to --no-dump-schema.
Wait for database DBMATE_WAIT Corresponds to --wait.
Wait timeout DBMATE_WAIT_TIMEOUT Corresponds to --wait-timeout.
Wait interval DBMATE_WAIT_INTERVAL Corresponds to --wait-interval.
Strict migrations DBMATE_STRICT Corresponds to --strict on up / migrate.
Verbose SQL output DBMATE_VERBOSE Corresponds to --verbose / -v on up / migrate / rollback.

--env and --env-file are CLI-only options (they are not themselves read from environment variables).

Loading .env files

By default, dbmate loads environment variables from a .env file in the current working directory (if present). Missing files are ignored; an invalid dotenv file causes dbmate to exit with an error.

To load one or more alternate dotenv files, pass --env-file (repeatable). When any --env-file is given, only the listed files are loaded - the default .env is not included unless you specify it:

bash
dbmate --env-file .env.development up
dbmate --env-file .env --env-file .env.local up

Files are loaded in the order given.

Variable precedence

From highest to lowest priority:

  1. Command line flags (for example --url, --driver, --migrations-dir)
  2. Environment variables already set in the calling process
  3. Values from dotenv files (.env or files passed with --env-file)
  4. Built-in defaults

Dotenv files never overwrite variables that are already set in the process environment. For the database URL specifically, --url / -u takes precedence over the environment variable named by --env / -e (default DATABASE_URL).

Connecting to the Database

Dbmate locates your database using the DATABASE_URL environment variable by default. If you are writing a twelve-factor app, you should be storing all connection strings in environment variables.

To make this easy in development, dbmate looks for a .env file in the current directory, and treats any variables listed there as if they were specified in the current environment (existing environment variables take preference, however). See Environment Variables for details on --env-file and precedence.

If you do not already have a .env file, create one and add your database connection URL:

bash
$ cat .env
DATABASE_URL="postgres://[email protected]:5432/myapp_development?sslmode=disable"

DATABASE_URL should be specified in the following format:

protocol://username:password@host:port/database_name?options
  • protocol must be one of mysql, postgres, postgresql, sqlite, sqlite3, clickhouse
  • username and password must be URL encoded (you will get an error if you use special characters)
  • host can be either a hostname or IP address
  • options are driver-specific (refer to the underlying Go SQL drivers if you wish to use these)

Dbmate can also load the connection URL from a different environment variable. For example, before running your test suite, you may wish to drop and recreate the test database. One easy way to do this is to store your test database connection URL in the TEST_DATABASE_URL environment variable:

bash
$ cat .env
DATABASE_URL="postgres://[email protected]:5432/myapp_dev?sslmode=disable"
TEST_DATABASE_URL="postgres://[email protected]:5432/myapp_test?sslmode=disable"

You can then specify this environment variable in your test script (Makefile or similar):

bash
$ dbmate -e TEST_DATABASE_URL drop
Dropping: myapp_test
$ dbmate -e TEST_DATABASE_URL --no-dump-schema up
Creating: myapp_test
Applying: 20151127184807_create_users_table.sql
Applied: 20151127184807_create_users_table.sql in 123µs

Alternatively, you can specify the url directly on the command line:

bash
$ dbmate -u "postgres://[email protected]:5432/myapp_test?sslmode=disable" up

The only advantage of using dbmate -e TEST_DATABASE_URL over dbmate -u $TEST_DATABASE_URL is that the former takes advantage of dbmate's automatic .env file loading.

PostgreSQL

When connecting to Postgres, you may need to add the sslmode=disable option to your connection string, as dbmate by default requires a TLS connection (some other frameworks/languages allow unencrypted connections by default).

bash
DATABASE_URL="postgres://username:[email protected]:5432/database_name?sslmode=disable"

A socket or host parameter can be specified to connect through a unix socket (note: specify the directory only):

bash
DATABASE_URL="postgres://username:password@/database_name?socket=/var/run/postgresql"

For passwordless authentication such as PostgreSQL peer auth, the password can be omitted (note the @ is still required):

bash
DATABASE_URL="postgres://username@/database_name?socket=/var/run/postgresql"

If the username is also omitted, it defaults to the PGUSER environment variable if set, otherwise the OS username (via lib/pq, matching libpq behavior):

bash
DATABASE_URL="postgres:///database_name?socket=/var/run/postgresql"

A search_path parameter can be used to specify the current schema while applying migrations, as well as for dbmate's schema_migrations table. If the schema does not exist, it will be created automatically. If multiple comma-separated schemas are passed, the first will be used for the schema_migrations table.

bash
DATABASE_URL="postgres://username:[email protected]:5432/database_name?search_path=myschema"
bash
DATABASE_URL="p

Issues· 33 open

View all issuesOpen on GitHub
  • #670

    "Applied:" is logged when migration fails

    questionUpdated Sep 22, 2026
  • #671

    Bug? Expected functionality? No error thrown on rollback if migration doesn't exist

    feature requestneeds reviewbreaking changeUpdated Sep 22, 2026
  • #440

    Migration table is not found

    bugneeds more infoUpdated Sep 22, 2026
  • #675

    Multi-statement allows partial application and ordering issues

    bugneeds workUpdated Sep 22, 2026
  • #783

    Default ZooKeeper path for schema_migration table in a distributed (on_cluster) ClickHouse setup clashes with multiple databases

    bugUpdated Sep 16, 2026
  • #820

    Error on multiple migrations in a single file

    bugUpdated Sep 11, 2026
  • #786

    Debug log not shown in v2.29.4 with debug=1 in DSN

    Updated Apr 16, 2026
  • #759

    Windows binary reported as virus by GDATA and browser

    bugneeds triageUpdated Mar 12, 2026
  • #722

    `pq: syntax error at or near "."` error received when calling functions with schema in front.

    bugneeds more infoUpdated Jan 30, 2026
  • #429

    Separating `schema_migration` from the search_path

    bugneeds more infoUpdated Jan 12, 2026

> Tags

Goclickhousecppdatabasedatabase-migrations

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 22, 2026
Category数据库
PricingOpen source

> Related tools

P
PostgreSQL
功能强大的开源关系型数据库
R
Redis
内存数据结构存储,常用作缓存与队列
M
MySQL
广泛使用的开源关系型数据库