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 :-)
If you're hosting GitLab on a cloud provider, you can optionally use a managed service for PostgreSQL. For example, AWS offers a managed Relational Database Service (RDS) that runs PostgreSQL.
Connecting to a Database In order to connect to a database you need to know the name of your target database, the host name and port number of the server, and what user name you want to connect as. psql can be told about those parameters via command line options, namely -d , -h , -p , and -U respectively.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With