# 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.

Source: https://livops.dev/en/guides/error-django-error-loading-psycopg2-module

By Livops team, the people who build and run Livops. Updated 3 December 2026.

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:

```text
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]:

```text
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]:

```text
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](#which-driver-to-put-in-requirementstxt)):

```text
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:

```sh
.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](https://livops.dev/en/guides/error-psycopg2-pg-config-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]:

```text
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:

```sh
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](https://livops.dev/en/guides/error-postgres-password-authentication-failed).

## 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](https://livops.dev/en/docs/error-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](https://livops.dev/en/docs/error-install#py-compiler).
- **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`:

  ```python
  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](https://livops.dev/en/docs/databases) and [Django hosting](https://livops.dev/en/hosting/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)](https://raw.githubusercontent.com/django/django/main/django/db/backends/postgresql/base.py). Checked 3 December 2026.
2. [Django source — django/db/backends/postgresql/base.py (stable/4.1.x)](https://raw.githubusercontent.com/django/django/stable/4.1.x/django/db/backends/postgresql/base.py). Checked 3 December 2026.
3. [Django source — django/db/backends/postgresql/base.py (stable/4.2.x)](https://raw.githubusercontent.com/django/django/stable/4.2.x/django/db/backends/postgresql/base.py). Checked 3 December 2026.
4. [Django source — django/db/backends/postgresql/base.py (stable/5.2.x)](https://raw.githubusercontent.com/django/django/stable/5.2.x/django/db/backends/postgresql/base.py). Checked 3 December 2026.
5. [Django source — django/db/backends/postgresql/base.py (stable/6.0.x)](https://raw.githubusercontent.com/django/django/stable/6.0.x/django/db/backends/postgresql/base.py). Checked 3 December 2026.
6. [Django docs — Databases, PostgreSQL notes](https://docs.djangoproject.com/en/stable/ref/databases/). Checked 3 December 2026.
7. [Django 4.2 release notes — Psycopg 3 support](https://docs.djangoproject.com/en/stable/releases/4.2/). Checked 3 December 2026.
8. [Django docs — Settings, DATABASES](https://docs.djangoproject.com/en/stable/ref/settings/). Checked 3 December 2026.
9. [PyPI — Django](https://pypi.org/project/Django/). Checked 3 December 2026.
10. [Psycopg 3 — Installation](https://www.psycopg.org/psycopg3/docs/basic/install.html). Checked 3 December 2026.
11. [Psycopg 3 source — psycopg/pq/__init__.py](https://raw.githubusercontent.com/psycopg/psycopg/master/psycopg/psycopg/pq/__init__.py). Checked 3 December 2026.
12. [Psycopg 2 — Installation](https://www.psycopg.org/docs/install.html). Checked 3 December 2026.
13. [PyPI — psycopg2-binary 2.9.9, release files](https://pypi.org/project/psycopg2-binary/2.9.9/#files). Checked 3 December 2026.
14. [PyPI — psycopg2-binary 2.9.11, release files](https://pypi.org/project/psycopg2-binary/2.9.11/#files). Checked 3 December 2026.
15. [pip docs — User Guide, Running pip](https://pip.pypa.io/en/stable/user_guide/). Checked 3 December 2026.
