1

I want to establish a gitlab test stage that uses a gitlab service postgres database. The problem is, that everytime I try to access the database via a script call in the pipeline I get the following error:

psql: error: could not connect to server: Connection refused
    Is the server running on host "postgres" (127.0.0.1) and accepting
    TCP/IP connections on port 5432?

yaml looks the following

image: some_image:latest

stages:
    - test

tests:
    image: node:latest
    stage: test
    services:
        - postgres:latest
    before_script:
        - apt-get update && apt-get install -y postgresql-client libpq-dev
        # access database script from another repo here through git clone
        - psql -U postgres -h postgres < ./create-database.sql
    script:
        - npm install
        - npm run tests
    only:
        - master

Am I missing something - is the database maybe not created and I am calling to soon?

2
  • 1
    How is your DB configured? SHould be done at this file since UI params are not passed to the DB container. Check the docs here and add VARIABLES docs.gitlab.com/ee/ci/services/postgres.html Commented Aug 1, 2022 at 17:20
  • Thanks, that did the trick - should have read the documentation. ;) Can you please put this into an actual answer so I can accept it for your reputation gain? Commented Aug 1, 2022 at 17:42

1 Answer 1

2

You're missing the configuration properties of the DB. They can not be configured on the UI - or they can but won't be passed to the actual DB service. So please add the following properties to the script:

variables:
  POSTGRES_DB: <db name>
  POSTGRES_USER: <db user>
  POSTGRES_PASSWORD: <some PW>
  POSTGRES_HOST_AUTH_METHOD: trust

Documentation link: https://docs.gitlab.com/ee/ci/services/postgres.html

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.