Skip to main content

Tables

Basic Tables

All Column Types

pgtofu supports all PostgreSQL built-in types:

Identity Columns

Modern alternative to SERIAL (PostgreSQL 10+):

Generated Columns

Computed columns (PostgreSQL 12+):

Row-Level Security

Declare row-level security after the table definition, or in a file under policies/. pgtofu reads policy files after the other schema files.
This policy filters reads and checks writes against the transaction’s organization setting. Set it with SELECT set_config('app.organization_id', 'your-organization', true) inside the application transaction. An absent setting admits no rows. pgtofu supports ENABLE, DISABLE, FORCE, and NO FORCE ROW LEVEL SECURITY as independent table attributes. Table owners normally bypass policies; FORCE also applies them to the owner. Superusers and roles with BYPASSRLS still bypass policies. PostgreSQL row security Policies support AS PERMISSIVE or AS RESTRICTIVE, all five commands (ALL, SELECT, INSERT, UPDATE, DELETE), explicit role lists, USING, WITH CHECK, and comments. The defaults are PERMISSIVE, ALL, and PUBLIC. INSERT accepts only WITH CHECK; SELECT and DELETE accept only USING. For ALL and UPDATE, an omitted WITH CHECK uses the USING expression. PostgreSQL policy syntax Edit or remove the desired CREATE POLICY declaration to change or drop a policy. Policy definition changes use transactional drop and recreation, so clauses can be removed and the command or mode can change. Comment changes use COMMENT ON POLICY without replacing the policy. RLS and policy definition changes are classified as POTENTIALLY_BREAKING because they can change access. pgtofu extracts policy definitions from pg_policy and compares them after PostgreSQL deparsing. Policy creation follows its table, columns, referenced relations, functions, and types. Policies are removed before those dependencies are dropped or their referenced column types change, then restored in the correct order on rollback. Type drops refuse unexpected dependencies instead of cascading into policies. A temporary removal and recreation stays in one migration even when the operation limit is smaller. Keep the default transaction mode to apply these changes atomically. Roles and grants remain authored migrations. Use explicit role names or PUBLIC; CURRENT_ROLE, CURRENT_USER, and SESSION_USER cannot be resolved from schema files and are rejected. Declare flags separately from other ALTER TABLE actions. Policies on partition parents are supported; independent policies and flags on individual partition children are not modeled. Extract a fresh current schema after upgrading pgtofu to include its RLS metadata. Older JSON snapshots omit the table flags and policies and therefore represent disabled row security with no policies.

Constraints

Primary Key

Foreign Keys

Referential Actions:
  • ON DELETE CASCADE - Delete dependent rows
  • ON DELETE SET NULL - Set FK to NULL
  • ON DELETE SET DEFAULT - Set FK to default
  • ON DELETE RESTRICT - Prevent deletion (default)
  • ON DELETE NO ACTION - Similar to RESTRICT

Unique Constraints

Check Constraints

Exclusion Constraints

Deferrable Constraints

Indexes

Basic Indexes

Index Types

Partial Indexes

Covering Indexes (PostgreSQL 11+)

Expression Indexes

Views

Regular Views

Materialized Views

Functions

PL/pgSQL Functions

SQL Functions

Function Attributes

Triggers

Row-Level Triggers

Statement-Level Triggers

Conditional Triggers

Constraint Triggers

Constraint-trigger identity, deferrability, initial timing, and optional FROM relation are preserved during extraction, diffing, and migration generation.

Custom Types

Enum Types

Composite Types

Domain Types

Extensions

Sequences

Comments

See Also