Introducing Livops 2.0! Promo codeCEOgives you 8,000 so‘m on your balance.What’s new

django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 or psycopg module — how to fix it

django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 or psycopg module means Django's PostgreSQL backend could import neither Psycopg 3 nor psycopg2 in the Python that runs your app. The driver is not in requirements.txt, it went into another virtual environment, or it is installed but cannot load. Add psycopg[binary] (Django 4.2 and later) or psycopg2-binary to requirements.txt, and install it with the same Python that runs manage.py.

By Livops team, the people who build and run LivopsUpdated

The deploy goes through, and then migrate, or the first page that needs the database, stops with a traceback like this one. On Django 4.2 and later, the backend tries Psycopg 3 first, then psycopg2[1][3], so the end of the traceback is three chained errors:

Traceback (most recent call last):
  File ".../site-packages/django/db/backends/postgresql/base.py", line 25, in <module>
    import psycopg as Database
ModuleNotFoundError: No module named 'psycopg'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File ".../site-packages/django/db/backends/postgresql/base.py", line 27, in <module>
    import psycopg2 as Database
ModuleNotFoundError: No module named 'psycopg2'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  ...
django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 or psycopg module

Django 4.1 and older import psycopg2 only, and put the reason on the same line[2]:

django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 module: No module named 'psycopg2'

This page explains what Django is telling you, how to read the lines above the last one, the causes from the most common, which driver package to put in requirements.txt, and how to check the fix before you deploy.

What does the error mean?

With ENGINE set to django.db.backends.postgresql[8], Django loads its PostgreSQL backend, and that module imports a driver as it loads[1]. Since Django 4.2 it accepts either one: it tries import psycopg, and if that raises ImportError, import psycopg2; if that fails too, it raises ImproperlyConfigured("Error loading psycopg2 or psycopg module")[1][3]. The ENGINE is the same for both drivers[7].

So the message means one thing: in the Python that runs your app, neither import worked. It does not say why. The reason is in the chained errors above it, which Django’s own message no longer repeats[1]:

The lines above say What it means
No module named 'psycopg', then No module named 'psycopg2' Neither driver is installed in this Python
no pq wrapper available. with a list of attempts Psycopg 3 is installed, but none of its implementations can load[11]
any other ImportError under import psycopg2 psycopg2 is installed, but its compiled part or a library it needs does not load

Installing Django does not install a driver: Django’s own dependencies are asgiref and sqlparse (and tzdata on Windows)[9]. Its docs list the driver as a requirement of the PostgreSQL backend, which you install yourself: psycopg 3.1.12+ or psycopg2 2.9.9+ for Django 6.1, with the latest psycopg 3 recommended[6].

A close variant: the driver is too old

If a driver imports but is older than Django supports, the message is different[1]:

django.core.exceptions.ImproperlyConfigured: psycopg2 version 2.9.9 or newer is required; you have 2.8.6
Django Oldest psycopg2 Oldest psycopg 3
6.0 and later 2.9.9[5][1] 3.1.12[5][1]
4.2 to 5.2 2.8.4[3][4] 3.1.8[3][4]
4.1 and older 2.8.4 for 4.1[2] not supported[2]

Raise the version in requirements.txt; the rest of this page is about the driver being missing.

Causes, most common first

1. The driver is not in requirements.txt

It works on your laptop because you ran pip install psycopg2-binary once by hand, and the server installs only what requirements.txt lists. Since nothing in your own code imports the driver — only Django does, from the ENGINE setting — reading your imports does not show it is missing either.

Fix: add one driver to requirements.txt (which one: below):

django>=4.2
psycopg[binary]

2. It is installed, but into another Python

You ran pip install with one Python and run Django with another: the virtual environment was not active, pip on the PATH belongs to the system Python, or the IDE uses its own interpreter. python -m pip runs pip with the interpreter you name[15], so naming the one that runs Django puts the package where Django looks:

.venv/bin/python -m pip install "psycopg[binary]"
.venv/bin/python -c "import psycopg; print(psycopg.__version__)"

If the second line prints a version, manage.py run with that same .venv/bin/python finds the driver too.

3. psycopg2 failed to build, so it was never installed

psycopg2 without -binary is a source package, and building it needs pg_config, the PostgreSQL client headers and a C compiler[12]. When that fails, pip stops with pg_config executable not found; if you then removed the line to get the deploy through, Django is left with no driver. The same build starts unexpectedly when a psycopg2-binary pin has no wheel for your Python: 2.9.9 has wheels only up to Python 3.12[13], so on 3.13 or 3.14 pip compiles it from source, while 2.9.11 has wheels for 3.13 and 3.14[14].

