Home:ALL Converter>Setting up PostGIS on Gitlab CI fails: psql could not connect to server: No such file or directory

Setting up PostGIS on Gitlab CI fails: psql could not connect to server: No such file or directory

Ask Time:2020-12-30T20:45:23         Author:Cesare

Json Formatter

I'm trying to create a PostGIS extension in the testing job of GitLab CI, as some of the tests require that extension on the PostgreSQL database to pass. My .gitlab-ci.yml looks like this:

image: docker:stable

stages:
  - build
  - test

variables:
  IMAGE: ${CI_REGISTRY}/${CI_PROJECT_NAMESPACE}/${CI_PROJECT_NAME}

build:
  stage: build
  services:
    - docker:dind
  variables:
    DOCKER_DRIVER: overlay2
  script:
    - docker login -u $CI_REGISTRY_USER -p $CI_JOB_TOKEN $CI_REGISTRY
    - docker pull $IMAGE:latest || true
    - docker build
        --cache-from $IMAGE:latest
        --tag $IMAGE:latest
        --file ./Dockerfile.prod
        "."
    - docker push $IMAGE:latest

test:
  stage: test
  image: $IMAGE:latest
  services:
    - postgis/postgis:latest
  variables:
    POSTGRES_DB: users
    POSTGRES_USER: runner
    POSTGRES_PASSWORD: runner
    DATABASE_TEST_URL: postgis://runner:runner@postgres:5432/users
  script:
    - psql -h "postgres" -U "$POSTGRES_USER" -d "$POSTGRES_DB" -c "CREATE EXTENSION \"postgis\";"
    - pytest "src/tests" -p no:warnings

The build job passes, test fails with psql: could not connect to server: No such file or directory for psql -h "postgres" -U "$POSTGRES_USER" -d "$POSTGRES_DB" -c "CREATE EXTENSION \"postgis\";" line. Why?

Author:Cesare,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/65507177/setting-up-postgis-on-gitlab-ci-fails-psql-could-not-connect-to-server-no-such
yy