Peter Weightman
Back

Updating Postgres on a mac

Today I wanted to update my postgres version from 14.9 to 16.0. I'd originally installed postgres using homebrew and wasn't quite sure how to do it.

The steps that worked for me:

  1. Install the version of postgres you'd like, in my case: brew install postgresql@16.
  2. Stop the old postgres service, in my case: brew services stop postgresql@14
    • Make sure you export the path as mentioned in the output of the command above, in my case: echo 'export PATH="/opt/homebrew/opt/postgresql@16/bin:$PATH"' >> ~/.zshrc. (And remember to source ~/.zshrc afterwards)
  3. Upgrade your PostgreSQL data directory, in my case:
pg_upgrade \
  -d /opt/homebrew/var/postgresql@14 \
  -D /opt/homebrew/var/postgresql@16 \
  -b /opt/homebrew/opt/postgresql@14/bin/ \
  -B /opt/homebrew/opt/postgresql@16/bin/
  1. Start the service for the new version, in my case: brew services start postgresql@16
  2. Uninstall the old version (if you want), in my case: brew uninstall postgresql@14

And that was it. Hope this helps you (or the future me when I need to do this again).

When trying to find out how to do this originally I came across the command: brew postgresql-upgrade-database, but that seems to be old as I got the following error:

Warning: Calling brew postgresql_upgrade_database is deprecated! Use using new, versioned e.g. `var/postgres@14` datadir and `pg_upgrade` instead.
Error: No available formula with the name "homebrew/core/postgresql".
Please tap it and then try again: brew tap homebrew/core`