By Brian Fitzgerald
Introduction
Here is a walkthough of upgrading PostgreSQL from version 10 to 12. PostgreSQL version 12 was released today, Oct 3, 2019. The operating system is Red Hat Linux 7.7.
Install the new binaries
Caution: the installation will overwrite your ~postgres/.bash_profile.
As root:
# yum -y erase pgdg-redhat-repo # yum -y install https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm # yum -y install postgresql12-server
Note: the installation will re-point some symbolic links in /usr/bin and /etc/alternatives. For example::
lrwxrwxrwx. 1 root root 28 Oct 3 15:27 /usr/bin/psql -> /etc/alternatives/pgsql-psql lrwxrwxrwx. 1 root root 22 Oct 3 15:27 /etc/alternatives/pgsql-psql -> /usr/pgsql-12/bin/psql
Shut down the old version
As postgres:
$ echo $PATH /usr/pgsql-10/bin:/var/lib/pgsql/bin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/local/sbin $ echo $PGDATA /var/lib/pgsql/10/data $ pg_ctl stop waiting for server to shut down.... done server stopped
Initialize the new PGDATA
$ PATH=/usr/pgsql-12/bin:/var/lib/pgsql/bin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/local/sbin
$ PGDATA=/var/lib/pgsql/12/data
$ mkdir -p /var/lib/pgsql/12/data
$ initdb
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.
The database cluster will be initialized with locale "en_US.UTF-8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".
Data page checksums are disabled.
fixing permissions on existing directory /var/lib/pgsql/12/data ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default time zone ... America/New_York
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok
initdb: warning: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.
Success. You can now start the database server using:
pg_ctl -D /var/lib/pgsql/12/data -l logfile start
Upgrade
Supply the old and new bin and data directories on the command line.
$ pg_upgrade -b /usr/pgsql-10/bin -B /usr/pgsql-12/bin -d /var/lib/pgsql/10/data -D /var/lib/pgsql/12/data
Performing Consistency Checks
-----------------------------
Checking cluster versions ok
Checking database user is the install user ok
Checking database connection settings ok
Checking for prepared transactions ok
Checking for reg* data types in user tables ok
Checking for contrib/isn with bigint-passing mismatch ok
Checking for tables WITH OIDS ok
Creating dump of global objects ok
Creating dump of database schemas
ok
Checking for presence of required libraries ok
Checking database user is the install user ok
Checking for prepared transactions ok
If pg_upgrade fails after this point, you must re-initdb the
new cluster before continuing.
Performing Upgrade
------------------
Analyzing all rows in the new cluster ok
Freezing all rows in the new cluster ok
Deleting files from new pg_xact ok
Copying old pg_xact to new server ok
Setting next transaction ID and epoch for new cluster ok
Deleting files from new pg_multixact/offsets ok
Copying old pg_multixact/offsets to new server ok
Deleting files from new pg_multixact/members ok
Copying old pg_multixact/members to new server ok
Setting next multixact ID and offset for new cluster ok
Resetting WAL archives ok
Setting frozenxid and minmxid counters in new cluster ok
Restoring global objects in the new cluster ok
Restoring database schemas in the new cluster
ok
Copying user relation files
ok
Setting next OID for new cluster ok
Sync data directory to disk ok
Creating script to analyze new cluster ok
Creating script to delete old cluster ok
Upgrade Complete
----------------
Optimizer statistics are not transferred by pg_upgrade so,
once you start the new server, consider running:
./analyze_new_cluster.sh
Running this script will delete the old cluster's data files:
./delete_old_cluster.sh
Start the new version
[postgres@ip-172-31-47-137 data]$ pg_ctl start waiting for server to start....2019-10-03 15:28:59.551 EDT [2589] LOG: starting PostgreSQL 12.0 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-39), 64-bit 2019-10-03 15:28:59.551 EDT [2589] LOG: listening on IPv6 address "::1", port 5432 2019-10-03 15:28:59.551 EDT [2589] LOG: listening on IPv4 address "127.0.0.1", port 5432 2019-10-03 15:28:59.554 EDT [2589] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432" 2019-10-03 15:28:59.559 EDT [2589] LOG: listening on Unix socket "/tmp/.s.PGSQL.5432" 2019-10-03 15:28:59.569 EDT [2589] LOG: redirecting log output to logging collector process 2019-10-03 15:28:59.569 EDT [2589] HINT: Future log output will appear in directory "log". done server started
Analyze the database
I have one user database, “elections”.
$ ./analyze_new_cluster.sh
This script will generate minimal optimizer statistics rapidly
so your system is usable, and then gather statistics twice more
with increasing accuracy. When it is done, your system will
have the default level of optimizer statistics.
If you have used ALTER TABLE to modify the statistics target for
any tables, you might want to remove them and restore them after
running this script because they will delay fast statistics generation.
If you would like default statistics as quickly as possible, cancel
this script and run:
"/usr/pgsql-12/bin/vacuumdb" --all --analyze-only
vacuumdb: processing database "elections": Generating minimal optimizer statistics (1 target)
vacuumdb: processing database "postgres": Generating minimal optimizer statistics (1 target)
vacuumdb: processing database "template1": Generating minimal optimizer statistics (1 target)
vacuumdb: processing database "elections": Generating medium optimizer statistics (10 targets)
vacuumdb: processing database "postgres": Generating medium optimizer statistics (10 targets)
vacuumdb: processing database "template1": Generating medium optimizer statistics (10 targets)
vacuumdb: processing database "elections": Generating default (full) optimizer statistics
vacuumdb: processing database "postgres": Generating default (full) optimizer statistics
vacuumdb: processing database "template1": Generating default (full) optimizer statistics
Done
Delete the old data files
[postgres@ip-172-31-47-137 data]$ du -s /var/lib/pgsql/10/data /var/lib/pgsql/12/data 2379432 /var/lib/pgsql/10/data 1693660 /var/lib/pgsql/12/data [postgres@ip-172-31-47-137 data]$ ./delete_old_cluster.sh [postgres@ip-172-31-47-137 data]$ du -s /var/lib/pgsql/10/data /var/lib/pgsql/12/data du: cannot access ‘/var/lib/pgsql/10/data’: No such file or directory 1691976 /var/lib/pgsql/12/data
Update ~postgres/.pgsql_profile
PATH=/usr/pgsql-12/bin:$PATH export PGDATA=/var/lib/pgsql/12/data
Restore your old ~postgres/.bash_profile, if desired.
Conclusion
This was an overview of an upgrade of a standalone server from PostgreSQL 10 to version 12.