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.txtwith pip into the project’s.venvon 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 --noinputbefore every start of a Django project, so this error usually shows up first in the output of thatmigrate, followed by the lineLivops: 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 asstart_failedand the reason is on the Logs page; see The app did not start. -
A driver that compiles. When
psycopg2or 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-binaryorpsycopg[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 withPGHOST,PGPORT,PGUSER,PGPASSWORDandPGDATABASE. Read them insettings.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-versionfile at the project root pins it, for example3.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
- Django source — django/db/backends/postgresql/base.py (main) (raw.githubusercontent.com). Checked .
- Django source — django/db/backends/postgresql/base.py (stable/4.1.x) (raw.githubusercontent.com). Checked .
- Django source — django/db/backends/postgresql/base.py (stable/4.2.x) (raw.githubusercontent.com). Checked .
- Django source — django/db/backends/postgresql/base.py (stable/5.2.x) (raw.githubusercontent.com). Checked .
- Django source — django/db/backends/postgresql/base.py (stable/6.0.x) (raw.githubusercontent.com). Checked .
- Django docs — Databases, PostgreSQL notes (docs.djangoproject.com). Checked .
- Django 4.2 release notes — Psycopg 3 support (docs.djangoproject.com). Checked .
- Django docs — Settings, DATABASES (docs.djangoproject.com). Checked .
- PyPI — Django (pypi.org). Checked .
- Psycopg 3 — Installation (psycopg.org). Checked .
- Psycopg 3 source — psycopg/pq/__init__.py (raw.githubusercontent.com). Checked .
- Psycopg 2 — Installation (psycopg.org). Checked .
- PyPI — psycopg2-binary 2.9.9, release files (pypi.org). Checked .
- PyPI — psycopg2-binary 2.9.11, release files (pypi.org). Checked .
- 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 appRelated
- Error: pg_config executable not found (psycopg2): how to fix itError: pg_config executable not found, followed by Failed building wheel for psycopg2, means pip is compiling psycopg2 from source and the machine has no PostgreSQL client headers or pg_config. Switch to a package that needs no compiling, psycopg2-binary or psycopg[binary] for Psycopg 3, or install libpq-dev and a C compiler where the build runs.
- ModuleNotFoundError: No module named — after a deployPython searched sys.path and could not find the module you import. After a deploy that almost always means the package is not in requirements.txt, is listed under a different name than the one you import, or the start command runs a Python that the packages were not installed into.
- FATAL: password authentication failed for user "…": how to fix it after deployFATAL: password authentication failed for user "…" means PostgreSQL was reached but refused the login: the password, or the user name, is not the one the server has. After a deploy it is usually a DATABASE_URL copied from your local .env, a password with special characters that are not percent-encoded in the URL, the wrong user, or a password that was changed. Test the exact value your app reads with psql.
- sqlalchemy.exc.NoSuchModuleError: Can't load plugin: sqlalchemy.dialects:postgres: how to fix itNoSuchModuleError: Can't load plugin: sqlalchemy.dialects:postgres means your database URL starts with postgres://, a name SQLAlchemy 1.4 and later no longer accept. Use postgresql://, or name the driver: postgresql+psycopg2:// or postgresql+psycopg://. When a host hands you a postgres:// DATABASE_URL, rewrite the scheme in code before create_engine.
- pip: Failed building wheel / failed-wheel-build-for-install — how to fix itpip found no ready-made wheel for a package on your Python version and platform, tried to compile one from source, and the compiler step failed. The fix is almost never to install a compiler: move to a version of the package that ships a wheel for your Python, switch to its binary variant (psycopg[binary], psycopg2-binary), or pin the Python version the package supports.
- Django hosting in 2026: how to deploy a Django app, and whereFor most Django apps, a platform that deploys from Git is the right pick: Render and DigitalOcean App Platform are the simplest fixed monthly prices, Fly.io is the cheapest in dollars, Railway bills by usage, and a VPS is for when you want root. Livops fits Django apps whose users are in Uzbekistan, at 39 credits a month. Below: prices and free tiers side by side, a Django deployment checklist, and how to host a Django project on Livops.
- Managed PostgreSQL hosting in 2026: the options comparedA production PostgreSQL needs backups you can restore, a connection limit your app stays under, the app in the same region, and a supported version. On Livops a PostgreSQL 15, 16 or 17 database runs inside your project, its DATABASE_URL is written into your services, and it is backed up every night; 500 MB is free, one per team, and 1 GB is 9 credits a month. Here is how the paid plans of Neon, Supabase, Render, Railway and a VPS compare; free tiers have their own page.