Database and file inputs
Compare schemas using database connection strings or a SQL file checked into your repository.
dbdiffv0.2.1
Database schema comparison CLI
Compare two databases or check a database against a SQL schema file. Review table, column and index changes, or check for drift in CI.
Rust · MIT · Last update
$ dbdiff postgres://prod/app --schema schema.sql
~ orders
+ paid_at timestamptz
+ idx_paid_at INDEX
- payment_date varchar(32)
$ dbdiff … --ci
# exit 0: match · exit 1: drift
dbdiff
Compare schemas using database connection strings or a SQL file checked into your repository.
Read table, column and index differences, then review generated SQL and locking warnings before applying it.
CI mode reports drift with an exit code. JSON output lets your own tooling decide what happens next.
Database schema comparison CLI
Point dbdiff at the environments or the SQL schema you need to compare.
Save the output, review destructive or locking operations, and run it through your normal deployment process.
01 / Install
cargo install dbdiff02 / README
Setup, examples and API reference.
Compare database schemas across environments and generate safe migration SQL — in one command.
Project website · All projects by rekurt
$ dbdiff postgres://prod/myapp postgres://staging/myapp
~ table: orders
+ column paid_at timestamptz NOT NULL DEFAULT now()
+ index idx_orders_paid_at ON orders(paid_at)
- column payment_date varchar(32)
~ table: users
+ column deleted_at timestamptz
Generated migration → migration_20240406_143201.sql
Most teams discover schema drift at the worst possible moment — right before a deploy. Existing tools either support only one database, require heavy setup, or can't compare a live DB against a .sql file.
dbdiff is a single binary that works anywhere CI runs.
Cargo:
cargo install dbdiff
Binary — download from Releases and put it in your $PATH:
# Linux (x86_64)
curl -sSL https://github.com/rekurt/dbdiff/releases/latest/download/dbdiff-x86_64-unknown-linux-musl.tar.gz | tar xz
sudo mv dbdiff /usr/local/bin/
# macOS (Apple Silicon)
curl -sSL https://github.com/rekurt/dbdiff/releases/latest/download/dbdiff-aarch64-apple-darwin.tar.gz | tar xz
sudo mv dbdiff /usr/local/bin/
From source:
git clone https://github.com/rekurt/dbdiff.git
cd dbdiff
cargo build --release
# Binary is at ./target/release/dbdiff
dbdiff postgres://user:pass@prod-host/myapp \
postgres://user:pass@staging-host/myapp
dbdiff postgres://user:pass@prod-host/myapp --schema ./schema.sql
Useful during code review — verify that a migration file actually matches what's in production.
dbdiff mysql://user:pass@prod-host/myapp mysql://user:pass@staging-host/myapp
dbdiff myapp.db --schema ./schema.sql
dbdiff postgres://prod/myapp postgres://staging/myapp \
--out migration.sql
dbdiff postgres://prod/myapp postgres://staging/myapp --ci
Returns exit code 0 if schemas match, 1 if they differ. Use this in GitHub Actions, GitLab CI, or any pipeline.
dbdiff postgres://prod/myapp postgres://staging/myapp --config ./my-config.yml
dbdiff postgres://prod/myapp postgres://staging/myapp --format json
Add schema drift detection to every PR:
# .github/workflows/schema-check.yml
name: Schema check
on: [pull_request]
jobs:
schema-drift:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install dbdiff
run: |
curl -sSL https://github.com/rekurt/dbdiff/releases/latest/download/dbdiff-x86_64-unknown-linux-musl.tar.gz | tar xz
sudo mv dbdiff /usr/local/bin/
- name: Check for schema drift
env:
PROD_DSN: ${{ secrets.PROD_DSN }}
STAGING_DSN: ${{ secrets.STAGING_DSN }}
run: dbdiff "$PROD_DSN" "$STAGING_DSN" --ci
| Object | Added | Removed | Modified |
|---|---|---|---|
| Tables | ✓ | ✓ | — |
| Columns | ✓ | ✓ | ✓ |
| Column types | — | — | ✓ |
| Indexes | ✓ | ✓ | ✓ |
| Unique constraints | ✓ | ✓ | — |
Some ALTER operations lock the table on Postgres. dbdiff marks them explicitly:
⚠ ALTER TABLE orders ADD COLUMN paid_at timestamptz NOT NULL
This operation will rewrite the table and acquire AccessExclusiveLock.
Consider: ADD COLUMN ... DEFAULT NULL first, then backfill.
Create .dbdiff.yml in your project root:
# .dbdiff.yml
ignore:
tables:
- _migrations
- schema_version
columns:
- "*.created_at" # ignore created_at in all tables
- "sessions.*" # ignore all columns in sessions table
output:
format: pretty # pretty | json | sql
color: true
| Database | Status | Version |
|---|---|---|
| PostgreSQL | ✅ stable | 12+ |
| MySQL / MariaDB | ✅ stable | 8.0+ |
| SQLite | ✅ stable | 3.x |
Database backends are optional and can be toggled via Cargo features:
# Install with only PostgreSQL support
cargo install dbdiff --no-default-features --features postgres
# Install with PostgreSQL and SQLite only
cargo install dbdiff --no-default-features --features postgres,sqlite
All backends (postgres, mysql, sqlite) are enabled by default.
git clone https://github.com/rekurt/dbdiff
cd dbdiff
cargo build
# Run tests
cargo test
# Run with local SQL files
cargo run -- tests/fixtures/schema_a.sql --schema tests/fixtures/schema_b.sql
# Run with a local Postgres
docker run -d -p 5432:5432 -e POSTGRES_PASSWORD=pass postgres:16
cargo run -- postgres://postgres:pass@localhost/myapp --schema tests/fixtures/schema_b.sql
src/
main.rs CLI entry point
lib.rs Library root
cli.rs Argument parsing (clap)
model.rs Schema / Table / Column / Index structs
error.rs Error types
diff.rs Schema comparison engine
migration.rs SQL migration generator
output.rs Terminal rendering (colored diff)
config/
mod.rs Config file parsing (.dbdiff.yml)
filter.rs Schema filtering (ignore tables/columns)
loader/
mod.rs Source dispatch logic
postgres.rs PostgreSQL introspection
mysql.rs MySQL / MariaDB introspection
sqlite.rs SQLite introspection
sqlfile.rs .sql file parser
tests/
cli.rs Integration tests
config.rs Config integration tests
fixtures/ SQL test schemas + config fixtures
Issues and PRs are welcome. Please open an issue before working on a large change.
For a new database driver, see src/loader/postgres.rs — implement a load function returning Schema and add detection logic in src/loader/mod.rs.
See CONTRIBUTING.md for full details.
MIT © Nikita
dbdiff
03 / All projects
Libraries, command-line tools and applications.