Upgrading Postgres database from v13 to v14 running in Podman or Docker containers
Background
I have Podman containers running with auto-updating the container images on a recurring schedule. The containers were running fine with the latest version (v13) and patches until today, and the container had stopped initiating with the below error message and stopped working.
Error Message
which is not compatible with this version 14.0
Reason
The reason is pretty self-explanatory is that the container image has been updated to version 14, whereas the data directory mapped to your host path uses version 13 data. We will look at solving this issue pretty quickly in simple steps.
Steps to fix the issue
1. Temporarily change the podman service to use the old version.
For example, see below:
After restarting the service, it should still be able to start the database service successfully.
2. Take a backup of old version data (v13) in a file.
a. Login to the podman/docker image bash shell by running the command:
podman exec -it postgres bash
b. Go to the data directory:
cd /var/lib/postgresql/data
c. Run the dump all command. Note: this command should export all the databases.
pg_dumpall -U {postgres_user} > dump_13.0.sql
d. Exit from the container command prompt. using (ctrl+D)
3. Rename the folder of the host path data folder and create a new empty directory.
a. For example,
cd ${HOST_PATH}
mv data data_old
b. Create a new empty directory in place of the old directory,
mkdir data
4. Change the Podman systems service back to the original and restart the container service.
For example:
a. change the version back to the original.
vi /usr/lib/systemd/system/postgres.service
systemctl daemon-reload
systemctl restart postgres
5. Verify the database is back up and running with an empty database.
a. Check the container logs, and see if the error related to incompatible data directory error went away.
b. Try to connect and see data using your credentials set for the Postgres or root user.
6. Copy the dump file from the old data directory to the new data directory.
Commands:
cd ${HOST_PATH}cp data-old/dump-13.0.sql data/
7. Open a bash prompt inside the container and execute the data load command.
Commands:
podman exec -it postgres bash
cd /var/lib/postgresql/data
psql -U ${PGUSER} < dump_13.0.sql
8. Restart the container and verify the data and Postgres database version.
Command:
systemctl restart postgres