Upgrading PostgreSQL From v11 to v15 in Your Django Application

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

  1. Review Django's PostgreSQL version support:

Changes

  1. 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
  2. Add POSTGRESQL_CLIENT_VERSION to Dockerfile and update RUN_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 .
  3. 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)
  4. 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
  5. 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;'
  6. 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.

New Call-to-action
blog comments powered by Disqus
Times
Check

Success!

Times

You're already subscribed

Times