Fix: use a package that needs no compiling, psycopg[binary] or a current psycopg2-binary. The full story is on pg_config executable not found.

4. psycopg is installed without [binary], and there is no libpq

pip install psycopg, without an extra, installs the pure Python implementation, which needs the libpq client library already installed on the machine[10]. Psycopg 3 tries its C, binary and pure Python implementations in turn, and when none loads it raises[11]:

ImportError: no pq wrapper available.
Attempts made:
- couldn't import psycopg 'c' implementation: No module named 'psycopg_c'
- couldn't import psycopg 'binary' implementation: No module named 'psycopg_binary'
- couldn't import psycopg 'python' implementation: libpq library not found

Django catches that ImportError, tries psycopg2, and ends with the same Error loading psycopg2 or psycopg module[1]. Fix: write psycopg[binary] rather than psycopg in requirements.txt; the binary package is self-contained[10].

5. Django is older than 4.2, and only Psycopg 3 is installed

Psycopg 3 support arrived in Django 4.2, released on 3 April 2023[7]. Django 4.1 and older import psycopg2 and nothing else[2], so psycopg in the requirements does not help them, and the message is the older Error loading psycopg2 module: No module named 'psycopg2'.

Fix: install psycopg2-binary for the old Django, or upgrade Django to 4.2 or later and keep psycopg[binary].

Which driver to put in requirements.txt

Line in requirements.txt What you get Builds on install?
psycopg[binary] Psycopg 3 with its own libraries; “recommended for most users”[10] No
psycopg[c] Psycopg 3 built on your machine; “the preferred way” for a production site[10] Yes: compiler, Python and libpq headers, pg_config[10]
psycopg Psycopg 3, pure Python, much slower; needs the system libpq[10] No
psycopg2-binary psycopg2 with its own copies of libpq and libssl[12] No, where a wheel exists[13][14]
psycopg2 psycopg2 from source[12] Yes

For a new project on Django 4.2 or later, take psycopg[binary]. Django recommends Psycopg 3 and says support for psycopg2 is likely to be deprecated and removed at some point[6][7]. Psycopg 3 also brings some breaking changes over psycopg2[7], which matters only if your own code imports the driver directly.

The two projects describe their binary packages differently, so know what you are choosing. Psycopg 3’s docs call psycopg[binary] the quickest way to start and recommended for most users, and a local C build the preferred way for a production site[10]. psycopg2’s docs say psycopg2-binary is meant for beginners, advise the source distribution for production, and warn that its bundled libssl can conflict with Python’s ssl module[12]. A library you publish should depend on plain psycopg, not a specific build[10].

How to check before you deploy

Do what the server does: a new virtual environment, only requirements.txt, then the database:

python3 -m venv /tmp/fresh-venv
/tmp/fresh-venv/bin/python -m pip install -r requirements.txt
/tmp/fresh-venv/bin/python -c "import django.db.backends.postgresql.base"

The last line imports exactly the module that raises this error[1], and needs no database, so it fails here when the driver is missing and prints nothing when it is fine. Delete /tmp/fresh-venv afterwards. Once the driver loads, an error from the database itself, such as password authentication failed, is a different problem, with its own page.

On Livops

  • requirements.txt is the whole list. Livops installs your requirements.txt with pip into the project’s .venv on every build, and start commands run from it: for a Django project with Gunicorn, .venv/bin/gunicorn mysite.wsgi:application. A driver you installed by hand on your laptop is not there; put it in the file. A start command you write yourself should begin with .venv/bin/ too, for example .venv/bin/python manage.py ....

  • Where you see it. Livops runs .venv/bin/python manage.py migrate --noinput before every start of a Django project, so this error usually shows up first in the output of that migrate, followed by the line Livops: migrate failed — the app starts anyway; look above. The app is then started anyway, and pages that use the database fail with the same error until the driver is installed. If the app does not start at all, the deploy fails as start_failed and the reason is on the Logs page; see The app did not start.

  • A driver that compiles. When psycopg2 or another package fails to compile, Livops retries the install once in an image with a compiler; if it still fails, use the variant that needs no compiling, psycopg2-binary or psycopg[binary]. See A package compiles on install.

  • The database. Add PostgreSQL under Databases: version 15, 16 (the default) or 17, which fits Django’s own minimum of PostgreSQL 15[6]. Its address is written to every service in the environment as DATABASE_URL=postgresql://app:<password>@postgres:5432/app, together with PGHOST, PGPORT, PGUSER, PGPASSWORD and PGDATABASE. Read them in settings.py:

    import os
    
    DATABASES = {
        "default": {
            "ENGINE": "django.db.backends.postgresql",
            "NAME": os.environ["PGDATABASE"],
            "USER": os.environ["PGUSER"],
            "PASSWORD": os.environ["PGPASSWORD"],
            "HOST": os.environ["PGHOST"],
            "PORT": os.environ["PGPORT"],
        }
    }
  • Choose the Python. Livops runs Python 3.10 to 3.14; a .python-version file at the project root pins it, for example 3.12.

