After setting up PostgreSQL with Django in a virtual environment, you might encounter this frustrating error:
django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 module: No module named psycopg2
Despite running what seems like the correct installation commands:
sudo apt-get install libpq-dev python-dev
sudo apt-get update
workon myenv
sudo pip install psycopg2
The root cause typically stems from one of these common issues:
- Installing psycopg2 system-wide instead of in the virtualenv
- Virtualenv not being properly activated
- Permission issues with pip installation
- Missing PostgreSQL development libraries
Here's the correct way to install psycopg2 in your Django virtualenv:
# First ensure system dependencies are installed
sudo apt-get install libpq-dev python3-dev
# Activate your virtualenv
source /path/to/your/venv/bin/activate # or 'workon myenv' if using virtualenvwrapper
# Install psycopg2-binary (recommended for development)
pip install psycopg2-binary
# Or for production (requires proper build environment)
# pip install psycopg2
Before configuring Django, verify psycopg2 is properly installed in your virtualenv:
python -c "import psycopg2; print(psycopg2.__version__)"
If this doesn't work, try reinstalling with:
pip uninstall psycopg2 psycopg2-binary
pip install psycopg2-binary --force-reinstall
For production deployments, consider adding psycopg2 to your requirements.txt:
# requirements.txt
psycopg2-binary==2.9.3 # for development
# or
psycopg2==2.9.3 # for production
Then install with:
pip install -r requirements.txt
Here's a proper PostgreSQL configuration for Django settings.py:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'mydb',
'USER': 'postgres',
'PASSWORD': 'yoursecurepassword',
'HOST': 'localhost',
'PORT': '5432',
}
}
Note: Modern Django versions can use either 'postgresql' or 'postgresql_psycopg2' as the ENGINE.
- Ensure PostgreSQL server is running:
sudo service postgresql status
- Check virtualenv activation:
which python
should point to your virtualenv - Verify pip installation location:
pip show psycopg2
- Test database connection outside Django:
python -c "import psycopg2; conn = psycopg2.connect(dbname='mydb', user='postgres', password='yourpassword', host='localhost')"
When working with Django and PostgreSQL in a virtual environment, a common roadblock developers face is the infamous "Error loading psycopg2 module: No module named psycopg2" message. This typically occurs despite having installed psycopg2 via pip.
The primary culprit here is using sudo pip install
within a virtualenv. The sudo
command causes the package to be installed system-wide rather than in your virtual environment. Here's what's happening:
# This installs globally, not in your virtualenv
sudo pip install psycopg2
To correctly install psycopg2 in your virtual environment:
workon myenv # Activate your virtualenv
pip install psycopg2-binary # Recommended for easier installation
# OR for the standard version
pip install psycopg2
Before configuring Django, verify psycopg2 is properly installed:
python -c "import psycopg2; print(psycopg2.__version__)"
If this command executes without errors, you've successfully installed psycopg2 in your virtualenv.
If you encounter compilation issues (common on some systems), consider these alternatives:
# For Debian/Ubuntu systems
sudo apt-get install python3-psycopg2
# Using the binary package (recommended for development)
pip install psycopg2-binary
# Specifying the exact version
pip install psycopg2==2.9.3
Ensure your Django settings are properly configured. Here's a complete example:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'mydb',
'USER': 'postgres',
'PASSWORD': 'password1234',
'HOST': 'localhost',
'PORT': '5432', # Explicit port is better than empty string
'OPTIONS': {
'connect_timeout': 5,
},
}
}
If the error persists after proper installation:
- Verify virtualenv activation:
which python
should point to your virtualenv - Check installed packages:
pip freeze
should list psycopg2 - Try reinstalling:
pip uninstall psycopg2 && pip install psycopg2-binary
- Verify PostgreSQL server is running:
sudo service postgresql status
For production environments, consider these additional steps:
# Install the correct dependencies
sudo apt-get install build-essential libpq-dev python3-dev
# Then install psycopg2 (not binary version)
pip install psycopg2
# Verify the build was successful
python -c "import psycopg2; print(psycopg2.extensions.libpq_version())"