Last active
June 20, 2018 20:14
-
-
Save ramytamer/17330c64462f55e0cccacd59798d8312 to your computer and use it in GitHub Desktop.
Upgrade PostgreSQL 9.4 to 10 ([email protected] to postgresql) version using Homebrew (macOS)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# I would recommend to run it step by step instead of running the whole script to detect and fix any issue that may come up. | |
# Stop if something went wrong | |
set -e; | |
# database host, mine is the default localhost | |
host=localhost | |
# db user, mine is the default postgres user | |
dbuser=postgres | |
# create a dir to contain all our dump databases and change directory into it | |
mkdir ~/dbdump && cd $_ | |
# listing all databases and dumping them each one into it's file | |
psql -h $host -U $dbuser -t -A -c \ | |
"SELECT datname FROM pg_database where datname not in ('template1', 'template0', 'postgres')" | \ | |
while read db; do pg_dump -U $dbuser -h $host --clean --file="$db.sql" $db; end | |
# dump all globals for the cluster | |
# note use "-o" option to backup foreign keys | |
pg_dumpall -U postgres -h localhost -p 5433 --clean --globals-only --file=globals.sql | |
# stop the service | |
brew services stop [email protected] | |
# Stop current server (if started manually) | |
# pg_ctl -D /usr/local/var/[email protected] stop | |
# unlink the current one | |
brew unlink [email protected] | |
# install the new version | |
brew install postgresql | |
# start it up | |
brew services start postgresql | |
# create postgresuser | |
createuser -s postgres | |
# import globals first | |
psql -U $dbuser -h $host < globals.sql | |
# re-import each database file in the directory (except the globals) | |
# we read each one, split by the dot to get the database name, create the database and import from it's file. | |
# this will take some time, if you want to tackle each single database separately: | |
# createdb databasename && psql -h $host -U $dbuser databasename < databasename.sql | |
ls | grep -v global | while read filename; do \ | |
db=$(echo $filename | cut -d'.' -f1); createdb "$db" && psql -h $host -U $dbuser "$db" < "$filename"; done | |
# Happy Hacking! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment