Databases 4 min read 748 words

minipg: PostgreSQL in a directory you carry around

ES
minipg: PostgreSQL in a directory you carry around

Snails don’t have to look for a house or ask anyone’s permission to move: they carry it with them, and wherever they stop, they’re at home. It’s a very comfortable way to live, as long as you don’t mind going slowly.

Databases are just the opposite. SQLite and DuckDB are a file you copy and take wherever you like, but PostgreSQL is a house with foundations: you have to install it, start the server, create users, remember the port… or, as almost all of us do, spin up a Docker container and forget about it. That’s why minipg made me smile. It’s by Jeff Lindsay (progrium, of Dokku fame), and it turns Postgres into a single executable and each database into a directory.

How it’s used

The idea is that a .pgdb directory is the database: inside go the data, the configuration, the automatically generated credentials and even the Postgres version. And using it is the kind of thing you learn in a minute:

minipg app.pgdb                     # psql shell (creates app.pgdb if missing)
minipg app.pgdb "select 1"          # one statement
minipg url app.pgdb                 # postgres://postgres:...@127.0.0.1:54321/app
minipg serve app.pgdb               # an explicit foreground server
minipg run app.pgdb -- npm start    # your app, with DATABASE_URL set
minipg dump app.pgdb > dump.sql     # and minipg restore app.pgdb dump.sql

There’s nothing to start by hand: if a command needs a server and there isn’t one, it starts one in the background, and when it’s been idle for a while it shuts itself down. A lock makes sure there’s only one server per .pgdb. The data lives in pgdata/ as plain files, so a stopped database can be copied like any other directory.

What I like most is the continuous integration case, which comes down to two lines:

minipg init test.pgdb
minipg run test.pgdb -- go test ./...

Under the hood

Here’s the curious part, because minipg doesn’t ship a Postgres recompiled its own way. It embeds Docker Hub’s official postgres:18-alpine image, the usual one, and runs it with h9, a runtime by the same author that works directly on the machine’s hypervisor. No Linux kernel boots, there’s no virtual machine with its disk image, and no Docker: Alpine’s binaries run as they are, and a small Plan 9 kernel answers their system calls.

flowchart LR
    A["minipg"] --> B["h9 · Plan 9 kernel"]
    B --> C["Postgres from postgres:18-alpine"]
    C --> D[("app.pgdb · data and configuration")]
    B --> E["Hypervisor · KVM or Hypervisor.framework"]

Tools like psql or pg_dump each run in their own throwaway h9 machine, which starts in about 90 milliseconds. It works on Linux (x86_64 and arm64 with KVM, or without it on x86_64 with h9’s native backend) and on macOS with Apple Silicon. And as a cherry on top, there’s a demo that runs in the browser on an x86 emulator, with a download of about 35 MB, which needs a recent Chrome or Edge.

One detail I find very sensible: new databases use Postgres’s built-in C.UTF-8 locale, so text sorting is the same on every platform. Anyone who has had a test that passes on the Mac and fails in CI because of an ORDER BY knows what I mean.

What it’s for (and what it isn’t)

The author is very clear with the warnings, and it’s worth listening:

  • It’s very early, and the part of h9 that runs Linux binaries is young too.
  • It’s slower: on h9’s native backend, Postgres runs roughly half as fast as native with a single client, and worse under load.
  • Don’t trust it with data you can’t recreate, for now.
  • Building it requires Zig 0.16, and the licence is MIT.

In other words, this isn’t here to replace your production Postgres, and it doesn’t claim to. Where I see it fitting is everything around it: the development environment, tests, a demo, that small project where you don’t feel like setting up a docker-compose.yml just to have a database. There, having the database be a directory you copy, delete or drop into a fixtures folder is a huge convenience.

I’ve been writing a lot about Postgres lately, from REPACK in version 19 to that 22-year-old bug, and also about the limits of DuckDB when it’s asked for more than it is. minipg goes in the opposite direction to all that: it doesn’t try to make Postgres bigger, but to make it fit in your pocket. Going slowly, like the snail, but with the house on its back, which for many things is exactly what’s needed.