1

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,
    }
}
5
  • 1
    1) There is no Postgres 3.12.5 that is your Python version. 2) psql -V gives: psql (PostgreSQL) 12.3 is telling you are using version 12.3 of the command line client psql. Not that you necessarily have a Postgres 12.3 server running, as it is possible to install psql as part of a client package. 3) You need to add to your question, as text update, what OS and version you are using and how you installed Postgres. 4) Is the Django project connecting to local or remote database? Commented Aug 24, 2024 at 17:49
  • @Adrian Klaver I think OP made it clear in the question that she's using a local environment Commented Aug 24, 2024 at 18:15
  • @16171413. ... local environment for a Django project, which refers to where Django is running. That does say anything about where the database it is connecting to is running. That would be whatever the value is for DB_HOST. I was asking to make that value explicit instead of depending on an assumption. Commented Aug 24, 2024 at 19:01
  • @AdrianKlaver thanks for that differentiation that the psql version is the command line client psql- that makes sense! And had been using a local database- it sounds from the comment below that it's likely that database itself that needs upgrading, which makes sense since I haven't touched it in two years. I'm on the trail! Commented Aug 24, 2024 at 21:00
  • If you are going to upgrade might as well move up to Postgresql 16 as it will be supported until November 9, 2028 + or -. See Postgres Versioning for more information. You should also spend some time investigating what you have now as found 10.13 and psql -V gives: psql (PostgreSQL) 12.3 tends to indicate you have more then one instance of Postgres installed. Commented Aug 24, 2024 at 21:36

6 Answers 6

4

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

Sign up to request clarification or add additional context in comments.

3 Comments

Of course, that makes sense! I can pip install upgrade til the cows come home but yes the local database from two years ago will still be old and moldy. I'll take a look at that link and confirm that once I upgrade the database I'm good to go- thank you!!!
not sure if this is sarcasm or not, but anyways you are welcome 🖖
Not a bit sarcastic, your answer was exactly it- thank you! I just confirmed in the version properties for that local database itself, that's where the 10.13 was coming from! (And for next steps I can upgrade that or make dump/restore a new prod copy to use, but this was the answer!)
0

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

Comments

0

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

Comments

0

One possible cause I haven't seen listed here is the existence of an outdated Postgres Docker image that is deemed to be "latest".

You can verify with "docker images postgres":

$ docker images postgres
REPOSITORY   TAG       IMAGE ID       CREATED      SIZE
postgres     latest    563071385448   7 days ago   438MB

If "CREATED" says "4 years ago", you may want to delete the image and replace it with a fresh one.

Comments

0

Also, you can downgrade your Django if you can't upgrade your Postgresql DB.

Comments

-1

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.

4 Comments

This is not good advice. Blindly uninstalling without verifying what you have running or whether you have the data saved somewhere is a recipe for disaster.
@AdrianKlaver I can't find anywhere I advised OP to uninstall postgres. Let's look at her question: She tried to remove an old version of postgresql but ran a wrong command. I then said You can uninstall a specific version of postgresql the same way you'll uninstall programs in your machine. That's not an advise but a response to what she already tried doing. So who better knows the content of a db than the owner? 🤔
If you don't think that is advice you are seriously deluded.
It's OP's choice to delete her db or not. Sir, if you have a better answer, you can post it here. I'd love to learn

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.