Upgrade PostgreSQL from 11 to 13 on Centos 7

Upgrade PostgreSQL from 11 to 13 on CentOS 7

This article shows how to upgrade PostgreSQL from version 11 to version 13 on CentOS 7. I have tested it several times without any problems. This process typically takes between 20-30 minutes, depending on the size of your database.

If you are looking to upgrade from PostgreSQL version 10 to version 13, click here.

Let's get started!

  1. Login as root
    sudo su

  2. Create a backup of the current database(s)
    su - postgres -c "pg_dumpall -p 5432 > /tmp/dump_file.sql"

  3. Install necessary packages
    sudo yum -y install postgresql13-server postgresql13-contrib

  4.  Inicialize the DB
    /usr/pgsql-13/bin/postgresql-13-setup initdb

  5. Stop old PostgreSQL
    sudo systemctl stop postgresql-11

  6. Change user to "postgres"
    su - postgres

  7. Change directory to home (required to avoid error in next step)
    cd

  8. Check that everything is fine
    /usr/pgsql-13/bin/pg_upgrade --old-datadir=/var/lib/pgsql/11/data --new-datadir=/var/lib/pgsql/13/data --old-bindir=/usr/pgsql-11/bin --new-bindir=/usr/pgsql-13/bin --old-options '-c config_file=/var/lib/pgsql/11/data/postgresql.conf' --new-options '-c config_file=/var/lib/pgsql/13/data/postgresql.conf' --check

  9. Run, if everything is fine
    /usr/pgsql-13/bin/pg_upgrade --old-datadir=/var/lib/pgsql/11/data --new-datadir=/var/lib/pgsql/13/data --old-bindir=/usr/pgsql-11/bin --new-bindir=/usr/pgsql-13/bin --old-options '-c config_file=/var/lib/pgsql/11/data/postgresql.conf' --new-options '-c config_file=/var/lib/pgsql/13/data/postgresql.conf'

  10. Exit to root
    exit

  11. Start the PostgreSQL 13
    systemctl start postgresql-13

  12. Change user to "postgres"
    su - postgres

  13. Check version number
    psql -c "SELECT version();"

  14. Analyze new cluster
    /var/lib/pgsql/analyze_new_cluster.sh

  15. Exit again to root
    exit

  16. Copy all config settings from postgresql.conf and pg_hba.conf. If you have any command to do that, share with us in comment section.
    • These are located here:
      • /var/lib/pgsql/11/data/postgresql.conf
      • /var/lib/pgsql/11/data/pg_hba.conf
      • /var/lib/pgsql/13/data/postgresql.conf
      • /var/lib/pgsql/13/data/pg_hba.conf

  17. If you are done with these config files, restart the service
    systemctl restart postgresql-13

  18. Enable auto-start on startup
    systemctl enable postgresql-13

  19. At this point, you have to test your application. You need to restart your application (GitLab or whatever you use). If everything is working fine, let's continue by removing old packages and files:
    • sudo yum remove postgresql11*
    • rm -rf /var/lib/pgsql/11
    • su - postgres -c "/var/lib/pgsql/delete_old_cluster.sh"

That's all.

If you have any questions, feel free to ask in the comment section!

If you found this article useful and would like to show your appreciation, please consider making a small donation via PayPal. Your support will allow me to continue creating valuable content and make my blog even better. Thank you for your contribution!

Comments