Declarative Postgres Schema with pgschema
I got tired of numbered migration files. Typical workflows mean you write the SQL to get from A to B by hand, keep those files forever, and hope nobody applied step 17 twice. I wanted the Infrastructure as Code (IaC) loop for Postgres: declare the desired schema, preview the diff, apply it.
I think the IaC style of database schemas makes it easier for both humans and agents to know what is going on in a database. I still keep migration files for records, rollbacks, or whatever - but they are not the core source of truth. Migration files really bog up LLM context and human context.
pgschema is the tool. Desired state lives in a .sql file. It diffs that against the live database and generates the DDL. The workflow is dump → edit → plan → apply, the same shape as Terraform.
pgschema is Postgres-only - it understands functions, views, RLS, partitions, and the other objects. Other tools exist for other databases, such as Atlas. Applying functions or views there requires a premium subscription.
Also, it's worth noting the Apache 2.0 license!
Setup
Installation is described in the repo. Easiest is Go or Homebrew:
go install github.com/pgplex/pgschema@latest
# or
brew install pgschema
Testing it out
Create a postgres
services:
postgres:
image: postgres:17-alpine
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: postgres
ports:
- "5432:5432"
volumes:
- postgres_data:/var/lib/postgresql/data
volumes:
postgres_data:
docker compose up -d
Define the schema
Desired schema is just SQL. Tables, a function, and a view that uses the function - the objects other tools often treat as extras:
CREATE TABLE users (
id SERIAL PRIMARY KEY,
email VARCHAR(255) UNIQUE NOT NULL,
created_at TIMESTAMP DEFAULT NOW()
);
CREATE TABLE posts (
id SERIAL PRIMARY KEY,
user_id INTEGER REFERENCES users(id),
title VARCHAR(200) NOT NULL,
content TEXT,
created_at TIMESTAMP DEFAULT NOW()
);
CREATE FUNCTION user_post_count(target_user_id INTEGER)
RETURNS INTEGER
LANGUAGE sql
STABLE
AS $$
SELECT COUNT(*)::INTEGER
FROM posts
WHERE posts.user_id = target_user_id;
$$;
CREATE VIEW user_post_summaries AS
SELECT
users.id,
users.email,
user_post_count(users.id) AS post_count
FROM users;
Save that as schema/public.sql. Against an empty database, plan shows everything it will create: both tables, the function, the view, and the foreign key.
Plan and apply with pgschema
export PGPASSWORD=postgres
pgschema plan \
--host 127.0.0.1 \
--port "$(docker compose port postgres 5432 | cut -d: -f2)" \
--db postgres \
--user postgres \
--schema public \
--file schema/public.sql \
--output-human stdout
Apply it the same way (pgschema apply ... --file schema/public.sql --auto-approve). Then pgschema dump prints the live schema as SQL. It will not match your file character-for-character - pgschema normalizes (IF NOT EXISTS, constraints pulled out of the CREATE TABLE). That is expected.
The .sql file is the source of truth.
Makefile
Instead of setting all the CLI variables for each call, I personally enjoy just making a simple little Makefile to run and modify commands from.
POSTGRES_HOST_PORT := 5432
export PGPASSWORD := postgres
db:
docker compose up -d
db-down:
docker compose down
plan:
pgschema plan --host 127.0.0.1 --port $(POSTGRES_HOST_PORT) --db postgres --user postgres --schema public --file schema/public.sql --output-json plan.json --output-human stdout
apply:
pgschema apply --host 127.0.0.1 --port $(POSTGRES_HOST_PORT) --db postgres --user postgres --schema public --file schema/public.sql --auto-approve
dump:
pgschema dump --host 127.0.0.1 --port $(POSTGRES_HOST_PORT) --db postgres --user postgres --schema public
plan.json is the machine-readable plan, not the SQL. The SQL is the human stdout from make plan.
I use pgtui to look at the live tables and the user_post_summaries view afterward. Same Go/Homebrew install path as pgschema if you want it.
Comments
No account required. Open the Sign In menu and choose Anonymous to comment with a name.