Skip to Content Collective Idea Home

Photo © Vaughan Leiberum https://www.flickr.com/photos/laertes_za/5342495338/. Licensed under a Creative Commons license.

PostgreSQL 9.5 Upgrade with Homebrew

by Daniel Morrison

Updated May 13, 2016 with new brew services syntax

PostgreSQL 9.5 was released yesterday and has a lot of cool new features. If you’re on Mac OS X and using Homebrew, you can upgrade with the steps below.

Note: Keita Kobayashi guide to upgrading to 9.4 is a great resource and this post attempts the same for 9.5.

First, install Postgres 9.5:

brew update && brew upgrade postgres

Then let’s stop the old installation (but don’t load up the new one yet).

brew services stop postgresql

Next, we initialize an empty database:

initdb /usr/local/var/postgres9.5 -E utf8

Then we do the upgrade. First, find out exactly which version you had before:

ls /usr/local/Cellar/postgresql/

Which gives you output something like this: 9.4.5_2 9.5.0. If you have more than two, that’s ok, just get the most recent one before 9.5. In my case, that was 9.4.5_2. Remember that version for the next step.

Run pg_upgrade and put the version number we remembered above in the 4th line here.

pg_upgrade \
  -d /usr/local/var/postgres \
  -D /usr/local/var/postgres9.5 \
  -b /usr/local/Cellar/postgresql/9.4.5_2/bin/ \
  -B /usr/local/Cellar/postgresql/9.5.0/bin/ \
  -v

Next, move the new directory to where postgres expects it to be.

mv /usr/local/var/postgres /usr/local/var/postgres-9.4.5_2
mv /usr/local/var/postgres9.5 /usr/local/var/postgres

You could alternately move the old directory to the trash. I don’t like to delete it completely until we’ve made sure our upgrade works. Finally, start up PostgreSQL again:

brew services start postgresql

Test out your application(s) (for me, I fired up a rails console and pulled up a record). You can clean up the directory and run brew cleanup if it all worked.

Comments

Brian Ryckbost
::

FYI: I had to set add `locale=en_US.UTF-8` to the initdb … to avoid the following error, lc_collate cluster values do not match: old “en_US.UTF-8”, new “C”.


Eliza Marcum
::

Thanks for this article! It was very helpful!


Michael Holroyd
::

Epic. This should be included in the brew info of every postrges update!!!


Jason Cartwright
::

Awesome, thank you for this brilliant writeup.


Serge Matisko
::

export LC_CTYPE=en_US.UTF-8 before initdb did the trick. Thanks


Garvit Verma
::

I’m doing the same thing, but command `initdb /usr/local/var/postgres9.5 -E utf8` does not create any folder at location `/usr/local/Cellar/postgresql/`. I only found 1 folder and that is `9.4.4`. Apart from that `homebrew.mxcl.postgresql.plist` is not present at location `~/Library/LaunchAgents/`. Please help me out with this.


Daniel Morrison
::

@Garvit Verma: it could be that versions are newer, so yes, check what folders you have and swap those version numbers in.

I updated the post to use the new brew services syntax for instead of loading and unloading the plist files.


Daniel Morrison
::

Don Pflaster
::

Great tutorial! The thought of major version upgrades always freak me out, but this was concise and I was done in a minute.


Simon Eichenauer
::

Thank you so much. I accidentally upgraded from Postgres 9.5 to 9.6 and this worked like a charm!