Postgres won't start after brew upgrade: database files are incompatible with server
After an onboarding script moved our Macs to postgresql@17, local databases refused to start with an incompatible data directory. The new binaries were pointed at a cluster created by 16. We pinned per-project paths and upgraded with pg_upgrade.
On a Monday morning, six engineers on our team reported the same thing: the local Postgres that backs our API test suite would not start. make db-start printed a one-line failure and the app could not connect. Everyone affected had run our updated bin/setup script on Friday afternoon. The server log under the data directory showed:
2027-01-04 09:12:47.031 CET [41822] FATAL: database files are incompatible with server
2027-01-04 09:12:47.031 CET [41822] DETAIL: The data directory was initialized by PostgreSQL version 16, which is not compatible with this version 17.2.
The app error was the usual downstream symptom:
psql: error: connection to server on socket "/tmp/.s.PGSQL.5432" failed: No such file or directory
Is the server running locally and accepting connections on that Unix domain socket?
Reproducing it
On a machine that still had the old setup, we could trigger it every time:
Have a cluster created by
postgresql@16in a directory our Makefile controls (PGDATA=$HOME/dev/pgdata).Install
postgresql@17and change the shellPATHline from the@16keg to the@17keg (exactly what Friday's setup script did).Open a new shell and run
pg_ctl -D "$PGDATA" start.
The pg_ctl and postgres binaries found first on PATH were now 17, the files on disk were 16.
What we ruled out
A corrupted data directory.
cat "$PGDATA/PG_VERSION"printed16and starting it with the 16 binaries by full path worked fine, so the files were healthy.A port or socket clash.
lsof -nP -iTCP:5432 -sTCP:LISTENreturned nothing, so no other server held the port.Homebrew's own service. Nobody affected used
brew servicesfor this cluster;brew services listshowedpostgresql@16asnone. The Makefile starts Postgres itself so tests use a throwaway directory.A macOS or Xcode update. Machines on macOS 15 and macOS 26 were both affected; the only shared change was the setup script.
Root cause
Our setup script had been edited to "keep up with Postgres" by switching both the Brewfile entry and the exported PATH from postgresql@16 to postgresql@17. Homebrew did exactly what it was told. But Postgres's on-disk format is tied to the major version, and our Makefile always pointed whatever pg_ctl was first on PATH at the same long-lived data directory. A major version change on the binary side with no migration on the data side is precisely what the startup check refuses.
What is happening under the hood
Every Postgres data directory records the major version that created it in PG_VERSION. At startup the server compares that with its own major version and stops with the FATAL above if they differ. Minor releases (17.1 to 17.2) never change the format; major releases (16 to 17) may change system catalogs and internal layouts, so the server will not guess.
Homebrew ships each Postgres major line as its own formula: postgresql@14, postgresql@15, postgresql@16, postgresql@17. They are keg-only, meaning their binaries are not linked into /opt/homebrew/bin; you choose one by adding $(brew --prefix postgresql@16)/bin to PATH. When Homebrew runs the service for a formula, it uses a data directory named after it, such as /opt/homebrew/var/postgresql@16, so two formulae never share files on their own. The breakage only happens when your own scripts reuse one directory across versions.
brew upgrade on a versioned formula only moves within the major line, so a routine upgrade would not have broken us. A human editing @16 to @17 did.
Environment
Component | Version / value |
|---|---|
OS | macOS 15.x and macOS 26.x, Apple Silicon |
Homebrew prefix |
|
Old server |
|
New server |
|
Data directory |
|
Start method |
|
Minimal reproduction
brew install postgresql@16 postgresql@17
export PGDATA="$HOME/tmp/pgdemo"
"$(brew --prefix postgresql@16)/bin/initdb" -D "$PGDATA" --locale=C -E UTF8
"$(brew --prefix postgresql@17)/bin/pg_ctl" -D "$PGDATA" -l "$PGDATA/server.log" start
tail -n 3 "$PGDATA/server.log"
The last command prints the same FATAL and DETAIL lines.
Who is affected
Anyone who starts Postgres with binaries chosen by PATH against a fixed data directory: Makefiles, foreman/overmind Procfiles, direnv setups, or IDE run configurations. People who only use brew services start postgresql@NN are protected, because each formula keeps its own directory, though switching to a new formula gives them an empty cluster rather than their data.
The same thing happens on Linux with distribution packages when a script hard-codes PGDATA, so the lesson is not Mac-specific.
The fix
We did two things: made every script name the binary version explicitly, and migrated each developer's data to 17 with pg_upgrade instead of throwing it away.
1. Tie binaries to the data directory. The Makefile now reads the version from a single variable and derives both paths from it:
-PGDATA ?= $(HOME)/dev/pgdata
-db-start:
- pg_ctl -D $(PGDATA) -l $(PGDATA)/server.log start
+PG_MAJOR ?= 17
+PG_BIN := $(shell brew --prefix postgresql@$(PG_MAJOR))/bin
+PGDATA ?= $(HOME)/dev/pgdata-$(PG_MAJOR)
+db-start:
+ $(PG_BIN)/pg_ctl -D $(PGDATA) -l $(PGDATA)/server.log start
A version in the directory name means a mismatch now produces "directory does not exist" instead of a scary FATAL, and the old cluster stays untouched.
2. Upgrade the data with pg_upgrade. Both majors must be installed. The new cluster must be created with the same locale and encoding as the old one, or pg_upgrade --check will refuse.
OLD_BIN="$(brew --prefix postgresql@16)/bin"
NEW_BIN="$(brew --prefix postgresql@17)/bin"
OLD_DATA="$HOME/dev/pgdata"
NEW_DATA="$HOME/dev/pgdata-17"
"$OLD_BIN/psql" -d postgres -Atc "SHOW lc_collate; SHOW server_encoding;" # while 16 runs
"$OLD_BIN/pg_ctl" -D "$OLD_DATA" stop
"$NEW_BIN/initdb" -D "$NEW_DATA" --locale=C -E UTF8
cd "$HOME/dev"
"$NEW_BIN/pg_upgrade" -b "$OLD_BIN" -B "$NEW_BIN" -d "$OLD_DATA" -D "$NEW_DATA" --check
"$NEW_BIN/pg_upgrade" -b "$OLD_BIN" -B "$NEW_BIN" -d "$OLD_DATA" -D "$NEW_DATA"
--check reports problems without changing anything; we only ran the real upgrade after it printed *Clusters are compatible*. We ran pg_upgrade from $HOME/dev because it writes its logs into the current directory.
After the upgrade, start the new cluster and refresh planner statistics, which pg_upgrade does not carry over:
"$NEW_BIN/pg_ctl" -D "$NEW_DATA" -l "$NEW_DATA/server.log" start
"$NEW_BIN/vacuumdb" --all --analyze-in-stages
3. Make the Brewfile explicit about both versions during the transition.
brew "postgresql@16" # remove after everyone has run bin/upgrade-pg
brew "postgresql@17"
We removed @16 two weeks later, once bin/upgrade-pg (the commands above wrapped in a script with set -euo pipefail) had been run on every laptop.
Alternatives and trade-offs
Dump and restore.
pg_dumpallwith the old binaries, thenpsql -finto a fresh 17 cluster. Slower for big local datasets and needs disk for the dump, but it works across any version gap and also cleans up bloat.pg_upgrade --link. Uses hard links instead of copying, so it is nearly instant and needs little extra disk. After you start the new cluster, the old one can no longer be used safely. Fine on a laptop with a backup, not our default.brew pin postgresql@16. Stops Homebrew from upgrading that formula at all, including security patch releases. It does not protect you from a person editing theBrewfile, which is what actually happened, so we did not use it.Throw the data away. For pure test fixtures, recreating the cluster with
initdband reseeding is legitimate and fastest. Several teammates chose this.
How to test the fix
cat "$HOME/dev/pgdata-17/PG_VERSION" # expect 17
make db-start && psql -d postgres -Atc 'SHOW server_version;'
make test
We also checked the opposite case: setting PG_MAJOR=16 with only a 17 cluster present now fails with a clear "No such file or directory" for pgdata-16, not a FATAL from the server.
Confirming in production
This was a developer-environment incident, so "production" meant every laptop and the CI macOS runner. We added a check to bin/doctor that every engineer runs after setup:
want=$(make -s print-PG_MAJOR)
have=$(cat "$HOME/dev/pgdata-$want/PG_VERSION" 2>/dev/null || echo missing)
[ "$want" = "$have" ] || echo "Postgres data is '$have', setup expects $want; run bin/upgrade-pg"
Within a day all twenty machines reported a matching version; CI ran the same check on its runner image.
Regression guard
bin/doctorruns at the end ofbin/setup, so a future version bump in the Makefile is caught on the first run.Any change to
PG_MAJORor the Postgres line in theBrewfilerequires a matchingbin/upgrade-pgchange in the same pull request; CODEOWNERS routes those files to the platform team.The integration suite asserts
SHOW server_version_numis at least the value the migrations need, so a stale cluster fails fast in CI.
Security implications
None beyond the usual: local clusters trust connections on the Unix socket by default (initdb sets trust for local connections when no --auth option is given), so we left that unchanged and kept listen_addresses at localhost. The old 16 data directories contained copies of anonymised seed data only; we asked people to delete them with pg_upgrade's generated delete_old_cluster.sh once the new cluster passed tests.
Prevention checklist
Put the major version in the data directory name.
Call Postgres binaries by explicit versioned path in scripts, never by whatever
PATHfinds.Treat a Postgres major bump as a migration with its own script, not a one-line
Brewfileedit.Run
pg_upgrade --checkbefore every real upgrade.Re-run
vacuumdb --analyze-in-stagesafter upgrading.
References
PostgreSQL
pg_upgradedocumentation: https://www.postgresql.org/docs/current/pgupgrade.htmlPostgreSQL upgrading a cluster: https://www.postgresql.org/docs/current/upgrading.html
PostgreSQL
initdboptions: https://www.postgresql.org/docs/current/app-initdb.htmlHomebrew FAQ (pinning, keg-only formulae): https://docs.brew.sh/FAQ
Homebrew
brew bundleandbrew pinmanual: https://docs.brew.sh/Manpage
Add a label
Embed this post
<iframe src="https://hibug.me/brew-upgrade-broke-postgres-versioned-formula" width="700" height="420" frameborder="0" loading="lazy"></iframe>
I build and maintain public and partner APIs, currently for a logistics platform with web, mobile and third-party clients all calling the same endpoints. Before that I worked on identity: login, sessions, OAuth integrations and the long tail of edge cases they create. The bugs I care about sit at the contract between client and server. A PUT that wiped fields the client never sent, a cache that served one user's page to another, a field removal that broke app versions still in the wild, a refresh flow that logged people out when two tabs raced. None of these show up in a happy-path demo. My posts walk through how each one surfaced, what the HTTP or auth spec actually says, the fix we shipped and the tests we added so the next change to the API does not repeat it.
Comments
0No comments yet. Be the first to comment!
Sign In to post a comment.