debian-mirror-gitlab/doc/ci/services/postgres.md

127 lines
3.7 KiB
Markdown
Raw Normal View History

2019-09-04 21:01:54 +05:30
---
type: reference
---
2015-12-23 02:04:40 +05:30
# 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](../runners/README.md) with the Docker executor
you basically have everything set up already.
First, in your `.gitlab-ci.yml` add:
```yaml
services:
2020-04-08 14:13:33 +05:30
- postgres:12.2-alpine
2015-12-23 02:04:40 +05:30
variables:
POSTGRES_DB: nice_marmot
POSTGRES_USER: runner
POSTGRES_PASSWORD: ""
2020-04-08 14:13:33 +05:30
POSTGRES_HOST_AUTH_METHOD: trust
2015-12-23 02:04:40 +05:30
```
2019-10-12 21:52:04 +05:30
NOTE: **Note:**
2020-04-08 14:13:33 +05:30
The `POSTGRES_DB`, `POSTGRES_USER`, `POSTGRES_PASSWORD` and `POSTGRES_HOST_AUTH_METHOD`
variables can't be set in the GitLab UI. To set them, assign them to a variable
2019-10-12 21:52:04 +05:30
[in the UI](../variables/README.md#via-the-ui), and then assign that
2020-04-08 14:13:33 +05:30
variable to the `POSTGRES_DB`, `POSTGRES_USER`, `POSTGRES_PASSWORD` and `POSTGRES_HOST_AUTH_METHOD`
variables in your `.gitlab-ci.yml`.
2019-10-12 21:52:04 +05:30
2015-12-23 02:04:40 +05:30
And then configure your application to use the database, for example:
```yaml
Host: postgres
User: runner
2020-04-08 14:13:33 +05:30
Password: ''
2015-12-23 02:04:40 +05:30
Database: nice_marmot
```
If you are wondering why we used `postgres` for the `Host`, read more at
2019-07-07 11:18:12 +05:30
[How services are linked to the job](../docker/using_docker_images.md#how-services-are-linked-to-the-job).
2015-12-23 02:04:40 +05:30
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`.
The `postgres` image can accept some environment variables. For more details
check the documentation on [Docker Hub][hub-pg].
## 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:
2020-03-13 15:44:24 +05:30
```shell
2015-12-23 02:04:40 +05:30
sudo apt-get install -y postgresql postgresql-client libpq-dev
```
The next step is to create a user, so login to PostgreSQL:
2020-03-13 15:44:24 +05:30
```shell
2015-12-23 02:04:40 +05:30
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.*
2020-03-13 15:44:24 +05:30
```shell
2015-12-23 02:04:40 +05:30
template1=# CREATE USER runner WITH PASSWORD '$password' CREATEDB;
```
*__Note:__ Notice that we created the user with the privilege to be able to
2019-09-30 21:07:59 +05:30
create databases (`CREATEDB`). In the following steps we will create a database
2015-12-23 02:04:40 +05:30
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`:
2020-03-13 15:44:24 +05:30
```shell
2015-12-23 02:04:40 +05:30
template1=# CREATE DATABASE nice_marmot OWNER runner;
```
If all went well you can now quit the database session:
2020-03-13 15:44:24 +05:30
```shell
2015-12-23 02:04:40 +05:30
template1=# \q
```
Now, try to connect to the newly created database with the user `runner` to
check that everything is in place.
2020-03-13 15:44:24 +05:30
```shell
2015-12-23 02:04:40 +05:30
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
Host: localhost
User: runner
Password: $password
Database: nice_marmot
```
## 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).
2020-04-08 14:13:33 +05:30
Want to hack on it? Simply fork it, commit and push your changes. Within a few
2017-08-17 22:00:37 +05:30
moments the changes will be picked by a public runner and the job will begin.
2015-12-23 02:04:40 +05:30
2019-09-30 21:07:59 +05:30
[hub-pg]: https://hub.docker.com/_/postgres
2015-12-23 02:04:40 +05:30
[postgres-example-repo]: https://gitlab.com/gitlab-examples/postgres