Search code examples
djangopostgresqlpsycopg2python-venv

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,
    }
}

Solution

  • 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