Staging and production schemas no longer match

Short answerCompare the two schemas side by side and turn the differences into one reviewed script. DbSchema Pro does this with Schema Synchronization: it lists every difference between a model and a live database and generates the SQL to align them. Liquibase's diff command compares two databases from the command line. Free basics: schema-only dumps, information_schema queries and no direct DDL rights on production.

01

Compare the two schemas with Schema Synchronization in DbSchema Pro

DbSchema Pro keeps the schema as a design model in a .dbs file. Reverse-engineer staging into a DbSchema model, save it, and connect DbSchema to production: Schema Synchronization in DbSchema lists every difference between the model and the database, and for each one you choose which side wins. Generate SQL script writes the ALTER statements without running them, and Export Report saves the differences as HTML or Markdown for the release. You can also compare two saved .dbs files, one per environment, without touching a database.

Good if you need to see every difference and get the SQL that aligns the two

Skip if every change already goes through migration scripts and only the order is off

Schema Synchronization · Pricing and editions

Tool
Cost€€
EffortLow
HelpsToday
02

Run a diff between the two databases with Liquibase

The diff command in Liquibase compares two databases of the same type or of different types. You point it at a reference database, here staging, and at the target, here production, and it reports what differs. The Liquibase docs name detecting drift between a model schema and a database's actual schema as one of its uses. Liquibase Community is licensed under the Functional Source License.

Good if you want a diff you can run from the command line or a pipeline

Liquibase · GitHub

Tool
CostFree
EffortMedium
HelpsThis week
03

Route every change through versioned migrations

With Flyway, each schema change is an SQL script with a version number that you keep in version control, and the same scripts run in the same order on each environment. Once staging and production get their changes only this way, they stop drifting. Flyway's code is open source under the Apache 2.0 licence.

Good if you deploy the same schema to several environments

Redgate Flyway · GitHub

Tool
CostFree
EffortMedium
HelpsThis month
04

Bring in a DBA to reconcile the environments

If nobody knows which environment is right, book a DBA or a database consultancy that knows your engine. Ask them to agree with your team which schema is the target and to review the script that aligns the other one. The PostgreSQL project lists companies that offer professional services, by region.

Good if production depends on the database and the differences are large

PostgreSQL

Expert
Cost€€€
EffortMedium
HelpsThis month
05

Dump the schema of both environments and diff the files

On PostgreSQL, pg_dump with --schema-only writes only the object definitions, not the data. Run it once against staging and once against production, then compare the two files with git diff --no-index, which compares two paths on the filesystem. Every line that differs is a place where the schemas drifted.

Good if you want a first look today with tools you already have

PostgreSQL · Git

Do it yourself
CostFree
EffortLow
HelpsToday
06

Query information_schema on both sides and compare

The information schema is defined in the SQL standard, so the same query works across databases. In PostgreSQL, information_schema.columns lists every table column with its data type and whether it is nullable; SQL Server has information schema views too. Run the same query on staging and production, export both results and compare them with a script or a spreadsheet.

Good if you only care about tables and columns

Skip if you also need indexes, triggers and functions

The Information Schema · Information Schema, columns · Microsoft Learn

Do it yourself
CostFree
EffortMedium
HelpsToday
07

Take away direct DDL rights on production

Drift starts when people change production by hand. In PostgreSQL, the docs show how to revoke the right to create objects in the public schema, REVOKE CREATE ON SCHEMA public FROM PUBLIC; grant that right only to the user that runs your migrations. Then a schema change reaches production only through the reviewed path.

Good if people can still change the production schema directly

PostgreSQL

Do it yourself
CostFree
EffortLow
HelpsToday

When it's bigger than a quick fix

Bring in a DBA or a database consultancy when nobody knows which environment holds the right schema, when the differences touch large or critical tables, or when the aligning script would drop columns or tables.

Back up production before you run any script that aligns the schemas, and check that you can restore it. The PostgreSQL docs say databases with valuable data should be backed up regularly.

Backup and Restore · Professional Services

Questions people ask

What is schema drift?

Schema drift means two environments that should have the same schema, such as staging and production, no longer do, usually because changes were made in one and not the other.

How do I compare two database schemas?

Dump the schema of both with a schema-only export and diff the files, query information_schema on both sides, or use a tool that compares two databases and lists the differences.

Does a schema-only dump include the data?

No. pg_dump with --schema-only writes only the object definitions, without the data.

How do I stop staging and production from drifting apart?

Let every change reach both environments only through versioned migrations that run in the same order, and take away direct DDL rights on production.

Should I back up before aligning the schemas?

Yes. Back up production before you run the aligning script and check that you can restore it.

Sources

  1. DbSchema: Schema Synchronization
  2. DbSchema: Pricing and editions
  3. Liquibase: diff
  4. GitHub: liquibase/liquibase
  5. Redgate Flyway: Migrations
  6. GitHub: flyway/flyway
  7. PostgreSQL: Professional Services
  8. PostgreSQL: pg_dump
  9. Git: git-diff Documentation
  10. PostgreSQL: The Information Schema
  11. PostgreSQL: Information Schema, columns
  12. Microsoft Learn: System information schema views (Transact-SQL)
  13. PostgreSQL: Schemas
  14. PostgreSQL: Backup and Restore