PostgreSQL 9.5 Upgrade with Homebrew

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

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.

Photo of Daniel Morrison

Daniel founded Collective Idea in 2005 to put a name to his growing and already full-time freelance work. He works hard writing code, teaching, and mentoring.

Comments

  1. brian@collectiveidea.com
    Brian Ryckbost
    January 11, 2016 at 14:04 PM

    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".

  2. eliza@elizamarcum.com
    Eliza Marcum
    January 30, 2016 at 15:47 PM

    Thanks for this article! It was very helpful!

  3. February 12, 2016 at 19:37 PM

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

  4. February 17, 2016 at 4:47 AM

    Awesome, thank you for this brilliant writeup.

  5. March 02, 2016 at 17:03 PM

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

  6. garvitjee@gmail.com
    Garvit Verma
    May 13, 2016 at 11:42 AM

    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.

  7. May 13, 2016 at 12:51 PM

    @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.

  8. May 13, 2016 at 12:51 PM
  9. May 20, 2016 at 20:24 PM

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

  10. Simon Eichenauer
    March 26, 2017 at 21:37 PM

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