There are several approaches to assess when you need to upgrade PostgreSQL. In this blog post, we look at the option for upgrading a postgres database using pg_dumpall. As this tool can also be used to back up PostgreSQL clusters, then it is a valid option for upgrading a cluster too. We consider the advantages and disadvantages of this approach, and show you the steps needed to achieve the upgrade.
This is the first of our Upgrading or Migrating Your Legacy PostgreSQL to Newer PostgreSQL Versions series where we’ll be exploring different paths to accomplish postgres upgrade or migration. The series will culminate with a practical webinar.
We begin this journey by providing you the most straightforward way to carry on with a PostgreSQL upgrade or migration: by rebuilding the entire database from a logical backup.
Let’s define what we mean by upgrading or migrating PostgreSQL using pg_dumpall.
If you need to perform a PostgreSQL upgrade within the same database server, we’d call that an in-place upgrade or just an upgrade. Whereas a procedure that involves migrating your PostgreSQL server from one server to another server, combined with an upgrade from an older version (let’s say 9.3) to a newer version PostgreSQL (say PG 11.2), can be considered a migration.
There are two ways to achieve this requirement using logical backups :
We’ll be discussing the first option (pg_dumpall) here, and will leave the discussion of the second option for our next post.
pg_dumpall can be used to obtain a text-format dump of the whole database cluster, and which includes all databases in the cluster. This is the only method that can be used to backup globals such as users and roles in PostgreSQL.
There are, of course, advantages and disadvantages in employing this approach to upgrading PostgreSQL by rebuilding the database cluster using pg_dumpall.
Let’s look at the steps involved in performing an upgrade using pg_dumpall:
|
1 |
-- For a RedHat family OS<br># yum install postgresql11* <br>Or<br>-- In an Ubuntu/Debian OS<br># apt install postgresql11<br> |
|
1 |
$ /usr/pgsql-11/bin/initdb -D new_data_directory<br>$ cd new_data_directory<br>$ echo "port = 5433" >> postgresql.auto.conf<br>$ /usr/pgsql-11/bin/pg_ctl -D new_data_directory start |
|
1 |
$ psql -d dbname -c "dx" |
|
1 |
-- Command to dump the whole cluster to a file.<br>$ /usr/pgsql-11/bin/pg_dumpall > /tmp/dumpall.sql<br><br>-- Command to restore the dump file to the new cluster (assuming it is running on port 5433 of the same server).<br>$ /usr/pgsql-11/bin/psql -p 5433 -f /tmp/dumpall.sql |
|
1 |
$ pg_dumpall -p 5432 | psql -p 5433 <br>Or<br>$ pg_dumpall -p 5432 -h source_server | psql -p 5433 -h target_server |
Our next post in this series provides a similar way of upgrading your PostgreSQL server while at the same time providing some flexibility to carry on with changes like the ones described above. Stay tuned!
—
Image based on photo by Sergio Ortega on Unsplash
Resources
RELATED POSTS