I am starting a Django project and I am using gitlab's ci/cd on the shared runners and I use Postgres as the database. I have this weird problem that it seems like Django is creating the test database with the username "postgres" and I can't find a way to configure it's setting and change it to use the role named "runner". This causes a break in my ci/cd pipeline.
here is my .gitlab-ci.yml
:
image: python:3.6.5
services:
- postgres:latest
variables:
POSTGRES_DB: asdproject
POSTGRES_USER: runner
POSTGRES_PASSWORD: asdpassword
test:
script:
- whoami
- export DATABASE_URL=postgres://postgres:@postgres:5432/asdproject
- export PGPASSWORD=$POSTGRES_PASSWORD
- apt-get update -qy
- apt-get install -y python-dev python-pip
- pip install -r requirements.txt
- python manage.py test - settings=asd.gitlab_runner_settings
and my gitlab_runner_settings.py
file:
I tried many forms of changing settings.py that were recommended in questions but neither worked.
from asd.with_debug_settings import *
import sys
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'asdproject',
'USER': 'runner',
'PASSWORD': 'asdpassword',
'HOST': 'postgres',
'PORT': '5432',
'TEST': {
'NAME': 'asdtest',
'USER': 'runner'
},
}
}
The error I get while running my pipeline script in gitlab is:
---------------------------------------------------------------------
Ran 0 tests in 0.000s
OK
Using existing test database for alias 'default'...
System check identified no issues (0 silenced).
Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/django/db/backends/utils.py", line 83, in _execute
return self.cursor.execute(sql)
psycopg2.ProgrammingError: role "postgres" does not exist
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python3.6/site-packages/django/core/management/commands/test.py", line 26, in run_from_argv
super().run_from_argv(argv)
File "/usr/local/lib/python3.6/site-packages/django/core/management/base.py", line 316, in run_from_argv
self.execute(*args, **cmd_options)
File "/usr/local/lib/python3.6/site-packages/django/core/management/base.py", line 353, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python3.6/site-packages/django/core/management/commands/test.py", line 56, in handle
failures = test_runner.run_tests(test_labels)
File "/usr/local/lib/python3.6/site-packages/django/test/runner.py", line 607, in run_tests
self.teardown_databases(old_config)
File "/usr/local/lib/python3.6/site-packages/django_heroku/core.py", line 41, in teardown_databases
self._wipe_tables(connection)
File "/usr/local/lib/python3.6/site-packages/django_heroku/core.py", line 33, in _wipe_tables
"""
File "/usr/local/lib/python3.6/site-packages/django/db/backends/utils.py", line 68, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/usr/local/lib/python3.6/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/usr/local/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "/usr/local/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/usr/local/lib/python3.6/site-packages/django/db/backends/utils.py", line 83, in _execute
return self.cursor.execute(sql)
django.db.utils.ProgrammingError: role "postgres" does not exist
ERROR: Job failed: exit code 1
You need to have a host
'HOST': 'postgres',
this is wrong
It should be either 'localhost' or the address of remote server ip
The error occurs because when gitlab-ci runs tests it creates database according to the settings set in you yml file:
variables:
POSTGRES_DB: asdproject
POSTGRES_USER: runner
POSTGRES_PASSWORD: asdpassword
So runner
role and asdproject
database is created. But you set your database url as DATABASE_URL=postgres://postgres:@postgres:5432/asdproject
Where postgres
user is set.
Please try DATABASE_URL=postgres://runner:asdpassword@postgres:5432/asdproject
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.