> ## Documentation Index
> Fetch the complete documentation index at: https://pgtofu.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Introduction

> Declarative PostgreSQL schema management for modern applications

**pgtofu** is a state-based database migration tool that enables declarative schema management for PostgreSQL and TimescaleDB. Define your desired schema in SQL files, and pgtofu automatically generates safe, versioned migration files compatible with [golang-migrate](https://github.com/golang-migrate/migrate).

> [card] [Quickstart](https://pgtofu.com/quickstart.md)
>
> Get up and running with pgtofu in 5 minutes

> [card] [Installation](https://pgtofu.com/installation.md)
>
> Install pgtofu via Docker or from source

> [card] [CLI Reference](https://pgtofu.com/cli/overview.md)
>
> Complete reference for all CLI commands

> [card] [How It Works](https://pgtofu.com/concepts/how-it-works.md)
>
> Deep dive into how pgtofu works

## The Problem

Traditional migration tools require manually writing incremental migration files. This approach has several drawbacks:

> [accordion] Hard to understand current state
>
> With dozens or hundreds of migration files, understanding what your current schema looks like requires mentally replaying all migrations or examining the live database.

> [accordion] Easy to introduce inconsistencies
>
> Manual migration writing is error-prone. Forgetting to update a foreign key, missing an index, or using inconsistent naming conventions is common.

> [accordion] Requires tracking dependencies manually
>
> When adding a foreign key to a table that doesn't exist yet, you need to manually ensure the referenced table is created first.

> [accordion] Difficult to resolve conflicts
>
> When multiple developers modify the schema simultaneously, resolving merge conflicts in migration files is tedious and risky.

## The Solution

pgtofu uses a **declarative approach**:

**1. Extract**

Extract the current database schema into a JSON representation

**2. Define**

Define your desired schema in organized SQL files

**3. Compare**

Compare current vs. desired schema to detect differences

**4. Generate**

Generate safe, ordered migration files automatically

**5. Apply**

Apply migrations using golang-migrate or any compatible tool

## Key Features

> [card] Full PostgreSQL Support
>
> Tables, indexes, views, functions, triggers, constraints, sequences, custom types, and more

> [card] TimescaleDB Native
>
> First-class support for hypertables, compression policies, retention policies, and continuous aggregates

> [card] Smart Diff Engine
>
> 40+ change types with severity classification (SAFE, POTENTIALLY\_BREAKING, BREAKING, DATA\_MIGRATION\_REQUIRED)

> [card] Dependency Resolution
>
> Topological sorting ensures correct execution order for all schema changes

> [card] Safe Migrations
>
> Breaking change detection, idempotent DDL, and transaction control

> [card] golang-migrate Compatible
>
> Drop-in replacement for existing migration workflows

## Design Principles

1. **Correctness First** - Never lose data; always generate safe migrations
2. **TimescaleDB Native** - First-class support for all TimescaleDB features
3. **Declarative** - Define desired state; the tool figures out how to get there
4. **golang-migrate Compatible** - Works seamlessly with existing migration workflows
5. **Production Ready** - Comprehensive testing, error handling, and validation

## How It Works

```mermaid theme={null}
flowchart TB
    DB[(PostgreSQL/TimescaleDB<br/>Current State)]
    DB -->|Extractor| JSON1[JSON Schema<br/>Current]

    SQL[SQL Files<br/>Desired] -->|Parser| JSON2[JSON Schema<br/>Desired]

    JSON1 --> Differ
    JSON2 --> Differ

    Differ --> Changes[Ordered Changes<br/>with Dependencies]
    Changes -->|Generator| Migrations[Migration Files .sql<br/>golang-migrate compatible]
```

## Next Steps

> [card] [Quickstart Guide](https://pgtofu.com/quickstart.md)
>
> Follow a step-by-step tutorial to understand the basic workflow

> [card] [Core Concepts](https://pgtofu.com/concepts/overview.md)
>
> Learn about declarative migrations and how pgtofu processes schemas


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.