6

I am having an issue getting GitLab's CI to work with my Django project. I am using a postgres server and it appears I have set something up wrong. Any help would be much appreciated!

The error:

django.db.utils.OperationalError: could not connect to server: No such file or directory
    Is the server running locally and accepting
    connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

My .gitlab-ci.yml file:

image: python:3.6.1

services:
    - postgres:9.3

variables:
  POSTGRES_DB: project_ci_test
  POSTGRES_USER: postgres
  POSTGRES_PASSWORD: ""

test:
    script:
    - export DATABASE_URL=postgres://postgres:@postgres:5432/project_ci_test
    - apt-get update -qy
    - apt-get install -y python-dev python-pip
    - pip install -r requirements.txt
    - python manage.py makemigrations
    - python manage.py migrate
    - python manage.py test Project

In my django settings file:

DATABASES = {
    'default': {
        'ENGINE' : 'django.db.backends.postgresql_psycopg2',
        'NAME' : 'project_ci_test',
        'USER' : 'postgres',
        'PASSWORD': '',

    }
}

I am currently trying to utilize GitLab's shared runners. I am hoping someone has successfully gotten a Django project with postgres to work on GitLab's CI.

Thanks!

Tyler Bell
  • 837
  • 10
  • 30
  • Hello @TylerBell. I am having a similar dilemma, and I'm somewhat disappointed with the lack of examples for using a database. Is the rationale that you "tee up" the postgres image in 'services', but then fire it up and allocate it to a port in the test's 'export' line? – Mike Sadler Jul 24 '20 at 13:10

1 Answers1

11

Finally figured out the problem. Specifying the host and port in my Django project's settings file resolved the issue!

DATABASES = {
    'default': {
        'ENGINE' : 'django.db.backends.postgresql_psycopg2',
        'NAME' : 'project_ci_test',
        'USER' : 'postgres',
        'PASSWORD': '',
        'HOST' : 'postgres'
        'PORT' : '5432'

    }
}
Tyler Bell
  • 837
  • 10
  • 30