Tuesday, July 28, 2026

.::: Upgrade PostgreSQL from 16 to 18 on Almalinux 9 :::.

 
 
correlation https://teguhth.blogspot.com/2023/10/upgrade-postgresql-from-15-to-16-on.html

 1. check version before upgrade
SELECT version();
select pg_read_file('/etc/hostname') as hostname, version();

 


2. backup all

su - postgres -c "pg_dumpall -p 5432 > /var/lib/pgsql/backupupgrade/all_16postgresbeforeupgrade.sql"

3. remove old repository & install repository, install postgres16
detail http://teguhth.blogspot.com/2021/07/install-postgresql-linux-using.html

rpm -qa | grep -i pgdg
yum remove pgdg-redhat-repo-42.0-66.rhel9.8PGDG.noarch -y
yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-9-x86_64/pgdg-redhat-repo-latest.noarch.rpm
yum clean all
yum -y install postgresql18-server

 
 


4.  Inisialize the DB

[root@teguhth-redhat8 data]# /usr/pgsql-18/bin/postgresql-18-setup initdb
Initializing database ... OK

[root@teguhth-redhat8 data]#
[root@teguhth data]#
 

5. stop old progres 16

systemctl stop postgresql-16

[root@teguhth-all backupupgrade]# systemctl stop postgresql-16
[root@teguhth-all backupupgrade]# systemctl disable postgresql-16
Removed "/etc/systemd/system/multi-user.target.wants/postgresql-16.service".
[root@teguhth-all backupupgrade]#

6. Check that everything is fine before upgrade

su - postgres
/usr/pgsql-18/bin/pg_upgrade --old-datadir=/var/lib/pgsql/16/data --new-datadir=/var/lib/pgsql/18/data --old-bindir=/usr/pgsql-16/bin --new-bindir=/usr/pgsql-18/bin --old-options '-c config_file=/var/lib/pgsql/16/data/postgresql.conf' --new-options '-c config_file=/var/lib/pgsql/18/data/postgresql.conf' --check

su - postgres

/usr/pgsql-18/bin/pg_upgrade \
  --old-datadir=/var/lib/pgsql/16/data \
  --new-datadir=/var/lib/pgsql/18/data \
  --old-bindir=/usr/pgsql-16/bin \
  --new-bindir=/usr/pgsql-18/bin \
  --old-options '-c config_file=/var/lib/pgsql/16/data/postgresql.conf' \
  --new-options '-c config_file=/var/lib/pgsql/18/data/postgresql.conf' \
  --check
  

7. error checksum 

[postgres@teguhth-all ~]$ /usr/pgsql-16/bin/pg_controldata /var/lib/pgsql/16/data | grep checksum
Data page checksum version:           0
[postgres@teguhth-all ~]$ /usr/pgsql-18/bin/pg_controldata /var/lib/pgsql/18/data | grep checksum
Data page checksum version:           1
[postgres@teguhth-all ~]$

 


8. disable checksum postgres8  

