GitLab CI generates database name for Postgres that is longer than the 63 character limit (django.core.exceptions.ImproperlyConfigured)
I'm having trouble using postgres as a service in a GitLab CI job that runs pytest for a Django application.
Here is my GitLab CI job:
Pytest:
image: python:3.8
stage: test
services:
- postgres:13.1
- redis:6.0.9-alpine
variables:
DJANGO_SETTINGS_MODULE: "backend.settings.gitlab_ci"
# the database name is too long, setting here explicitly
# I tried setting DATABASE_URL and it didn't work:
# DATABASE_URL: "postgresql://postgres:postgres@postgres:5432/postgres"
SECRET_KEY: "secret"
DEBUG: "1"
POSTGRES_HOST_AUTH_METHOD: trust
script:
- cd backend
- pip install -r requirements/base.txt
- pip install -r requirements/test.txt
- flake8
- black -l 79 -S --diff .
- pytest --cov --cov-config=.coveragerc
coverage: '/TOTAL.+ ([0-9]{1,3}\.[0-9]{1,3}%)/'
In backend/settings/gitlab_ci.py
I have the following DATABASES
settings:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'postgres',
'USER': 'postgres',
'PASSWORD': 'postgres',
'HOST': 'postgres',
'PORT': '5432',
},
}
Here are relevant logs from the failing CI job.
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = <django.db.backends.postgresql.base.DatabaseWrapper object at 0x7f8d71b8de20>
def get_connection_params(self):
settings_dict = self.settings_dict
# None may be used to connect to the default 'postgres' db
if settings_dict['NAME'] == '':
raise ImproperlyConfigured(
"settings.DATABASES is improperly configured. "
"Please supply the NAME value.")
if len(settings_dict['NAME'] or '') > self.ops.max_name_length():
> raise ImproperlyConfigured(
"The database name '%s' (%d characters) is longer than "
"PostgreSQL's limit of %d characters. Supply a shorter NAME "
"in settings.DATABASES." % (
settings_dict['NAME'],
len(settings_dict['NAME']),
self.ops.max_name_length(),
)
)
E django.core.exceptions.ImproperlyConfigured: The database name 'test_/runner-72989761-project-22626407-concurrent-0-a737df6e05b7c794-build-2/postgres' (85 characters) is longer than PostgreSQL's limit of 63 characters. Supply a shorter NAME in settings.DATABASES.
/usr/local/lib/python3.8/site-packages/django/db/backends/postgresql/base.py:161: ImproperlyConfigured
Here are a link to all of the pipelines on GitLab https://gitlab.com/briancaffey/sec-filings-app/-/pipelines.
Here are some things I have tried in order to fix the issue:
- setting the
NAME
value in thedefault
forDATABASES
toci
. - setting
DATABASE_URL: "postgresql://postgres:postgres@postgres:5432/postgres"
in the CI job variables - Reverting to postgres version 11.5, the version I have used on several other projects in a similar way where the database name length is not an issue.
- Setting the
NAME
toNone
(from the logs:# None may be used to connect to the default 'postgres' db
)
I think GitLab CI is somehow changing the value of the database name, since it showing up as:
test_/runner-72989761-project-22626407-concurrent-0-a737df6e05b7c794-build-2/postgres
in the failing CI job logs.