PostgreSQL 9.5 Upgrade with Homebrew
Photo © Vaughan Leiberum https://www.flickr.com/photos/laertes_za/5342495338/. Licensed under a Creative Commons license.
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:
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.
FYI: I had to set add
initdb …to avoid the following error,
lc\_collate cluster values do not match: old "en\_US.UTF-8", new "C".
Thanks for this article! It was very helpful!
Epic. This should be included in the brew info of every postrges update!!!
Awesome, thank you for this brilliant writeup.
export LC_CTYPE=en_US.UTF-8 before initdb did the trick. Thanks
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.
@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.
Great tutorial! The thought of major version upgrades always freak me out, but this was concise and I was done in a minute.
Thank you so much. I accidentally upgraded from Postgres 9.5 to 9.6 and this worked like a charm!