A Python web app costs 39 credits a month, and one 500 MB database per team is free; see Databases and Django hosting.

Questions

Which package do I install, psycopg or psycopg2?

On Django 4.2 or later, psycopg[binary], which is Psycopg 3; Django recommends Psycopg 3 and says psycopg2 support is likely to be deprecated. On Django 4.1 or older, psycopg2-binary, because those versions import psycopg2 only. Either way the ENGINE stays django.db.backends.postgresql.

I installed psycopg2 and still get the error. Why?

It is installed into a different Python from the one that runs Django. Install with python -m pip, using the exact interpreter that runs manage.py, for example .venv/bin/python -m pip install psycopg2-binary, and check with .venv/bin/python -c followed by import psycopg2.

What does no pq wrapper available mean in the traceback?

Psycopg 3 is installed without its binary or C part, as plain psycopg, and the machine has no libpq library for its pure Python part to load. Install psycopg[binary] instead, which brings its own libpq, or install the system libpq.

Is psycopg2-binary or psycopg[binary] fine in production?

The two projects say it differently. Psycopg 3 calls the binary package recommended for most users and the local C build the preferred way for a production site. psycopg2's docs call psycopg2-binary a package for beginners and advise the source distribution for production. The binary packages carry their own copies of libpq and libssl, so build from source when you want the system's own.

Why does my old psycopg2-binary pin try to compile?

pip uses a wheel only if one exists for your Python. psycopg2-binary 2.9.9 has wheels up to Python 3.12, so on Python 3.13 or 3.14 pip falls back to the source package and compiles it, which fails with pg_config executable not found. Upgrade the pin; 2.9.11 has wheels for 3.13 and 3.14.

What does psycopg2 version 2.9.9 or newer is required mean?

The driver is installed but too old for your Django. Django 6.0 and later need psycopg2 2.9.9 or psycopg 3.1.12 at least; Django 4.2 to 5.2 need psycopg2 2.8.4 or psycopg 3.1.8. Raise the version in requirements.txt.

Sources

  1. Django source — django/db/backends/postgresql/base.py (main) (raw.githubusercontent.com). Checked .
  2. Django source — django/db/backends/postgresql/base.py (stable/4.1.x) (raw.githubusercontent.com). Checked .
  3. Django source — django/db/backends/postgresql/base.py (stable/4.2.x) (raw.githubusercontent.com). Checked .
  4. Django source — django/db/backends/postgresql/base.py (stable/5.2.x) (raw.githubusercontent.com). Checked .
  5. Django source — django/db/backends/postgresql/base.py (stable/6.0.x) (raw.githubusercontent.com). Checked .
  6. Django docs — Databases, PostgreSQL notes (docs.djangoproject.com). Checked .
  7. Django 4.2 release notes — Psycopg 3 support (docs.djangoproject.com). Checked .
  8. Django docs — Settings, DATABASES (docs.djangoproject.com). Checked .
  9. PyPI — Django (pypi.org). Checked .
  10. Psycopg 3 — Installation (psycopg.org). Checked .
  11. Psycopg 3 source — psycopg/pq/__init__.py (raw.githubusercontent.com). Checked .
  12. Psycopg 2 — Installation (psycopg.org). Checked .
  13. PyPI — psycopg2-binary 2.9.9, release files (pypi.org). Checked .
  14. PyPI — psycopg2-binary 2.9.11, release files (pypi.org). Checked .
  15. pip docs — User Guide, Running pip (pip.pypa.io). Checked .

Put your project online today

Connect GitHub or drop a folder. No card is asked for.

Deploy your Django app

Got a question? — @livops_uz