Cyrus Stoller home about consulting

Upgrading Postgresql with Homebrew

Homebrew makes it easy to keep packages up to date on Mac OS X, but sometimes upgrades take a few more steps than a simple brew update && brew upgrade. For example, upgrading Postgresql requires you to unload and load the LaunchAgent to get the binaries to load and start properly after minor upgrades.

$ launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
$ launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist

After a major upgrade (e.g. from 8.4.x to 9.5.x or 9.4.x to 9.5.x), you also need to run pg_upgrade to keep the internal data storage up to date. But, unfortunately this is easy to miss because it isn’t shown in the Homebrew output.

Here’s how to do it. $OLDPG and $NEWPG are just placeholders for version identifiers.

1) Stop Postgres

$ launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist

2) Backup your data

mv /usr/local/var/postgres/ /usr/local/var/postgres-$OLDPG

3) Initialize a new database

$ initdb /usr/local/var/postgres -E utf8 --locale=C

4) Run pg_upgrade to load your data into the new database

$ pg_upgrade -d /usr/local/var/postgres-$OLDPG/ \
    -D /usr/local/var/postgres \
    -b /usr/local/Cellar/postgresql/$OLDPG/bin \
    -B /usr/local/Cellar/postgresql/$NEWPG/bin

5) Start Postgres

$ launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist

If you forget this last step, Postgres won’t start running automatically the next time you restart your computer.

This process was confusing when I was just getting started with web development. It’s hard enough to figure out how to write your application, let alone making sure your computer is configured properly. Hope this helps keep you up to date.

Category Tutorial