Skip to content

Instantly share code, notes, and snippets.

@tdegrunt
Created October 26, 2017 10:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tdegrunt/44ed3cae8775f6c278ae32b1b55935fd to your computer and use it in GitHub Desktop.
Save tdegrunt/44ed3cae8775f6c278ae32b1b55935fd to your computer and use it in GitHub Desktop.
Auto-upgrade homebrew's PostgreSQL data directory
#!/usr/bin/env bash
old=$1
new=$2
if ! [ "$new" ]; then
new=`psql -V | cut -d' ' -f3`
fi
if ! [ "$old" ]; then
echo "Usage: pgupgrade.sh <old-version> [new-version]"
echo
echo "Please specify complete old version (i.e. 9.6.5)"
echo "Assumption is that the new psql ($new) is already active in path, if not please indicate it in arguments"
exit
fi
echo Upgrading PostgreSQL data directory from $old to $new
echo "=== Stopping postgresql ==============================="
brew services stop postgresql
echo "=== Making new data directory ========================="
initdb /usr/local/var/postgres$new -E utf8
echo "=== Upgrading data ===================================="
pg_upgrade -d /usr/local/var/postgres -D /usr/local/var/postgres$new -b /usr/local/Cellar/postgresql/$old/bin -B /usr/local/Cellar/postgresql/$new/bin -v
echo "=== Moving old data aside ============================="
mv /usr/local/var/postgres /usr/local/var/postgres$old
echo "=== Moving new data in place =========================="
mv /usr/local/var/postgres$new /usr/local/var/postgres
echo "=== Start postgresql =================================="
brew services start postgresql
@tdegrunt
Copy link
Author

Usage:

./pgupgrade.sh 9.6.5

Let me know if there are issues.

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