rm -rf /var/lib/pgsql/18/data/*

/usr/pgsql-18/bin/initdb --no-data-checksums -D /var/lib/pgsql/18/data

bukan 
/usr/pgsql-18/bin/initdb -D /var/lib/pgsql/18/data --data-checksums

9. try check  again 
[postgres@teguhth-all ~]$ /usr/pgsql-18/bin/pg_upgrade --old-datadir=/var/lib/pgsql/16/data --new-datadir=/var/lib/pgsql/18/data --old-bindir=/usr/pgsql-16/bin --new-bindir=/usr/pgsql-18/bin --old-options '-c config_file=/var/lib/pgsql/16/data/postgresql.conf' --new-options '-c config_file=/var/lib/pgsql/18/data/postgresql.conf' --check
Performing Consistency Checks
-----------------------------
Checking cluster versions                                     ok
Checking database connection settings                         ok
Checking database user is the install user                    ok
Checking for prepared transactions                            ok
Checking for contrib/isn with bigint-passing mismatch         ok
Checking data type usage                                      ok
Checking for not-null constraint inconsistencies              ok
Checking for presence of required libraries                   fatal

Your installation references loadable libraries that are missing from the
new installation.  You can add these libraries to the new installation,
or remove the functions using them from the old installation.  A list of
problem libraries is in the file:
    /var/lib/pgsql/18/data/pg_upgrade_output.d/20260728T140306.265/loadable_libraries.txt
Failure, exiting
[postgres@teguhth-all ~]$

[postgres@teguhth-all ~]$ cat /var/lib/pgsql/18/data/pg_upgrade_output.d/20260728T140306.265/loadable_libraries.txt
could not load library "$libdir/pgaudit": ERROR:  could not access file "$libdir/pgaudit": No such file or directory
In database: dbatools
[postgres@teguhth-all ~]$
 
10. install pg audit 18 

[postgres@teguhth-all ~]$ rpm -qa | grep pgaudit
pgaudit_16-16.1-4PGDG.rhel9.8.x86_64
pgauditlogtofile_16-1.8.4-1PGDG.rhel9.8.x86_64

yum install pgaudit_18 pgauditlogtofile_18 -y

  


11. try check  again 
[postgres@teguhth-all ~]$ /usr/pgsql-18/bin/pg_upgrade --old-datadir=/var/lib/pgsql/16/data --new-datadir=/var/lib/pgsql/18/data --old-bindir=/usr/pgsql-16/bin --new-bindir=/usr/pgsql-18/bin --old-options '-c config_file=/var/lib/pgsql/16/data/postgresql.conf' --new-options '-c config_file=/var/lib/pgsql/18/data/postgresql.conf' --check
Performing Consistency Checks
-----------------------------
Checking cluster versions                                     ok
Checking database connection settings                         ok
Checking database user is the install user                    ok
Checking for prepared transactions                            ok
Checking for contrib/isn with bigint-passing mismatch         ok
Checking data type usage                                      ok
Checking for not-null constraint inconsistencies              ok
Checking for presence of required libraries                   fatal

Your installation references loadable libraries that are missing from the
new installation.  You can add these libraries to the new installation,
or remove the functions using them from the old installation.  A list of
problem libraries is in the file:
    /var/lib/pgsql/18/data/pg_upgrade_output.d/20260728T140747.459/loadable_libraries.txt
Failure, exiting
[postgres@teguhth-all ~]$ cat /var/lib/pgsql/18/data/pg_upgrade_output.d/20260728T140747.459/loadable_libraries.txt
could not load library "$libdir/pgaudit": ERROR:  pgaudit must be loaded via shared_preload_libraries
In database: dbatools
[postgres@teguhth-all ~]$

12. copy temporary pgaudit 

shared_preload_libraries = 'pgaudit,pgauditlogtofile'
#pgaudit.log = 'ddl, write, role'
#pgaudit.log = 'all'
pgaudit.log = 'read, write, ddl'

pgaudit.log_exclude = 'pg_catalog,information_schema'
pgaudit.log_directory = '/var/lib/pgsql/18/data/log'
pgaudit.log_filename = 'pgaudit-teguhth.log'
pgaudit.log_relation = on

systemctl restart postgresql-18


13. check again 

[postgres@teguhth-all ~]$ /usr/pgsql-18/bin/pg_upgrade --old-datadir=/var/lib/pgsql/16/data --new-datadir=/var/lib/pgsql/18/data --old-bindir=/usr/pgsql-16/bin --new-bindir=/usr/pgsql-18/bin --old-options '-c config_file=/var/lib/pgsql/16/data/postgresql.conf' --new-options '-c config_file=/var/lib/pgsql/18/data/postgresql.conf' --check
Performing Consistency Checks
-----------------------------
Checking cluster versions                                     ok
Checking database connection settings                         ok
Checking database user is the install user                    ok
Checking for prepared transactions                            ok
Checking for contrib/isn with bigint-passing mismatch         ok
Checking data type usage                                      ok
Checking for not-null constraint inconsistencies              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@teguhth-all ~]$

 


14. upgrade  

su - postgres
/usr/pgsql-18/bin/pg_upgrade --old-datadir=/var/lib/pgsql/16/data --new-datadir=/var/lib/pgsql/18/data --old-bindir=/usr/pgsql-16/bin --new-bindir=/usr/pgsql-18/bin --old-options '-c config_file=/var/lib/pgsql/16/data/postgresql.conf' --new-options '-c config_file=/var/lib/pgsql/18/data/postgresql.conf' 


[postgres@teguhth-all ~]$ /usr/pgsql-18/bin/pg_upgrade --old-datadir=/var/lib/pgsql/16/data --new-datadir=/var/lib/pgsql/18/data --old-bindir=/usr/pgsql-16/bin --new-bindir=/usr/pgsql-18/bin --old-options '-c config_file=/var/lib/pgsql/16/data/postgresql.conf' --new-options '-c config_file=/var/lib/pgsql/18/data/postgresql.conf'
Performing Consistency Checks
-----------------------------
Checking cluster versions                                     ok
Checking database connection settings                         ok
Checking database user is the install user                    ok
Checking for prepared transactions                            ok
Checking for contrib/isn with bigint-passing mismatch         ok
Checking data type usage                                      ok
Checking for not-null constraint inconsistencies              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
----------------
Some statistics are not transferred by pg_upgrade.
Once you start the new server, consider running these two commands:
    /usr/pgsql-18/bin/vacuumdb --all --analyze-in-stages --missing-stats-only
    /usr/pgsql-18/bin/vacuumdb --all --analyze-only
Running this script will delete the old cluster's data files:
    ./delete_old_cluster.sh
[postgres@teguhth-all ~]$
 

15. copy all old config to new config
su - postgres

cp /var/lib/pgsql/16/data/postgresql.conf /var/lib/pgsql/18/data/postgresql.conf
cp /var/lib/pgsql/16/data/pg_hba.conf /var/lib/pgsql/18/data/pg_hba.conf

16. restart again 

systemctl restart postgresql-18

17. if you make sure no rollbackup  

/var/lib/pgsql/delete_old_cluster.sh

[postgres@teguhth-all ~]$ cat /var/lib/pgsql/delete_old_cluster.sh
#!/bin/sh

rm -rf '/var/lib/pgsql/16/data'
[postgres@teguhth-all ~]$


18. check rpm 

[root@teguhth-all /]# rpm -qa | grep -i postgres
postgresql16-libs-16.14-2PGDG.rhel9.8.x86_64
postgresql16-16.14-2PGDG.rhel9.8.x86_64
postgresql16-server-16.14-2PGDG.rhel9.8.x86_64
postgresql18-libs-18.4-2PGDG.rhel9.8.x86_64
postgresql18-18.4-2PGDG.rhel9.8.x86_64
postgresql18-server-18.4-2PGDG.rhel9.8.x86_64
[root@teguhth-all /]#
 


19. remove postgres 16 

yum remove postgresql16* -y
 

 

No comments:

Post a Comment

Popular Posts