How to version control config stored in Postgres

If your app reads config from Postgres (pricing rules, feature flags, prompts, routing), people edit those rows live. When something breaks there is no diff, no history and no undo.

This guide adds all three with cfgit, an open-source CLI. The rows stay in your table and your app keeps reading them the same way. It takes about ten minutes. Every output below is from a real run of cfgit 0.6.5 against Postgres 16.

Install cfgit

cfgit is a Python CLI. It needs Python 3.11 or newer.

pip install 'cfgit[postgres]'

Keep each record in a jsonb column

cfgit versions one doc jsonb column that holds the whole record, next to an id column. Scalar columns you filter on, like is_active, stay; cfgit keeps them in sync from the document on every write. If your config lives in separate columns today, this is the one change cfgit asks for.

CREATE TABLE pricing_rules (
  rule_id   text PRIMARY KEY,
  is_active boolean NOT NULL DEFAULT true,
  doc       jsonb NOT NULL
);

This guide uses two rows: a pro_monthly plan at $29 with a 14-day trial, and a team_annual plan.

Point it at your table

Create a .cfg.toml next to your app. live_when tells cfgit which rows count as live. The connection string comes from an environment variable, never the file.

# .cfg.toml
[project]
name = "billing"

[[collection]]
name = "pricing_rules"
id_field = "rule_id"
live_when = { is_active = true }

[env.dev]
database = "postgres"
uri = "env:DEV_POSTGRES_URI"
db = "postgres"
export DEV_POSTGRES_URI='postgresql://user:password@localhost:5432/postgres'

Record what is live today

cfg init creates cfgit's history and heads tables next to yours and reports that writes are transactional. cfg doctor checks for secrets and oversized fields. cfg import stores every live row as version 1. Your app keeps reading the same table.

$ cfg init
atomic:
  {
    "atomic": true,
    ...
  }

$ cfg doctor
doctor: 2 live record(s) scanned, no blockers. Safe to import.

$ cfg import --all -m "initial import"
pricing_rules:pro_monthly imported
pricing_rules:team_annual imported

$ cfg status
pricing_rules:pro_monthly clean
pricing_rules:team_annual clean

Preview a change, then make it

cfg set edits fields in place. --dry-run shows the exact field-level change and writes nothing.

$ cfg set pricing_rules:pro_monthly price_usd=35 trial_days=7 --dry-run
pricing_rules:pro_monthly: would commit (dry run)
  change price_usd
    before: 29
    after:  35
  change trial_days
    before: 14
    after:  7
Dry run: this is the field-level delta cfgit would write. Nothing was written.

$ cfg set pricing_rules:pro_monthly price_usd=35 trial_days=7 -m "raise pro to 35, shorter trial"
pricing_rules:pro_monthly: committed @2  fad3769197b3

Read the history

Every version keeps the full document, a content hash, the author and the message. cfg log shows what changed in each one.

$ cfg log pricing_rules:pro_monthly
pricing_rules:pro_monthly @2 fad3769197b3
  change price_usd
    before: 29
    after:  35
  change trial_days
    before: 14
    after:  7
pricing_rules:pro_monthly @1 622738e77f26
  add price_usd
    after:  29
  add trial_days
    after:  14
  ...

Roll back

cfg restore puts a row back to any earlier version. The rollback is itself a new version (@3 here), so nothing in the history is lost.

$ cfg restore pricing_rules:pro_monthly @1 -m "back to 29 while we test"
pricing_rules:pro_monthly: restored @3  622738e77f26

Catch edits made straight in SQL

Someone changes the team plan from an admin panel, which runs plain SQL:

UPDATE pricing_rules SET doc = jsonb_set(doc, '{seats}', '10') WHERE rule_id = 'team_annual';

cfgit does not block that. It notices it, shows exactly what changed, and refuses to write over it until someone decides:

$ cfg status
pricing_rules:pro_monthly clean
pricing_rules:team_annual changed_outside_cfgit

$ cfg diff pricing_rules:team_annual =HEAD =live
change seats
  before: 20
  after:  10

$ cfg set pricing_rules:team_annual price_usd=300 -m "annual to 300"
pricing_rules:team_annual: changed outside cfgit
The live record differs from what cfgit last recorded: something wrote it
outside cfgit.
→ Look at the drift. Keep it with adopt, or throw it away with restore
  --discard-drift (the discarded doc stays in history). Then retry.

$ cfg adopt pricing_rules:team_annual -m "keep the 10-seat change from the admin panel"
pricing_rules:team_annual: adopted @2  eb932b3dc53d

More on this in detecting config drift.

Let your coding agent do it

cfgit ships an MCP server, so Claude Code, Codex or Cursor make the same changes through the same previews and drift checks. In Claude Code:

claude plugin install cfgit --marketplace AusafMo/cfgit

It is also on the official MCP Registry as io.github.AusafMo/cfgit. See safe database writes for coding agents for what the agent sees.

Next

The cfgit guide covers branches and pull requests for config, promotion from dev to prod, validation and the safety model. Using MongoDB? The same flow is in the MongoDB guide. Weighing other options? See how cfgit compares.