Using an External Relational Database With the Platform

Using an External Relational Database with the Platform

Introduction

Due to a recent project requirement, I researched how to configure an external PostgreSQL instance. The setup is similar across old and new versions, though some commands have changed.

The Platform and Relational Database Relationship

  • The official documentation states that Omnibus GitLab bundles PostgreSQL, and PostgreSQL is the only supported database
  • Docker images also include a PostgreSQL version
  • To use an external PostgreSQL, you must install it yourself and ensure the version meets the platform’s requirements
  • Version 16.0 and later requires PostgreSQL 13.0 or above
  • Version 15.0 and later defaults to PostgreSQL 13.0
  • Version 13.7 and later recommends PostgreSQL 12.0 or above
  • Version 13.0 and earlier requires PostgreSQL 11.0 or above
  • Version 12.8 and earlier requires PostgreSQL 9.6 or above and PostgreSQL 11.7 or below

Installation and Configuration

Installing the Relational Database

I used Docker to install PostgreSQL. The steps are as follows:

  1. Install using docker compose
  • I created a local folder called postgresql to hold the docker-compose.yml file
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
       mkdir postgresql
       cd postgresql
       ```

- I set an environment path: `export POSTGRESQL_HOME=/root/postgresql`
- Create the `docker-compose.yml` file: `sudo vi docker-compose.yml`

```yaml
       version: "3.7"

       services:
       postgres:
           image: postgres:13.11
           container_name: postgres_13
           restart: always
           volumes:
           - '$POSTGRESQL_HOME/data:/var/lib/postgresql/data'
           ports:
           - '5432:5432'
           environment:
           POSTGRES_DB: gitlabhq_production # Gitlab 預設的資料庫名稱
           POSTGRES_USER: postgres
           POSTGRES_PASSWORD: 1234
       ```

- Make sure you are in the same directory as `docker-compose.yml`, then run `docker-compose up -d`

2. Install using Docker Engine

```bash
   sudo docker run -d --name postgres_13 \ 
       --restart always \
       -p 5432:5432 \
       -v $POSTGRESQL_HOME/data:/var/lib/postgresql/data \
       -e POSTGRES_DB=eflab \
       -e POSTGRES_USER=postgres \
       -e POSTGRES_PASSWORD=1234 \
       postgres:13.11
  1. Test the connection. If you do not have a database tool, you can use pgadmin4
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
   # 沒有其他工具執行下列Docker可以使用 pgadmin4,這裡設定的port號是20080,故瀏覽器輸入 http://localhost:20080
   docker run -p 20080:80 \
       -e 'PGADMIN_DEFAULT_EMAIL=user@example.com' \
       -e 'PGADMIN_DEFAULT_PASSWORD=admin' \
       -d dpage/pgadmin4:latest
   ```

Here is a screenshot of my DBeaver connection:
![image](https://github.com/loustack17/loustack17.github.io/assets/33840759/6677b460-9289-41e5-95f6-742c2270cc10)
![image](https://github.com/loustack17/loustack17.github.io/assets/33840759/54b1a02c-261a-48e9-b326-508462db61dc)

### Changing the Platform Configuration

1. Enter the container

```bash
   docker exec -it gitlab bash
   ```

2. Edit the `gitlab.rb` file

```bash
   vi /etc/gitlab/gitlab.rb
   ```

3. In VIM, search for `postgresql` and enable the following settings

```bash
   /postgresql
1
2
3
4
5
6
7
8
   # Disable the bundled Omnibus provided PostgreSQL
   postgresql['enable'] = false # 這裡要設定為false,不然會使用內建的PostgreSQL

   # PostgreSQL connection details
   gitlab_rails['db_adapter'] = 'postgresql'
   gitlab_rails['db_encoding'] = 'unicode'
   gitlab_rails['db_host'] = 'your IP/hostname of database server'
   gitlab_rails['db_password'] = '1234'

image

image

  1. Run sudo gitlab-ctl reconfigure (this may take about 2 minutes)

When complete, you will see the following:

image

image

References