Skip to main content
The extract command connects to a PostgreSQL database and exports its complete schema to a JSON file. This JSON representation serves as the “current state” for schema comparison.

Usage

Flags

Examples

Basic Extraction

Output to stdout

Increase Extraction Timeout

extract has a 5 minute default timeout for its database operations. Increase it for large schemas:
Use --timeout 0 to disable pgtofu’s command timeout for extract.

Exclude Additional Schemas

Docker

Connection URL Format

The PostgreSQL connection URL follows this format:

Examples

connect_timeout only controls connection establishment at the PostgreSQL driver level. Use --timeout to control pgtofu’s overall deadline for the database operations in extract, including connection checks and schema extraction.

SSL Modes

Schema Exclusion

Automatically Excluded Schemas

pgtofu automatically excludes system and internal schemas:

Excluding Third-Party Schemas

Use --exclude-schema to exclude schemas from third-party tools:

Output Format

The extracted schema is a JSON object containing all database objects:

What Gets Extracted

  • Column definitions (types, nullability, defaults)
  • Primary keys and foreign keys
  • Unique constraints and check constraints
  • Exclusion constraints
  • Indexes (including partial and covering indexes)
  • Table comments
  • Partition information (for partitioned tables)
  • Regular views with definitions
  • Materialized views with refresh settings
  • View indexes (for materialized views)
  • View comments
  • Function signatures (name, arguments, return type)
  • Function bodies (preserving original language)
  • Volatility settings (STABLE, VOLATILE, IMMUTABLE)
  • Security settings (DEFINER vs INVOKER)
  • Triggers with timing and events
  • Enum types with values
  • Composite types with fields
  • Domain types with constraints
  • Sequences with start/increment values
  • Hypertables with dimensions
  • Compression policies and settings
  • Retention policies
  • Continuous aggregates
  • Refresh policies

Performance Considerations

  • Extraction queries the PostgreSQL system catalogs directly
  • Large databases (1000+ tables) may take 30-60 seconds or longer, depending on catalog size, indexes, partitions, functions, and extensions
  • pgtofu enriches each table with columns, constraints, indexes, and partition metadata, so large schemas may need a longer --timeout
  • Consider using --output - with streaming for very large schemas

Troubleshooting

Ensure the database is running and accessible:
The user needs read access to system catalogs. Grant if needed:
Try different SSL modes:
The default extraction timeout is 5 minutes. Increase it when extracting large schemas:
If the error names a table or extraction step, check whether that object has unusually large catalog metadata, many indexes, many partitions, or blocking catalog locks. You can also run psql "$DATABASE_URL" -c "SELECT * FROM pg_stat_activity WHERE state <> 'idle';" while extraction is running to look for waits.

See Also

  • diff - Compare extracted schema with desired schema
  • generate - Generate migrations from schema differences