Skip to content
Rezha Julio
Go back

Upgrade Major PostgreSQL version on Docker

3 min read

Upgrading a PostgreSQL database server to a newer version is a common task for database administrators and developers. In this post, we will discuss how to upgrade a PostgreSQL database server running in a Docker container.

On 1 January, I decided to upgrade the PostgreSQL used by my Mastodon instance. It was using PostgreSQL 12 and I think it could used the upgrade to PostgreSQL 15 for that “extra performance and features”. For the upgrade process, basically you need to run pg_upgrade tool. pg_upgrade allows data stored in PostgreSQL data files to be upgraded to a later PostgreSQL major version without the data dump/restore typically required for major version upgrades, e.g., from 9.5.8 to 9.6.4 or from 10.7 to 11.2. It is not required for minor version upgrades, e.g., from 9.6.2 to 9.6.3 or from 10.1 to 10.2.

Fortunately, I found a github repository from Tianon that contain ready-to-use container image to upgrade PostgreSQL. I will share how my experience in using them.

Before:

db:
restart: always
image: postgres:12-alpine
shm_size: 256mb
networks:
- internal_network
healthcheck:
test: ['CMD', 'pg_isready', '-U', 'postgres']
volumes:
- ./postgres12:/var/lib/postgresql/data

After

db:
restart: always
image: postgres:15-alpine
shm_size: 256mb
networks:
- internal_network
healthcheck:
test: ['CMD', 'pg_isready', '-U', 'postgres']
volumes:
- ./postgres15:/var/lib/postgresql/data

I run the docker-compose up -d to check wether everythings work perfectly. You might want to check and compare the configuration on pg_hba.conf and postgresql.conf first beforehand. I had issue where the new pg_hba.conf only listen to localhost only.


Related Posts


Previous Post
Several Cool Things You Can Do with F-Strings in Python
Next Post
2023