Reputation: 3097
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 :-)
Upvotes: 25
Views: 26059
Reputation: 10179
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.
Upvotes: 22