In the ever-evolving world of data management, staying up-to-date with the latest technologies and best practices is imperative. One crucial aspect of this is ensuring your PostgreSQL version remains current. On November 9, 2023, PostgreSQL 11 reaches end of life (EOL).
This post provides a detailed guide on upgrading the PostgreSQL client from version 11 to version 15 in a dockerized Django application. The commands are customized for the tools used at Caktus, but I've included general commands where necessary. Please note that this guide focuses solely on upgrading the Postgres client version; upgrading the server version involves different steps, which we will not go over in this post.
Preparation for the Upgrade
-
Review Django's PostgreSQL version support:
- Django 3.2: PostgreSQL 9.6 and higher
- Django 4.2: PostgreSQL 12 and higher
Changes
-
Update psycopg2, the PostgreSQL database adapter, in your Django project requirements to the latest version:
# requirements/base/base.in psycopg2==2.9.9
Update the requirements and install, for example:
make update_requirements make setup
-
Add
POSTGRESQL_CLIENT_VERSION
to Dockerfile and updateRUN_DEPS
:ENV POSTGRESQL_CLIENT_VERSION="15" RUN set -ex && RUN_DEPS="postgresql-client-${POSTGRESQL_CLIENT_VERSION}"
Test the build as follows:
# With invoke inv image.build # Without invoke docker build -t
:pg15 --target deploy -f Dockerfile . -
Upgrade postgresql-server to
postgres:15
in the Docker Compose file. Update the Docker Compose image for the PostgreSQL service:services: db: image: postgres:15-alpine
Delete the existing volume and start the PostgreSQL service:
docker compose down docker volume rm <volume-name> docker compose up -d db
Ensure that the correct PostgreSQL server version is reported:
❯ psql psql (14.5 (Homebrew), server 15.2)
-
Restore the database archive:
# Get a database archive - with invoke inv staging pod.get-db-dump dropdb project_name; createdb project_name pg_restore -Ox -d $DATABASE_URL < project_name-staging_database.dump rm project_name-staging_database.dump
-
If needed, re-create any project-specific PostgreSQL extensions in
template1
before the test DB is created:psql -d template1 -c 'CREATE EXTENSION IF NOT EXISTS citext;'
-
Run the test suite:
pytest
That's it!
This guide has equipped you with the knowledge and practical steps to upgrade your PostgreSQL client from version 11 to 15 in your dockerized Django application. Keeping your technology stack current is essential for security, performance, feature enhancements, and more.