Prerequisites
- A running PostgreSQL database (local or remote)
- Docker installed (or Go 1.25+ for building from source)
- golang-migrate for applying migrations
Step 1: Install pgtofu
The quickest way to get started is with Docker:Alternative: Install from source
Alternative: Install from source
Step 2: Set Up Your Database Connection
Step 3: Extract Current Schema
Extract your database’s current schema to a JSON file:- Docker
- Binary
current-schema.json file containing a complete representation of your database schema.
Step 4: Define Your Desired Schema
Create aschema directory and define your desired schema using standard SQL files:
schema/tables/users.sql:
schema/tables/orders.sql:
Step 5: Preview Changes
Compare your current schema with the desired schema to see what changes will be made:- Docker
- Binary
Step 6: Generate Migrations
Generate migration files that can be applied with golang-migrate:- Docker
- Binary
migrations directory:
migrations
000001_add_users_table.up.sql
000001_add_users_table.down.sql
000002_add_orders_table.up.sql
000002_add_orders_table.down.sql
View generated migration content
View generated migration content
000001_add_users_table.up.sql:
Step 7: Apply Migrations
Use golang-migrate to apply the generated migrations:Step 8: Iterate
As your application evolves, repeat the workflow:- Update your SQL files in
schema/ - Re-extract the current schema:
pgtofu extract --output current-schema.json - Preview changes:
pgtofu diff --current current-schema.json --desired ./schema - Generate new migrations:
pgtofu generate --current current-schema.json --desired ./schema - Apply:
migrate -path ./migrations -database "$DATABASE_URL" up
Next Steps
Installation
Learn about all installation options including Docker and building from source
Core Concepts
Understand how pgtofu’s declarative approach works under the hood
CLI Reference
Complete reference for all CLI commands and options
TimescaleDB
Learn how to use pgtofu with TimescaleDB hypertables and policies