GitLab Documentation

Using PostgreSQL

As many applications depend on PostgreSQL as their database, you will eventually need it in order for your tests to run. Below you are guided how to do this with the Docker and Shell executors of GitLab Runner.

Use PostgreSQL with the Docker executor

If you are using GitLab Runner with the Docker executor you basically have everything set up already.

First, in your .gitlab-ci.yml add:

services:
  - postgres:latest

variables:
  POSTGRES_DB: nice_marmot
  POSTGRES_USER: runner
  POSTGRES_PASSWORD: ""

And then configure your application to use the database, for example:

Host: postgres
User: runner
Password:
Database: nice_marmot

If you are wondering why we used postgres for the Host, read more at How is service linked to the job.

You can also use any other docker image available on Docker Hub. For example, to use PostgreSQL 9.3 the service becomes postgres:9.3.

The postgres image can accept some environment variables. For more details check the documentation on Docker Hub.

Use PostgreSQL with the Shell executor

You can also use PostgreSQL on manually configured servers that are using GitLab Runner with the Shell executor.

First install the PostgreSQL server:

sudo apt-get install -y postgresql postgresql-client libpq-dev

The next step is to create a user, so login to PostgreSQL:

sudo -u postgres psql -d template1

Then create a user (in our case runner) which will be used by your application. Change $password in the command below to a real strong password.

Note: Do not type template1=#, this is part of the PostgreSQL prompt.

template1=# CREATE USER runner WITH PASSWORD '$password' CREATEDB;

Note: Notice that we created the user with the privilege to be able to create databases (CREATEDB). In the following steps we will create a database explicitly for that user but having that privilege can be useful if in your testing framework you have tools that drop and create databases.

Create the database and grant all privileges on it for the user runner:

template1=# CREATE DATABASE nice_marmot OWNER runner;

If all went well you can now quit the database session:

template1=# \q

Now, try to connect to the newly created database with the user runner to check that everything is in place.

psql -U runner -h localhost -d nice_marmot -W

Note: We are explicitly telling psql to connect to localhost in order to use the md5 authentication. If you omit this step you will be denied access.

Finally, configure your application to use the database, for example:

Host: localhost
User: runner
Password: $password
Database: nice_marmot

Example project

We have set up an Example PostgreSQL Project for your convenience that runs on GitLab.com using our publicly available shared runners.

Want to hack on it? Simply fork it, commit and push your changes. Within a few moments the changes will be picked by a public runner and the job will begin.


Leave a comment below if you have any feedback on the documentation. For support and other inquires, see getting help.