Skip to content

Instantly share code, notes, and snippets.

@dincosman
Last active April 20, 2024 22:30
Show Gist options
  • Save dincosman/d290790179e073271e291b25b76c2876 to your computer and use it in GitHub Desktop.
Save dincosman/d290790179e073271e291b25b76c2876 to your computer and use it in GitHub Desktop.
Upgrade of Postgresql database using pg_upgrade
[postgres@posvt01 pg_cron-main]# mkdir -p /mnt/postgres/pgdata/16
[postgres@posvt01 pg_cron-main]# mkdir -p /mnt/postgres/pg_wal/16
[root@posvt01 ~]# systemctl stop patroni
[postgres@posvt01 ~]$ /usr/pgsql-16/bin/initdb -U postgres --data-checksums -E 'UTF8' -W --wal-segsize='32' \
--waldir='/mnt/postgres/pg_wal/16' --lc-ctype='tr_TR.UTF-8' --lc-collate='tr_TR.UTF-8' --pgdata='/mnt/postgres/pgdata/16'
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 this locale configuration:
provider: libc
LC_COLLATE: tr_TR.UTF-8
LC_CTYPE: tr_TR.UTF-8
LC_MESSAGES: en_US.utf8
LC_MONETARY: en_US.utf8
LC_NUMERIC: en_US.utf8
LC_TIME: en_US.utf8
The default text search configuration will be set to "turkish".
Data page checksums are enabled.
Enter new superuser password:
Enter it again:
fixing permissions on existing directory /mnt/postgres/pgdata/16 ... ok
fixing permissions on existing directory /mnt/postgres/pg_wal/16 ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default time zone ... Europe/Istanbul
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
initdb: hint: 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:
/usr/pgsql-16/bin/pg_ctl -D /mnt/postgres/pgdata/16 -l logfile start
[postgres@posvt01 14]$ cp /mnt/postgres/pgdata/14/pg_hba.conf /mnt/postgres/pgdata/16/
[postgres@posvt01 14]$ cp /mnt/postgres/pgdata/14/pg_ident.conf /mnt/postgres/pgdata/16/
[postgres@posvt01 14]$ cp /mnt/postgres/pgdata/14/postgresql.conf /mnt/postgres/pgdata/16/
[postgres@posvt01 14]$ cp /mnt/postgres/pgdata/14/postgresql.base.conf /mnt/postgres/pgdata/16/postgresql.base.conf
-- Check for parameters
[postgres@posvt01 14]$ vi /mnt/postgres/pgdata/16/postgresql.conf
...
port = '3131'
hba_file = '/mnt/postgres/pgdata/16/pg_hba.conf'
ident_file = '/mnt/postgres/pgdata/16/pg_ident.conf'
unix_socket_directories = '/tmp'
...
-- An upgrade check
[postgres@posvt01 tmp]$ /usr/pgsql-16/bin/pg_upgrade --check -U postgres -b /usr/pgsql-14/bin/ -B /usr/pgsql-16/bin/ \
-d /mnt/postgres/pgdata/14/ -D /mnt/postgres/pgdata/16/ -p 3531 -P 3131 -o "-c config_file=/mnt/postgres/pgdata/14/postgresql.conf" \
-O "-c config_file=/mnt/postgres/pgdata/16/postgresql.conf" --socketdir='/tmp/'
Performing Consistency Checks on Old Live Server
------------------------------------------------
Checking cluster versions ok
Checking database user is the install user ok
Checking database connection settings ok
Checking for prepared transactions ok
Checking for system-defined composite types in user tables ok
Checking for reg* data types in user tables ok
Checking for contrib/isn with bigint-passing mismatch ok
Checking for incompatible "aclitem" data type in user tables ok
Checking for presence of required libraries ok
Checking database user is the install user ok
Checking for prepared transactions ok
Checking for new cluster tablespace directories ok
*Clusters are compatible*
[postgres@posvt01 ~]$ /usr/pgsql-16/bin/pg_upgrade -U postgres -b /usr/pgsql-14/bin/ -B /usr/pgsql-16/bin/ \
-d /mnt/postgres/pgdata/14/ -D /mnt/postgres/pgdata/16/ -p 3531 -P 3131 -o "-c config_file=/mnt/postgres/pgdata/14/postgresql.conf" \
-O "-c config_file=/mnt/postgres/pgdata/16/postgresql.conf" --socketdir='/tmp/'
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 system-defined composite types in user tables ok
Checking for reg* data types in user tables ok
Checking for contrib/isn with bigint-passing mismatch ok
Checking for incompatible "aclitem" data type in user tables 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
Checking for new cluster tablespace directories ok
If pg_upgrade fails after this point, you must re-initdb the
new cluster before continuing.
Performing Upgrade
------------------
Setting locale and encoding for new cluster ok
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 oldest XID for new cluster 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 delete old cluster ok
Checking for extension updates notice
Your installation contains extensions that should be updated
with the ALTER EXTENSION command. The file
update_extensions.sql
when executed by psql by the database superuser will update
these extensions.
Upgrade Complete
----------------
Optimizer statistics are not transferred by pg_upgrade.
Once you start the new server, consider running:
/usr/pgsql-16/bin/vacuumdb -U postgres --all --analyze-in-stages
Running this script will delete the old cluster's data files:
./delete_old_cluster.sh
[postgres@posvt01 ~]$ psql -f update_extensions.sql
[postgres@posvt01 ~]$ /usr/pgsql-16/bin/vacuumdb -U postgres --all --analyze-in-stages
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment