When upgrading to a new major version of PostgreSQL, you need to migrate your data. The easiest way to migrate your data is using pg_dumpall, but there are alternative methods that can be useful when you have very large databases, or if you want to migrate only parts of the database.
pg_dumpall
This is the easiest way to migrate your data.
pg_dumpall --quote-all-identifiers | gzip >postgresapp.sql.gz
gunzip <postgresapp.sql.gz | psql
Server Settings..
to swap which server to start automaticallypg_dump
This method lets you select which databases you’d like to migrate.
psql --list
to show the list of databasespg_dump database_name > database_name.sql
to create a dump of your databasepg_dumpall --globals-only > globals.sql
psql -f globals.sql
psql --command="create database database_name"
to create the databasepsql -d database_name -f database_name.sql
to restore from the backup~/Library/Application Support/Postgres
pg_upgrade
Using pg_upgrade
from the command line is a bit more difficult.
This is recommended only if you have a large database and using pg_dump
is too slow or uses too much disk space.
Make sure you completely understand the process and have a working backup before attempting this!
Since pg_upgrade
needs the old and new binaries, you must make sure that Postgres.app contains the binaries of the old server and of the new server.
Postgres.app 2 contains 9.5 and 9.6 by default, but using other versions is possible as well.
Here’s an example how to upgrade from 9.5 to 9.6:
Contents/Versions
and make sure that binaries for the old version and the new version are included.~/Library/Application Support/Postgres
and create a new, empty folder for the new data directory, eg var-9.6
/Applications/Postgres.app/Contents/Versions/9.6/bin/initdb -D ~/Library/Application\ Support/Postgres/var-9.6 --encoding=UTF-8 --locale=en_US.UTF-8
/Applications/Postgres.app/Contents/Versions/9.6/bin/pg_upgrade -b /Applications/Postgres.app/Contents/Versions/9.5/bin -B /Applications/Postgres.app/Contents/Versions/9.6/bin -d ~/Library/Application\ Support/Postgres/var-9.5 -D ~/Library/Application\ Support/Postgres/var-9.6 -v
(see the pg_upgrade documentation for details)pg_upgrade
will leave behind two scripts, analyze_new_cluster.sh
and delete_old_cluster.sh
. Run them to optimise the new database and remove the old database cluster