2019-05-30 06:21:27 +00:00
|
|
|
---
|
|
|
|
type: reference
|
|
|
|
---
|
|
|
|
|
2015-12-08 16:22:45 +00:00
|
|
|
# Using PostgreSQL
|
2015-11-25 13:41:14 +00:00
|
|
|
|
2015-12-08 16:22:45 +00:00
|
|
|
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.
|
2015-11-25 13:41:14 +00:00
|
|
|
|
2015-12-08 16:22:45 +00:00
|
|
|
## Use PostgreSQL with the Docker executor
|
2015-11-25 13:41:14 +00:00
|
|
|
|
2015-12-08 19:11:27 +00:00
|
|
|
If you are using [GitLab Runner](../runners/README.md) with the Docker executor
|
|
|
|
you basically have everything set up already.
|
2015-11-25 13:41:14 +00:00
|
|
|
|
2015-12-08 16:22:45 +00:00
|
|
|
First, in your `.gitlab-ci.yml` add:
|
2015-11-25 13:41:14 +00:00
|
|
|
|
2015-12-08 16:22:45 +00:00
|
|
|
```yaml
|
|
|
|
services:
|
2015-12-08 20:26:41 +00:00
|
|
|
- postgres:latest
|
2015-11-25 13:41:14 +00:00
|
|
|
|
2015-12-08 16:22:45 +00:00
|
|
|
variables:
|
|
|
|
POSTGRES_DB: nice_marmot
|
2015-12-08 20:26:41 +00:00
|
|
|
POSTGRES_USER: runner
|
2015-12-08 16:22:45 +00:00
|
|
|
POSTGRES_PASSWORD: ""
|
|
|
|
```
|
2015-11-25 13:41:14 +00:00
|
|
|
|
2015-12-08 19:11:27 +00:00
|
|
|
And then configure your application to use the database, for example:
|
2015-11-25 13:41:14 +00:00
|
|
|
|
2015-12-08 16:22:45 +00:00
|
|
|
```yaml
|
2015-12-08 21:49:05 +00:00
|
|
|
Host: postgres
|
2015-12-08 20:26:41 +00:00
|
|
|
User: runner
|
2015-12-08 16:22:45 +00:00
|
|
|
Password:
|
|
|
|
Database: nice_marmot
|
|
|
|
```
|
2015-11-25 13:41:14 +00:00
|
|
|
|
2015-12-08 21:49:05 +00:00
|
|
|
If you are wondering why we used `postgres` for the `Host`, read more at
|
2019-03-11 01:47:01 +00:00
|
|
|
[How services are linked to the job](../docker/using_docker_images.md#how-services-are-linked-to-the-job).
|
2015-12-08 21:49:05 +00:00
|
|
|
|
2015-12-08 16:22:45 +00:00
|
|
|
You can also use any other docker image available on [Docker Hub][hub-pg].
|
|
|
|
For example, to use PostgreSQL 9.3 the service becomes `postgres:9.3`.
|
2015-11-25 13:41:14 +00:00
|
|
|
|
2015-12-08 16:22:45 +00:00
|
|
|
The `postgres` image can accept some environment variables. For more details
|
|
|
|
check the documentation on [Docker Hub][hub-pg].
|
2015-11-25 13:41:14 +00:00
|
|
|
|
2015-12-08 16:22:45 +00:00
|
|
|
## Use PostgreSQL with the Shell executor
|
2015-11-25 13:41:14 +00:00
|
|
|
|
2015-12-08 16:22:45 +00:00
|
|
|
You can also use PostgreSQL on manually configured servers that are using
|
|
|
|
GitLab Runner with the Shell executor.
|
2015-11-25 13:41:14 +00:00
|
|
|
|
2015-12-08 16:22:45 +00:00
|
|
|
First install the PostgreSQL server:
|
2015-11-25 13:41:14 +00:00
|
|
|
|
2015-12-08 16:22:45 +00:00
|
|
|
```bash
|
|
|
|
sudo apt-get install -y postgresql postgresql-client libpq-dev
|
|
|
|
```
|
2015-11-25 13:41:14 +00:00
|
|
|
|
2015-12-08 20:26:41 +00:00
|
|
|
The next step is to create a user, so login to PostgreSQL:
|
2015-11-25 13:41:14 +00:00
|
|
|
|
2015-12-08 16:22:45 +00:00
|
|
|
```bash
|
|
|
|
sudo -u postgres psql -d template1
|
2015-12-08 20:26:41 +00:00
|
|
|
```
|
2015-11-25 13:41:14 +00:00
|
|
|
|
2015-12-08 20:26:41 +00:00
|
|
|
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.
|
2015-11-25 13:41:14 +00:00
|
|
|
|
2015-12-08 20:26:41 +00:00
|
|
|
*__Note:__ Do not type `template1=#`, this is part of the PostgreSQL prompt.*
|
2015-11-25 13:41:14 +00:00
|
|
|
|
2015-12-08 20:26:41 +00:00
|
|
|
```bash
|
|
|
|
template1=# CREATE USER runner WITH PASSWORD '$password' CREATEDB;
|
2015-12-08 16:22:45 +00:00
|
|
|
```
|
2015-11-25 13:41:14 +00:00
|
|
|
|
2015-12-08 20:26:41 +00:00
|
|
|
*__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`:
|
2015-11-25 13:41:14 +00:00
|
|
|
|
2015-12-08 16:22:45 +00:00
|
|
|
```bash
|
2015-12-08 20:26:41 +00:00
|
|
|
template1=# CREATE DATABASE nice_marmot OWNER runner;
|
|
|
|
```
|
|
|
|
|
|
|
|
If all went well you can now quit the database session:
|
2015-11-25 13:41:14 +00:00
|
|
|
|
2015-12-08 20:26:41 +00:00
|
|
|
```bash
|
|
|
|
template1=# \q
|
2015-12-08 16:22:45 +00:00
|
|
|
```
|
2015-11-25 13:41:14 +00:00
|
|
|
|
2015-12-08 20:26:41 +00:00
|
|
|
Now, try to connect to the newly created database with the user `runner` to
|
|
|
|
check that everything is in place.
|
2015-11-25 13:41:14 +00:00
|
|
|
|
2015-12-08 16:22:45 +00:00
|
|
|
```bash
|
2015-12-08 20:26:41 +00:00
|
|
|
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:
|
|
|
|
|
|
|
|
```yaml
|
2015-12-08 16:22:45 +00:00
|
|
|
Host: localhost
|
2015-12-08 20:26:41 +00:00
|
|
|
User: runner
|
|
|
|
Password: $password
|
2015-12-08 16:22:45 +00:00
|
|
|
Database: nice_marmot
|
|
|
|
```
|
2015-11-25 13:41:14 +00:00
|
|
|
|
2015-12-08 16:22:45 +00:00
|
|
|
## Example project
|
|
|
|
|
|
|
|
We have set up an [Example PostgreSQL Project][postgres-example-repo] for your
|
|
|
|
convenience that runs on [GitLab.com](https://gitlab.com) using our publicly
|
|
|
|
available [shared runners](../runners/README.md).
|
|
|
|
|
|
|
|
Want to hack on it? Simply fork it, commit and push your changes. Within a few
|
2017-02-13 16:59:57 +00:00
|
|
|
moments the changes will be picked by a public runner and the job will begin.
|
2015-12-08 16:22:45 +00:00
|
|
|
|
2016-05-07 04:29:24 +00:00
|
|
|
[hub-pg]: https://hub.docker.com/r/_/postgres/
|
2015-12-08 16:22:45 +00:00
|
|
|
[postgres-example-repo]: https://gitlab.com/gitlab-examples/postgres
|