brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: pg_dump -s dumps only the PostgreSQL schema so you can diff two servers

Postgresql
~/tips/tip-postgresql-pg-dump-schema-only

pg_dump -s -f prod-schema.sql -h db1.example.com appdb

Writes table, index and function definitions without any rows, ready to compare or keep in git.

When staging behaves differently from production, the cause is often a missing index or a column with another type. Comparing the definitions is quick when you dump them without data.

pg_dump -s -f prod-schema.sql -h db1.example.com appdb
pg_dump -s -f stage-schema.sql -h db2.example.com appdb
diff -u prod-schema.sql stage-schema.sql

-s (long form --schema-only) writes tables, indexes, constraints, views, functions and triggers, and no rows. Even for a big database the file is small and done in seconds. pg_dump writes objects in a stable order, so two dumps of identical schemas diff clean.

The name is confusing: -n, long form --schema, is a different option that picks a PostgreSQL schema such as public. The docs warn about mixing those two up.

A diff can still show noise. Different pg_dump versions write slightly different SQL, so use the same client version for both sides. Owners and grants differ between environments more often than I’d like; -O and -x leave out ownership and privileges when you only care about structure. Sequences show their definition, not their current value.

Further reading: pg_dump

More Postgresql tips FROM pg_settings FROM pg_hba_file_rules LOAD 'auto_explain'; CREATE EXTENSION pgstattuple; pg_dump -O -x -f mydb.sql mydb All Postgresql tips (24) All topics

Link