Unable to run tests django. django.db.utils.ProgrammingError: relation "auth_user" does not exist
I'm writing tests. When trying to run it, it returns the error "django.db.utils.ProgrammingError: relation "auth_user" does not exist". I'm running the project locally using a virtual environment. There is a similar error in the logs in pgadmin. This error also occurs when starting a project in docker. I checked the migrations. I tried all the options from the Internet and GPT. I will be very grateful for your help.
✅ Fix for: django.db.utils.ProgrammingError: relation "auth_user" does not exist
This error means the auth_user
table (from Django's built-in auth system) hasn’t been created in your PostgreSQL DB.
✅ Quick Fix Steps:
- Check DB settings in
settings.py
:
python
CopyEdit
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql', 'NAME': 'your_db_name', 'USER': 'your_db_user', 'PASSWORD': 'your_password', 'HOST': 'localhost', # or 'db' if using Docker 'PORT': '5432', } }
- Run migrations:
bash
CopyEdit
python manage.py makemigrations python manage.py migrate auth python manage.py migrate
- Using Docker?
DB might not be ready when Django runs — use a wait-for-it script:
bash
CopyEdit
./wait-for-it.sh db:5432 -- python manage.py migrate
Or add a health check in docker-compose.yml
.
- Using a custom user model?
Check this insettings.py
:
python
CopyEdit
AUTH_USER_MODEL = 'yourapp.YourCustomUser'
Make sure its migration was created and applied.
- Still failing?
Inpsql
or pgAdmin:
sql
CopyEdit
SELECT* FROM django_migrations WHERE app = 'auth';
If no rows, the auth
migrations never ran.