Multi-Tenant PostgreSQL 17 — Per-App Users · PgBouncer · Encrypted Offsite Backup

PostgreSQLPgBouncerMulti-tenantSecurityNixOSResticBackup

What it is

One PostgreSQL 17 cluster on the VPS serves six databases for my self-hosted apps: Plane, LiftCraft, Vaultwarden, n8n, Linkwarden and Gitea. Each app has its own user and its own database, and every network connection authenticates with scram-sha-256. PgBouncer sits in front for connection pooling; Restic ships encrypted hourly and daily dumps to the NAS over Tailscale. All of it is declarative in the NixOS flake.

Zoom

Why it exists

When I moved my containers to the VPS, I had to decide: run a Postgres per app, or one cluster with per-app isolation. Per app would have meant six Postgres processes, six backup schedules, six upgrade paths. One cluster meant I had to do multi-tenancy properly, without trusting the containers to behave. If one app gets compromised, its credentials should open its own database and nothing else.

Architecture

Each app gets a database it owns and a user with its own password. pg_hba.conf accepts only scram-sha-256 over TCP (local socket logins use peer auth): no md5, no trust, no catch-all rule open to the network. Remote access for admin tools (DBeaver, psql, the read-only Postgres MCP server) is limited to my three workstations, in both the firewall and pg_hba.conf.

PgBouncer fronts the cluster in transaction mode, so short-lived container connections don't pile up on the backend. Restic ships the dumps to the NAS over Tailscale, so backup traffic never crosses the public internet.

What I built

  • 6 databases, one owner each — declared in the VPS NixOS profile with an ownership flag and a password file in a root-only secrets directory. Passwords come from a git-crypt-encrypted secrets file and never land in /nix/store.
  • scram-sha-256 only — every TCP rule in pg_hba.conf uses it, including PgBouncer's own lookup user.
  • PgBouncer transaction pooling — sized at 20 default pool / 1,000 max client connections. Hides connection storms from short-lived workers (Rails, Python, n8n webhook handlers).
  • postgres_exporter — Prometheus scrapes it on the same box; Grafana shows cache hit ratio, active connections per database and slow queries.
  • Hourly + daily dumps — hourly in Postgres custom format, daily in custom plus plain SQL, one file per database so a per-app restore is one pg_restore. Restic encrypts them and pushes them to the NAS.
  • Workstation-only admin access — the firewall and pg_hba.conf both list only my workstations; every other device on the private network is refused.

Key decisions & tradeoffs

Decision 1: One cluster with per-app users vs. one Postgres per app. Six Postgres processes would have given physical isolation: a runaway query in one app can't touch another's WAL. The cost: six backup schedules, six upgrade paths, six exporters. I chose one cluster and put the isolation in credentials and ownership instead. Six of everything was more work than getting the auth right once.

Decision 2: scram-sha-256 only — no md5 fallback. Any very old client won't connect. Every client I run is current, so the cost is zero, and a leaked dump of password hashes is much harder to use.

Decision 3: PgBouncer in transaction mode, not session mode. Transaction mode reuses a backend connection between transactions, so many short-lived container connections collapse to a few backend sessions. Session mode would keep full feature compatibility (advisory locks, LISTEN/NOTIFY) at the cost of one backend per client. Apps that need session state connect to Postgres directly and skip the pooler.

Implementation highlights

  • PgBouncer auth_query via SECURITY DEFINER. PgBouncer checks client passwords by reading hashes from pg_shadow, which only superusers can read. A GRANT doesn't help. The fix: a lookup function owned by the superuser with SECURITY DEFINER, created in every proxied database because PgBouncer runs auth_query in the target database.
  • Credential plumbing. Each password is an attribute in a git-crypt-encrypted secrets file. At activation NixOS writes it to a root-only file (0400); Postgres sets the user's password from it, and each container reads its own through an env_file. Never on a command line, never in the world-readable Nix store.
  • Defense in depth on the public interface. Database ports are dropped explicitly on the VPS's public interface, in addition to the NixOS firewall defaults.

What broke / what I learned

Incident 1: GRANT SELECT ON pg_shadow silently did nothing. The first PgBouncer setup granted its role read access to pg_shadow. The statement succeeded; PgBouncer's auth then failed at runtime with "permission denied for view pg_shadow". pg_shadow stays superuser-only whatever you grant. The fix was the SECURITY DEFINER function above.

Incident 2: the per-subnet rules never did the isolating. My first pg_hba.conf had a row per database, user and Docker subnet, and I believed that row matrix was what kept one app out of another's database. It wasn't. Rootless Docker containers reach the database through the host's loopback, so they match the loopback rule, not the subnet rows. A security audit in October 2026 also found a narrow trust row I had added for PgBouncer's lookup user on loopback; since every container also arrives on loopback, that row applied to them too. It was switched to scram-sha-256 on 2026-10-23 and checked on the live server. What isolates the apps today is one owner per database, one password per app, scram on every TCP rule.

Incident 3: database ports on the public interface (caught before go-live). A scan from outside the VPS before the migration showed the database ports reachable on the public interface; a firewall flag was wrong. Fix: explicit DROP rules for those ports on the public interface, so a later firewall refactor can't reopen them. I measure from outside now instead of trusting one layer.

Results

  • 6 databases on one Postgres 17 cluster, one owner and one password per app.
  • scram-sha-256 on every TCP rule, no md5, no trust.
  • Hourly + daily encrypted dumps to the NAS over Tailscale.
  • Admin access from three workstations only, enforced in the firewall and in pg_hba.conf.

Stack

PostgreSQL 17, PgBouncer, NixOS (declarative services.postgresql + custom pg_hba.conf), systemd timers, Restic, Tailscale, postgres_exporter, Prometheus + Grafana, git-crypt.

Status