DEV Community

A Better Way to Store PostgreSQL Schemas in Git

Stop Committing 50,000-Line PostgreSQL Schema Files to Git

If you've ever version-controlled a PostgreSQL database, you've probably run into the same problem. You make a small schema change-a new column, an updated function, or a new index-and suddenly Git shows thousands of changed lines. Reviewing those changes becomes frustrating, merge conflicts become more common, and understanding what actually changed takes far longer than it should.

The problem isn't PostgreSQL. It's that pg_dump wasn't designed to produce Git-friendly output.

The Problem

A typical schema dump looks like this:

schema.sql
└── 58,000+ lines

Everything is stored in a single file. Even small changes can generate huge diffs because objects move around or dependencies affect the output. Code reviews become difficult because reviewers have to search through thousands of lines just to find the actual change.

What I Wanted Instead

I wanted a schema that behaved more like source code. Instead of one massive SQL file, every database object should have its own file. Something like this:

db-schema/
├── tables/
│   ├── public.users.sql
│   ├── public.orders.sql
│   └── ...
├── views/
├── functions/
├── triggers/
├── indexes/
├── constraints/
├── policies/
├── types/
├── domains/
└── deploy.sql

Now a Git diff only shows the objects that actually changed. If you modify one function, only one file changes. If you add a table, Git shows one new file. Exactly like reviewing application code.

Introducing PgSchemaExporter

So I built PgSchemaExporter. It's an open-source tool that exports PostgreSQL schemas into a structured, Git-friendly directory layout.

Features

  • One SQL file per database object
  • Clean Git diffs
  • Automatic dependency ordering
  • Export directly from a live PostgreSQL database
  • Split an existing pg_dump into individual object files
  • Generate a deploy.sql script
  • Compare two schema versions with a built-in diff command

Currently supported objects include:

  • Tables
  • Views
  • Functions
  • Procedures
  • Sequences
  • Indexes
  • Constraints
  • Triggers
  • Policies
  • Types
  • Domains
  • Extensions
  • Schemas
  • Comments
  • Grants
  • and more

Why I Built It

Many teams already use migration tools such as Flyway, Liquibase, or Entity Framework migrations. Those tools are excellent for deploying changes. PgSchemaExporter solves a different problem. It helps when you want to:

  • understand an existing database
  • audit a production schema
  • review database changes in Git
  • document legacy databases
  • compare two schema versions
  • keep the actual deployed schema under version control

In other words, it's focused on the current state of the database rather than its migration history.

Example

Instead of reviewing this:

schema.sql
58,421 lines changed

you review something like this:

functions/calculate_order_total.sql
tables/public.orders.sql

The change is immediately obvious.

Open Source

PgSchemaExporter is written in C#, runs on Windows, Linux, and macOS, and is released under the MIT license. The project is available on GitHub:

https://github.com/RomanShevel1977/PgSchemaExporter

I'd really appreciate any feedback, ideas, feature requests, or contributions. If you've ever struggled with reviewing PostgreSQL schema changes in Git, I'd love to hear how your team solves this problem.

Comments

No comments yet. Start the discussion.