Skip to content

Instantly share code, notes, and snippets.

@Dangeranger
Created October 14, 2017 15:00
Show Gist options
  • Save Dangeranger/512646d9fe25fd4643babb3e6a69782b to your computer and use it in GitHub Desktop.
Save Dangeranger/512646d9fe25fd4643babb3e6a69782b to your computer and use it in GitHub Desktop.
Upgrading Postgres after brew upgrade

Turn PostgreSQL off first:

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

or, if you're running a current version of Homebrew

$ brew services stop postgresql

Update PostgreSQL itself:

$ brew update && brew upgrade postgresql

Make a new, pristine 9.5 database:

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

Migrate the data to the new 9.5 database. Note that I have 9.4.5_2 in here, it could be that you aren’t on the latest version. Replace 9.4.5_2 with the most current version of postgres in that directory.

$ 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

Move 9.5 data directory back to where PostgreSQL expects it to be:

$ mv /usr/local/var/postgres /usr/local/var/postgres9.4
$ mv /usr/local/var/postgres9.5 /usr/local/var/postgres

Start PostgreSQL back up!

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

or, if you're running a current version of Homebrew

$ brew services start postgresql

Note: If you’re using the pg gem for Rails, you should recompile:

$ gem uninstall pg
$ gem install pg

Note 2: If you’ve already uninstalled a previous version of PostgreSQL, there is a good post on StackOverflow with instructions to install previous versions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment