Skip to content

Instantly share code, notes, and snippets.

@cjolly cjolly/pg.sh
Last active Dec 4, 2018

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

commented Nov 23, 2012

very useful, thanks!

@choppen5

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link
Owner Author

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
You can’t perform that action at this time.