Skip to main content
This guide walks you through the basic pgtofu workflow: extracting your current schema, defining your desired schema, and generating migrations.

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:

Step 2: Set Up Your Database Connection

Never commit database credentials to version control. Use environment variables or a secrets manager.

Step 3: Extract Current Schema

Extract your database’s current schema to a JSON file:
This creates a current-schema.json file containing a complete representation of your database schema.

Step 4: Define Your Desired Schema

Create a schema directory and define your desired schema using standard SQL files:
Create schema/tables/users.sql:
Create schema/tables/orders.sql:
Organize your SQL files by type (tables, views, functions, etc.) for maintainability. pgtofu will automatically resolve dependencies regardless of file organization.

Step 5: Preview Changes

Compare your current schema with the desired schema to see what changes will be made:
Example output:

Step 6: Generate Migrations

Generate migration files that can be applied with golang-migrate:
This creates migration files in the 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
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:
  1. Update your SQL files in schema/
  2. Re-extract the current schema: pgtofu extract --output current-schema.json
  3. Preview changes: pgtofu diff --current current-schema.json --desired ./schema
  4. Generate new migrations: pgtofu generate --current current-schema.json --desired ./schema
  5. Apply: migrate -path ./migrations -database "$DATABASE_URL" up
Use --preview with the generate command to see what migrations would be created without writing files.

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