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

126 lines
3.6 KiB
Markdown
Raw Normal View History

2019-09-04 21:01:54 +05:30
---
2020-06-23 00:09:42 +05:30
stage: Verify
group: Runner
2021-02-22 17:27:13 +05:30
info: To determine the technical writer assigned to the Stage/Group associated with this page, see https://about.gitlab.com/handbook/engineering/ux/technical-writing/#assignments
2019-09-04 21:01:54 +05:30
type: reference
---
2021-11-11 11:23:49 +05:30
# Using PostgreSQL **(FREE)**
2015-12-23 02:04:40 +05:30
2021-02-22 17:27:13 +05:30
As many applications depend on PostgreSQL as their database, you
2015-12-23 02:04:40 +05:30
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
2021-09-30 23:02:18 +05:30
If you're using [GitLab Runner](../runners/index.md) with the Docker executor,
2015-12-23 02:04:40 +05:30
you basically have everything set up already.
2021-11-11 11:23:49 +05:30
NOTE:
Variables set in the GitLab UI are not passed down to the service containers.
[Learn more](../variables/index.md).
2015-12-23 02:04:40 +05:30
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:
2021-11-11 11:23:49 +05:30
POSTGRES_DB: $POSTGRES_DB
POSTGRES_USER: $POSTGRES_USER
POSTGRES_PASSWORD: $POSTGRES_PASSWORD
2020-04-08 14:13:33 +05:30
POSTGRES_HOST_AUTH_METHOD: trust
2015-12-23 02:04:40 +05:30
```
And then configure your application to use the database, for example:
```yaml
Host: postgres
2021-12-11 22:18:48 +05:30
User: $POSTGRES_USER
Password: $POSTGRES_PASSWORD
Database: $POSTGRES_DB
2015-12-23 02:04:40 +05:30
```
2021-01-03 14:25:43 +05:30
If you're wondering why we used `postgres` for the `Host`, read more at
2021-04-29 21:17:54 +05:30
[How services are linked to the job](../services/index.md#how-services-are-linked-to-the-job).
2015-12-23 02:04:40 +05:30
2020-06-23 00:09:42 +05:30
You can also use any other Docker image available on [Docker Hub](https://hub.docker.com/_/postgres).
2021-01-03 14:25:43 +05:30
For example, to use PostgreSQL 9.3, the service becomes `postgres:9.3`.
2015-12-23 02:04:40 +05:30
2021-01-03 14:25:43 +05:30
The `postgres` image can accept some environment variables. For more details,
see the documentation on [Docker Hub](https://hub.docker.com/_/postgres).
2015-12-23 02:04:40 +05:30
## 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
```
2021-01-03 14:25:43 +05:30
The next step is to create a user, so sign in to PostgreSQL:
2015-12-23 02:04:40 +05:30
2020-03-13 15:44:24 +05:30
```shell
2015-12-23 02:04:40 +05:30
sudo -u postgres psql -d template1
```
2021-02-22 17:27:13 +05:30
Then create a user (in our case `runner`) which is used by your
2015-12-23 02:04:40 +05:30
application. Change `$password` in the command below to a real strong password.
2021-02-22 17:27:13 +05:30
NOTE:
2021-01-03 14:25:43 +05:30
Be sure to not enter `template1=#` in the following commands, as that's part of
the PostgreSQL prompt.
2015-12-23 02:04:40 +05:30
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;
```
2021-01-03 14:25:43 +05:30
The created user has the privilege to create databases (`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.
2015-12-23 02:04:40 +05:30
2021-01-03 14:25:43 +05:30
Create the database and grant all privileges to it for the user `runner`:
2015-12-23 02:04:40 +05:30
2020-03-13 15:44:24 +05:30
```shell
2015-12-23 02:04:40 +05:30
template1=# CREATE DATABASE nice_marmot OWNER runner;
```
2021-01-03 14:25:43 +05:30
If all went well, you can now quit the database session:
2015-12-23 02:04:40 +05:30
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
```
2021-01-03 14:25:43 +05:30
This command explicitly directs `psql` to connect to localhost to use the md5
2021-02-22 17:27:13 +05:30
authentication. If you omit this step, you are denied access.
2015-12-23 02:04:40 +05:30
Finally, configure your application to use the database, for example:
```yaml
Host: localhost
User: runner
Password: $password
Database: nice_marmot
```
## Example project
2020-04-22 19:07:51 +05:30
We have set up an [Example PostgreSQL Project](https://gitlab.com/gitlab-examples/postgres) for your
2015-12-23 02:04:40 +05:30
convenience that runs on [GitLab.com](https://gitlab.com) using our publicly
2021-09-30 23:02:18 +05:30
available [shared runners](../runners/index.md).
2015-12-23 02:04:40 +05:30
2021-01-03 14:25:43 +05:30
Want to hack on it? Fork it, commit, and push your changes. Within a few
2021-02-22 17:27:13 +05:30
moments the changes are picked by a public runner and the job begins.