Django tests in GitLab CI always use PostgreSQL instead of SQLite despite APP_ENV override
I am running Django tests inside GitLab CI and trying to switch between SQLite (for local/test) and PostgreSQL (for production).
My settings structure looks like this:
settings/_init_.py:
import os
APP_ENV = os.getenv("APP_ENV", "local")
if APP_ENV == "local":
from .local import DATABASES
elif APP_ENV == "production":
from .production import DATABASES
else:
raise ValueError("Incorrect configuration")
production.py:
import os
DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql",
"NAME": os.getenv("POSTGRES_DB"),
"USER": os.getenv("POSTGRES_USER"),
"PASSWORD": os.getenv("POSTGRES_PASSWORD"),
"HOST": os.getenv("POSTGRES_HOST"),
"PORT": os.getenv("POSTGRES_PORT"),
}
}
local.py:
from .base import BASE_DIR
DATABASES = {
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": BASE_DIR / "db.sqlite3",
}
}
In GitLab CI (.gitlab-ci.yml), my test job looks like this:
backend:test:
stage: test
image: python:3.13-slim
variables:
APP_ENV: local
services:
- name: postgres:15-alpine
alias: postgres
script: - uv run python manage.py test
What I expected
Tests should use SQLite when APP_ENV=local.
What actually happens
Tests always use PostgreSQL and fails with error.
Traceback:
django.db.utils.OperationalError: connection to server on socket "/var/run/[MASKED]ql/.s.PGSQL.5432" failed: No such file or directory
Is the server running locally and accepting connections on that socket?