Setting up postgis on Gitlab CI

I’ve being trying to setup gitlab CI with my django project. The project uses postgis extension. After all this setup I still get the error that postgis.control file could not be found

$ export PGPASSWORD=$POSTGRES_PASSWORD
$ psql -c "CREATE EXTENSION IF NOT EXISTS postgis;" -d $POSTGRES_DB -U $POSTGRES_USER -h "postgres"
ERROR:  could not open extension control file "/usr/share/postgresql/11/extension/postgis.control": No such file or directory
ERROR: Job failed: exit code 1

Here is my .gitlab-ci.yml file

image: python:3.6

stages:
  - test

services:
  - mdillon/postgis
  - postgres

variables:
  POSTGRES_DB: my_db
  POSTGRES_USER: my_user
  POSTGRES_PASSWORD: ""
  TESTFOLDER: "myapp/apps/api myapp/apps/logger"
  DATABASE_URL: "postgres://my_user:@mdillon-postgis/my_db"

test:
  stage: test
  image: mdillon/postgis
  before_script:
    - apt-get update -qy
    - export PGPASSWORD=$POSTGRES_PASSWORD
    - psql -c "CREATE EXTENSION IF NOT EXISTS postgis;" -d $POSTGRES_DB -U    $POSTGRES_USER -h "postgres"
    - psql -c "CREATE EXTENSION IF NOT EXISTS postgis_topology;" -d $POSTGRES_DB -U $POSTGRES_USER -h "postgres"
    - apt-get install -y openjdk-8-jre-headless libjpeg-dev zlib1g-dev software-properties-common ghostscript libxslt1-dev binutils libproj-dev libgdal-dev gdal-bin memcached libmemcached-dev 
    - export DEBIAN_FRONTEND=noninteractive;
    - pip install --upgrade pip
    - pip install -r requirements/base.pip
    - pip install flake8
  script:
    - python manage.py test $TESTFOLDER --noinput --settings=myapp.settings.gitlab_ci --parallel 4 --verbosity=2
  only:
    - master

I’ve Figured it out, totally my fault :man_facepalming: I should have read more on gitlab’s services to find out

@mensaah Can you post the working config file. Having this same issue. Thanks!

I have problem trying to do the same. If you solved it, could you share me you gitlab-ci please. thx

I am also having this problem. It would be nice if you could share your solution. :confused:

Sorry, I didn’t realize that many people had this issue

The Problem was the Host variable in the Database connection. In gitlab-ci, the host connection name should be a the service Name (with all / replaced with - or __ ) and everything after the colon ( : ) is stripped.see https://docs.gitlab.com/ee/ci/docker/using_docker_images.html#accessing-the-services. In the problem above the solution was to set the database host as mdillon-postgis or mdillon__postgis and then everything was fine.

image: python:3.6

stages:
  - test
 
services:
  - mdillon/postgis

variables:
  POSTGRES_DB: my_db
  POSTGRES_USER: my_user
  POSTGRES_PASSWORD: ""
  TESTFOLDER: "Test Folder"

test:
  stage: test
  before_script:
    - Some Before script commands
  script:
    - script
  only:
    - master

In my database connection using Django:

DATABASES = {
    'default': {
        'ENGINE': 'django.contrib.gis.db.backends.postgis',
        'NAME': 'my_db',
        'USER': 'my_user',
        'PASSWORD': '',
        'HOST': 'mdillon-postgis'
    }
}

Thank you!