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:
Install using docker compose
I created a local folder called postgresql to hold the docker-compose.yml file
mkdir postgresql
cd postgresql
```- I set an environment path: `exportPOSTGRESQL_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
Test the connection. If you do not have a database tool, you can use pgadmin4
# 沒有其他工具執行下列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:
### Changing the Platform Configuration1. 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'
Run sudo gitlab-ctl reconfigure (this may take about 2 minutes)