Skip to main content
For large schemas and teams, managing your database schema as a single file becomes unwieldy. The dump --multi-file option allows you to split your schema into modular files organized by database objects, enabling better collaboration, clearer ownership, and easier maintenance. This approach is ideal for:
  • Large schemas with hundreds of tables, views, and functions
  • Teams where different developers/teams own different parts of the schema
  • Schemas that need granular code review processes

Workflow

Step 1: Initialize Multi-File Structure

Set up your schema directory structure and dump your existing schema into multiple files:
Each database object is now in its own file, organized by object type for easy navigation and maintenance.

Step 2: Development Workflow

Use the multi-file structure to define the desired state of your schema components:
Each team defines the desired state of their schema components, and pgschema generates the necessary migration steps.

Step 3: Team Collaboration

Teams work on separate branches and test independently before combining changes for production deployment:
Each team validates their changes independently in test environments, then combines into a coordinated production deployment.

Step 4: Production Deployment

Deploy schema changes safely to production using the established multi-file workflow:

Ordering Requirements

pgschema applies your schema files in the order of their \i directives (and alphabetically within folder includes) to build the desired state. Most objects resolve regardless of order, but some objects depend on others existing at creation time. When a dependency is created after the object that references it, you’ll see an error such as:
The most common dependency that requires ordering is a function whose return type or arguments reference a table’s row type:
Here the recipe_user_save table must be included before the function. Other cases that require the referenced object first include %ROWTYPE parameters, table- or composite-typed arguments, and domains used in table columns.
Schema qualification (public.recipe_user_save) does not affect this — pgschema strips the target-schema prefix internally. Only the include order matters.
The default dump --multi-file output already orders directives correctly (types → tables → views → functions → indexes), so this only comes up when you reorganize files yourself. If you use Custom Organization, make sure tables (and any other referenced objects) are included before the functions, views, and procedures that depend on them:

File Organization Strategies

By Object Type (Default)

pgschema generates files organized by database object type:

Custom Organization

You can reorganize files into any structure you prefer and update the \i directives in main.sql:

By Business Domain

Update main.sql to match your structure:

Hybrid Approach

The key is ensuring your main.sql file contains the correct \i directives that match your chosen file organization.

Folder Includes

The \i directive supports including entire folders by adding a trailing slash (/). This automatically includes all .sql files in the folder in alphabetical order, and recursively processes any subdirectories:
Using folder includes in main.sql:
Key behaviors:
  • Files are processed in alphabetical order by filename
  • Subdirectories are processed recursively using depth-first search
  • Only .sql files are included; other files are ignored
  • Folder paths must end with / to be recognized as folders
  • Error if folder doesn’t exist or if you try to include a file as a folder

Nested Includes

The \i directive can be nested, allowing for hierarchical file organization:
With nested includes:
You can also combine folder includes with nested approaches:
This approach allows for modular organization where each subsystem manages its own includes.