Django venv finding old version of PostgreSQL -> django.db.utils.NotSupportedError: PostgreSQL 13 or later is required

I'm resurrecting my local environment for a Django project that I haven't run locally in 2 years, working through issues around things that have gone stale. But I have one a little different: it looks like Django is finding/using an older version of PostgreSQL than the version I see in the venv itself. What's a good approach for tracking down old versions so I can remove them?

When I run python mysite/manage.py runserver, I get

django.db.utils.NotSupportedError: PostgreSQL 13 or later is required (found 10.13).

BUT when I check package versions in the venv I'm running, most packages are current, and PostgreSQL is 3.12.5 (not 13 or later like we'll ultimately need, but also not 10.13).

  • (from pip list) Django 5.1

  • (from pip list) psycopg2 2.9.9

  • (from pip list) psycopg2-binary 2.9.9

  • (from pip list) psycopg2-pool 1.2

  • psql -V gives: psql (PostgreSQL) 12.3

  • python -v gives: Python 3.12.5

Unsurprisingly, if I try a naive uninstall from the venv (pip uninstall postgresql-10.13), it says it's not installed.

What's a good approach for tracing where that 10.13 could be coming from?

Looking in the stack trace this NotSupportedError is raised while connecting to the database, from .venv/lib/python3.12/site-packages/django/db/backends/base/base.py", line 200, in check_database_version_supported

From the venv, my $PATH variable has:

/Users/dkaplan/.vscode/extensions/ms-python.python-2024.12.3-darwin-x64/python_files/deactivate/bash:/Users/dkaplan/family-django/.venv/bin:/Library/Frameworks/Python.framework/Versions/3.12/bin:/Library/Frameworks/Python.framework/Versions/3.12/bin:/Library/Frameworks/Python.framework/Versions/3.10/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/dkaplan/.m2:/Applications/Postgres.app/Contents/Versions/latest/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:/Users/dkaplan/.vscode/extensions/ms-python.python-2024.12.3-darwin-x64/python_files/deactivate/bash:/Users/dkaplan/family-django/.venv/bin:/Library/Frameworks/Python.framework/Versions/3.12/bin:/Library/Frameworks/Python.framework/Versions/3.10/bin

My default databases settings is:

DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.postgresql_psycopg2",
        "NAME": DB_DATABASE,
        "USER": DB_USER,
        "PASSWORD": DB_PASSWORD,
        "HOST": DB_HOST,
        "PORT": "5432",
        "OPTIONS": DB_OPTIONS,
    }
}

You cannot pip uninstall postgresql-10.13 because postgresql-10.13 is not a Python package.

You can uninstall a specific version of postgresql the same way you'll uninstall programs in your machine. See Uninstalling PostgreSQL.

Basically you need to upgrade PostgreSQL

dig into pg_upgrade

here are docs https://www.postgresql.org/docs/current/pgupgrade.html

the problem occures because of postgresql database, that is on your machine. It is old :)

It is not a python package, pip won't help here.

Or you can consider dumping db with pg_dump, using something like docker compose version of postgres and django-dbbackup package to restore db

Given this /var/run/com.apple.security.cryptex and this vscode/extensions/ms-python.python-2024.12.3-darwin-x64 I'm going to say you are on MacOS. That means four ways of installing Postgres that I know of: a) Build from source. b) Install with Homebrew. c) Postgres.app d) Install using the EnterpriseDB installer.

I would do the following:

  1. Determine what instances of Postgres you have installed. At the command line ps ax | grep postgres would be a good way to start. Then you will need to determine which of the above methods where used to install Postgres(possibly multiple methods). If there are multiple instances then figure out which one contains the information you need, DDL as well as data.

  2. Come to a decision as to the viability of the data. I would suggest doing a pg_dump of the data just in case later it turns out there was important information.

  3. Decide on what version you want to move to. Per my comment my suggestion would be Postgres 16 as it gives you the most time before you need to upgrade again. Install that version.

  4. If you decided to move the data the then two choices: a) Do the pg_dump/pg_restore. Life is easier if you use the newer version pg_dump to dump the older Postgres database. b) Use pg_upgrade.

  5. Uninstall the old Postgres version(s) using whatever method the packaging tooling provides.

  6. Future-proofing per Django docs:

https://docs.djangoproject.com/en/5.1/ref/databases/#postgresql-notes

Django supports PostgreSQL 13 and higher. psycopg 3.1.8+ or psycopg2 2.8.4+ is required, though the latest psycopg 3.1.8+ is recommended.

Note Support for psycopg2 is likely to be deprecated and removed at some point in the future.

You might think about upgrading to psycopg(3) at this time. The caveat would be if you use psycopg2 outside of the Django app in that env. There are some significant differences betwen psycopg2 and psycopg see:

https://www.psycopg.org/psycopg3/docs/basic/from_pg2.html

So @OP, this issue isn't related to pip. I encountered a similar problem after upgrading to Django 5.1 from 5.0.8 and received the error: django.db.utils.NotSupportedError: PostgreSQL 13 or later is required (found 12.20).

This is expected actually, because, according to the Django 5.1 release notes, support for PostgreSQL 12 was dropped, as well as support for MariaDB 10.4.

You have two options:

  1. To Downgrade Django to a previous version that supports your current PostgreSQL version.
  2. Upgrade PostgreSQL/MariaDB to a version supported by Django 5.1.

Full release note: https://docs.djangoproject.com/en/5.1/releases/5.1/

How to Upgrade PostgresQL?: https://www.postgresql.org/docs/current/upgrading.html

Back to Top