Using PostgreSQL
As many applications depend on PostgreSQL as their database, you 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’re using GitLab Runner with the Docker executor, you basically have everything set up already.
First, in your .gitlab-ci.yml
add:
services:
- postgres:12.2-alpine
variables:
POSTGRES_DB: $POSTGRES_DB
POSTGRES_USER: $POSTGRES_USER
POSTGRES_PASSWORD: $POSTGRES_PASSWORD
POSTGRES_HOST_AUTH_METHOD: trust
And then configure your application to use the database, for example:
Host: postgres
User: $POSTGRES_USER
Password: $POSTGRES_PASSWORD
Database: $POSTGRES_DB
If you’re wondering why we used postgres
for the Host
, read more at
How services are linked to the job.
You can also use any other Docker image available on .
For example, to use PostgreSQL 9.3, the service becomes The You can also use PostgreSQL on manually configured servers that are using
GitLab Runner with the Shell executor.
First install the PostgreSQL server:
The next step is to create a user, so sign in to PostgreSQL:
Then create a user (in our case The created user has the privilege to create databases ( Create the database and grant all privileges to it for the user If all went well, you can now quit the database session:
Now, try to connect to the newly created database with the user This command explicitly directs Finally, configure your application to use the database, for example:
We have set up an shared runners.
Want to hack on it? Fork it, commit, and push your changes. Within a few
moments the changes are picked by a public runner and the job begins.
postgres:9.3
.
postgres
image can accept some environment variables. For more details,
see the documentation on .
Use PostgreSQL with the Shell executor
sudo apt-get install -y postgresql postgresql-client libpq-dev
sudo -u postgres psql -d template1
runner
) which is used by your
application. Change $password
in the command below to a real strong password.
template1=#
in the following commands, as that’s part of
the PostgreSQL prompt.template1=# CREATE USER runner WITH PASSWORD '$password' CREATEDB;
CREATEDB
). The
following steps describe how to 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.
runner
:
template1=# CREATE DATABASE nice_marmot OWNER runner;
template1=# \q
runner
to
check that everything is in place.
psql -U runner -h localhost -d nice_marmot -W
psql
to connect to localhost to use the md5
authentication. If you omit this step, you are denied access.
Host: localhost
User: runner
Password: $password
Database: nice_marmot
Example project