25

I am trying to set up a CI runner at GitLab.com and I would like to use it to run tests that need a PostgreSQL database connection. The documentation of CI + PostgreSQL seems to be overly simple and it seems to tell that once the connection information is provided the database setup is done. I probably misunderstand the documentation as I fail to create the database. My gitlab-ci.yml looks like this

image: node:latest

services:
  - postgres:latest

variables:
  POSTGRES_DB: rx_reactive_test
  POSTGRES_USER: rx_reactive_tester
  POSTGRES_PASSWORD: "1esdf3143"

cache:
  paths:
  - node_modules/

postgresql:
  script:
   - yarn install
   - npm test

By running the command npm test`, it will run a few tests that would build the connection with the database. But those tests fail with the error message

pg-reactive
  ✓ should build connection with the test database.
  1) should run a query returning one row.


1 passing (23ms)
1 failing

1) pg-reactive should run a query returning one row.:
   Uncaught Error: connect ECONNREFUSED 127.0.0.1:5432
    at Object.exports._errnoException (util.js:1033:11)
    at exports._exceptionWithHostPort (util.js:1056:20)
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1099:14)

These tests runs well with my local test database so there must be a problem with my CI setup. I will appreciate any help on my problem :-)

1 Answer 1

22

In GitLab CI your job is running within a docker container. Postgres is therefore NOT running on localhost, but on a host that is reachable on "postgres" (the service name is the server name that resolves to the IP you can use to reach the DB).

It looks like CI sets an ENV variable called "CI" so that you can switch you DB connection for test in CI from "localhost:5432" to "postgres:5432" in case you are running within the GitLab CI environment.

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

2 Comments

hey, thanks for the answer but it's still a bit vague, so it should work with the following connection settings? connection: { host: 'postgres', user: 'user', password: 'test', port: 5432, database: 'withlove_test' }?
Unless you are running on Kubernetes executor, in that case services are exposed on localhost and not via host aliases. gitlab.com/gitlab-org/gitlab-runner/issues/2229

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.