41

My OpenProject management software is installed with default postgresql 10. Currently the postgresql DB is 12, It is having lot of new features.

I want to upgrade my Postgres DB without losing the data in the DB. My system is ubuntu 18.04 and hosted openproject.

I searched the internet and could not find a step by step to upgrade postgresql.

Can you please guide me to install new DB and all data should be in the new DB. thanks for your help.

1

4 Answers 4

123

A) First create a backup of all the databases for that (You can continue from B if you dont need a backup)

  1. Log in as postgres user
sudo su postgres
  1. Create a backup .sql file for all the data you have in all the databases
pg_dumpall > backup.sql

B) Upgrade to PostgreSQL12

  1. update packages and install postgres 12
sudo apt-get update
sudo apt-get install postgresql-12 postgresql-server-dev-12
  1. Stop the postgresql service
sudo systemctl stop postgresql.service
  1. migrate the data
/usr/lib/postgresql/12/bin/pg_upgrade \
--old-datadir=/var/lib/postgresql/10/main \
--new-datadir=/var/lib/postgresql/12/main \
--old-bindir=/usr/lib/postgresql/10/bin \
--new-bindir=/usr/lib/postgresql/12/bin \
--old-options '-c config_file=/etc/postgresql/10/main/postgresql.conf' \
--new-options '-c config_file=/etc/postgresql/12/main/postgresql.conf'
  1. Switch to regular user
exit
  1. Swap the ports the old and new postgres versions.
#change port to 5432
sudo vim /etc/postgresql/12/main/postgresql.conf
#change port to 5433
sudo vim /etc/postgresql/10/main/postgresql.conf
  1. Start the postgresql service
sudo systemctl start postgresql.service
  1. Log in as postgres user
sudo su postgres
  1. Check your new postgres version
psql -c "SELECT version();"
  1. Run the generated new cluster script
./analyze_new_cluster.sh
  1. Return as a normal(default user) user and cleanup up the old version's mess
#uninstalls postgres packages     
sudo apt-get remove postgresql-10 postgresql-server-dev-10
#removes the old postgresql directory
sudo rm -rf /etc/postgresql/10/
#login as postgres user
sudo su postgres
#delete the old cluster data
./delete_old_cluster.sh
  1. Congrads! Your postgresql version is now upgraded, If everything works well in B, we dont have to apply the backup as we have already migrated the data from the older version to the newer version, the backup is just in case if anything goes wrong.

NOTE: Change the postgresql.conf and pg_hba.conf as per your requirement

PS: Feel free to comment your issues, suggestions or anyother modifications you would like to suggest

10
  • 8
    Just a note for anyone doing this on WSL, type cd ~ before running pg_upgrade. Otherwise you'll run into permission issues because its trying to do Linux permission stuff in the Windows file system and fails
    – twig
    Commented Sep 29, 2020 at 10:49
  • 3
    This is great, it even works with postgresql 13 o//.
    – Nek
    Commented Nov 22, 2020 at 19:29
  • 8
    I had to be in a directory writeable by user postgres to use this on my Ubuntu Linux system. I switched to cd /tmp before I sudo su postgres and then it ran fine. Otherwise, I was getting a could not open log file "pg_upgrade_internal.log" error.
    – Todd
    Commented Nov 30, 2020 at 23:10
  • 1
    should use: su - postgres
    – VnDevil
    Commented Oct 13, 2021 at 7:16
  • 1
    I'd suggest to backup files /etc/postgresql/10/main/pg_hba.conf and /etc/postgresql/10/main/postgresql.conf before running step 9. I just lost all the settings. Commented Jan 8, 2022 at 22:15
28
  1. Backup the database
    psql --version sudo -u postgres psql pg_dumpall > alldbs.sql
    
    (this command will backup all databases from the postgresql db)

Then exit from postgres user, and:

  1. Inside a terminal run these commands:

    sudo systemctl stop postgres
    sudo apt-get install -y postgresql-12 postgresql-server-dev-12 postgresql-contrib-12 libpq-dev postgresql-12-hypopg
    sudo pg_dropcluster 12 main --stop
    sudo pg_upgradecluster 10 main
    sudo pg_dropcluster 10 main --stop
    
  2. restart the postgresql service:

    sudo systemctl restart postgresql
    
  3. login to the postgres:

    su - postgres
    
  4. to check the version:

    psql --version
    

I have done using the above steps and I could update the DBand restore all data.

4
  • to reload the data you do psql -f db.out postgres
    – Rahul
    Commented Jan 7, 2021 at 10:18
  • 1
    @Rahul when you run pg_upgradecluster it migrates all databases, schemas, extensions and custom functions. Commented Aug 31, 2022 at 22:11
  • I've found this approach caused me unexpected trouble by automatically setting start.conf to manual. As a result, the postgres service claimed to be healthy but was in fact not running. There were other problems too, but this was the most difficult one to diagnose. Commented Nov 16, 2022 at 7:00
  • In step 1, this is showing multiple commands on one line. There should be line-breaks between them.
    – fooquency
    Commented Feb 19, 2023 at 17:08
4

Postgresql Upgrade using pg_upgrade

Step - 1 (Find the locale language and encoding of source Postgresql server)

  • Log in to Postgresql database as postgres user
  • Execute the query SHOW LC_COLLATE;

Step - 2 ( Stop the existing/source Postgresql server)

  • Find the running Postgresql service using $ sudo systemctl list-units | grep postgres
  • Stop the service $ sudo service postgresql-<source-version>.service stop

Step - 3 (Install target Postgresql server)

  • Configure repos and install (lot of tutorials available in google)

Step - 4 (Update the locale language and encoding)

$ /usr/pgsql-<target-version>/bin/initdb -D /var/lib/pgsql/<target-version>/data --locale=<add-your-encoding>

Step - 5 (Check the source to target upgrade has any potential issues)

  • Run the command as postgres user, $sudo su postgres
  $  /usr/pgsql-<target-version>/bin/pg_upgrade --old-bindir /usr/pgsql-<source-version>/bin --new-bindir /usr/pgsql-<target-version>/bin --old-datadir /var/lib/pgsql/<source-version>/data --new-datadir /var/lib/pgsql/12/data --link --jobs=2 --check

If it's ok we can move to the next step, If not fix the issues before proceeding to the next step Expected output: Clusters are compatible

Step - 6 (Upgrade the source to target Postgresql version using the link option)

  • link option is pretty faster than the migration
  • Run the command as postgres user, $sudo su postgres
    $ /usr/pgsql-<target-version>/bin/pg_upgrade --old-bindir /usr/pgsql-<source-version>/bin --new-bindir /usr/pgsql-<target-version>/bin --old-datadir /var/lib/pgsql/<source-version>/data --new-datadir /var/lib/pgsql/<target-version>/data --link

Expected output: Upgrade Complete

Thanks. ENJOY !!!

2

There is a problem when migrating from e.g. 10 to 14, because then the encoding of the passwords is changed from md5 to SCRAM-SHA-256 but of course no conversion is performed. There are then two possible solutions, set a new password for all users on the postgresql console, or set the procedure back to md5 in the postgresql.conf. But this is not enough, the pg_hda.conf must also be adapted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.