Skip to content

Instantly share code, notes, and snippets.

@cjolly
Last active July 25, 2022 20:16
Show Gist options
  • Save cjolly/2870054 to your computer and use it in GitHub Desktop.
Save cjolly/2870054 to your computer and use it in GitHub Desktop.
Use homebrew to upgrade to postgres on OSX
newpg=9.6.1 # set to new PG version number
oldpg=`pg_config --version | cut -d' ' -f2`
# PG 96. upgrades the readline to v7, which breaks anything linked against readline v6, like ruby via ruby-build.
# I *think* this should prevent it from installing v7. But if weird shit happens with various rubies,
# you'll have to reinstall them.
brew pin readline
# Stop current Postgres server
brew services stop postgresql
# Backup current db
mv /usr/local/var/postgres/ /usr/local/var/postgres-$oldpg
# Homebrew
brew update
brew upgrade postgresql
# Create new DB
initdb /usr/local/var/postgres -E utf8
# Upgrade old DB to new DB
mkdir -p /tmp/pgupgrade && cd $_
pg_upgrade \
--old-datadir=/usr/local/var/postgres-$oldpg/ \
--new-datadir=/usr/local/var/postgres \
--old-bindir=/usr/local/Cellar/postgresql/$oldpg/bin \
--new-bindir=/usr/local/Cellar/postgresql/$newpg/bin \
--jobs=4
# Start new Postgres server
# launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
brew services start postgresql
# Clean new DB
./analyze_new_cluster.sh
# Make sure all your shit works!
# After determining upgrade successful you can remove old DB.
./delete_old_cluster.sh
brew cleanup postgresql
# Go home
cd ~
@recurser
Copy link

very useful, thanks!

@choppen5
Copy link

choppen5 commented Feb 6, 2013

Awesome... this worked very well! And I have no idea what I'm doing with Postgres.. not sure how one would upgrade without such clear instructions.

@jslag
Copy link

jslag commented Mar 20, 2013

nice, thanks.

When upgrading from 9.0 to 9.2, I had to specify the new version of pg_upgrade, eg. /usr/local/Cellar/postgresql/9.2.3/bin/pg_upgrade . . .

@nzifnab
Copy link

nzifnab commented Nov 14, 2013

homebrew had issues with the upgrade command; It told me that links with various names (various pg binary files) already exist. brew link --overwrite postgres fixed the problem.

@cjolly
Copy link
Author

cjolly commented Jan 8, 2014

I updated the script to be a bit more dynamic based on your suggestions, thanks guys!

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