7

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!

2
  • 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? Commented Jul 24, 2020 at 13:10
  • You shouldn't be running makemigrations at all in that example, can be taken out. It's what you would run on your development instance not in CI. Commented Dec 27, 2023 at 18:20

1 Answer 1

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'

    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.