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:

  1. 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', } }

  1. Run migrations:
bash

CopyEdit

python manage.py makemigrations python manage.py migrate auth python manage.py migrate

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

  1. Using a custom user model?
    Check this in settings.py:
python

CopyEdit

AUTH_USER_MODEL = 'yourapp.YourCustomUser'

Make sure its migration was created and applied.

  1. Still failing?
    In psql or pgAdmin:
sql

CopyEdit

SELECT* FROM django_migrations WHERE app = 'auth';

If no rows, the auth migrations never ran.

Вернуться на верх