2020-09-17 11:09:24 -04:00
---
2022-05-29 20:08:35 -04:00
stage: Systems
2020-09-17 11:09:24 -04:00
group: Geo
2020-11-26 01:09:20 -05:00
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
2020-09-17 11:09:24 -04:00
---
2021-01-28 07:09:54 -05:00
# Geo with external PostgreSQL instances **(PREMIUM SELF)**
2020-09-17 11:09:24 -04:00
This document is relevant if you are using a PostgreSQL instance that is *not
2021-10-25 14:12:16 -04:00
managed by Omnibus*. This includes cloud-managed instances like Amazon RDS, or
2020-09-17 11:09:24 -04:00
manually installed and configured PostgreSQL instances.
2022-06-20 11:09:26 -04:00
Ensure that you are using one of the PostgreSQL versions that
[Omnibus ships with ](../../package_information/postgresql_versions.md )
to [avoid version mismatches ](../index.md#requirements-for-running-geo )
in case a Geo site has to be rebuilt.
2020-12-04 16:09:29 -05:00
NOTE:
2020-09-17 11:09:24 -04:00
We strongly recommend running Omnibus-managed instances as they are actively
developed and tested. We aim to be compatible with most external
(not managed by Omnibus) databases but we do not guarantee compatibility.
2022-07-15 05:10:09 -04:00
## **Primary** site
2020-09-17 11:09:24 -04:00
2022-07-15 05:10:09 -04:00
1. SSH into a **Rails node on your primary** site and login as root:
2020-09-17 11:09:24 -04:00
```shell
sudo -i
```
2021-06-02 14:10:01 -04:00
1. Edit `/etc/gitlab/gitlab.rb` and add:
2020-09-17 11:09:24 -04:00
```ruby
2021-06-02 14:10:01 -04:00
##
## Geo Primary role
## - configure dependent flags automatically to enable Geo
##
roles ['geo_primary_role']
##
2021-08-17 11:10:19 -04:00
## The unique identifier for the Geo site. See
## https://docs.gitlab.com/ee/user/admin_area/geo_nodes.html#common-settings
2021-06-02 14:10:01 -04:00
##
2021-08-17 11:10:19 -04:00
gitlab_rails['geo_node_name'] = '< site_name_here > '
2020-09-17 11:09:24 -04:00
```
2022-07-15 05:10:09 -04:00
1. Reconfigure the **Rails node** for the change to take effect:
2020-09-17 11:09:24 -04:00
```shell
gitlab-ctl reconfigure
```
2022-07-15 05:10:09 -04:00
1. Execute the command below on the **Rails node** to define the site as **primary** site:
2020-09-17 11:09:24 -04:00
```shell
gitlab-ctl set-geo-primary-node
```
2022-06-14 11:08:43 -04:00
This command uses your defined `external_url` in `/etc/gitlab/gitlab.rb` .
2020-09-17 11:09:24 -04:00
### Configure the external database to be replicated
To set up an external database, you can either:
2021-10-25 14:12:16 -04:00
- Set up [streaming replication ](https://www.postgresql.org/docs/12/warm-standby.html#STREAMING-REPLICATION-SLOTS ) yourself (for example Amazon RDS, bare metal not managed by Omnibus, and so on).
2020-09-17 11:09:24 -04:00
- Perform the Omnibus configuration manually as follows.
#### Leverage your cloud provider's tools to replicate the primary database
2022-07-15 05:10:09 -04:00
Given you have a primary site set up on AWS EC2 that uses RDS.
2020-09-17 11:09:24 -04:00
You can now just create a read-only replica in a different region and the
2022-06-14 11:08:43 -04:00
replication process is managed by AWS. Make sure you've set Network ACL (Access Control List), Subnet, and
2022-07-15 05:10:09 -04:00
Security Group according to your needs, so the secondary Rails node(s) can access the database.
2020-09-17 11:09:24 -04:00
The following instructions detail how to create a read-only replica for common
cloud providers:
- Amazon RDS - [Creating a Read Replica ](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/USER_ReadRepl.html#USER_ReadRepl.Create )
2022-06-09 05:09:12 -04:00
- Azure Database for PostgreSQL - [Create and manage read replicas in Azure Database for PostgreSQL ](https://docs.microsoft.com/en-us/azure/postgresql/single-server/how-to-read-replicas-portal )
2021-06-01 14:10:04 -04:00
- Google Cloud SQL - [Creating read replicas ](https://cloud.google.com/sql/docs/postgres/replication/create-replica )
2020-09-17 11:09:24 -04:00
2022-07-15 05:10:09 -04:00
Once your read-only replica is set up, you can skip to [configure your secondary site ](#configure-secondary-site-to-use-the-external-read-replica )
2020-09-17 11:09:24 -04:00
#### Manually configure the primary database for replication
The [`geo_primary_role` ](https://docs.gitlab.com/omnibus/roles/#gitlab-geo-roles )
configures the **primary** node's database to be replicated by making changes to
`pg_hba.conf` and `postgresql.conf` . Make the following configuration changes
manually to your external database configuration and ensure that you restart PostgreSQL
afterwards for the changes to take effect:
```plaintext
##
## Geo Primary Role
## - pg_hba.conf
##
host all all < trusted primary IP > /32 md5
host replication gitlab_replicator < trusted primary IP > /32 md5
host all all < trusted secondary IP > /32 md5
host replication gitlab_replicator < trusted secondary IP > /32 md5
```
```plaintext
##
## Geo Primary Role
## - postgresql.conf
##
wal_level = hot_standby
max_wal_senders = 10
wal_keep_segments = 50
max_replication_slots = 1 # number of secondary instances
hot_standby = on
```
2022-07-15 05:10:09 -04:00
## **Secondary** sites
2020-09-17 11:09:24 -04:00
### Manually configure the replica database
Make the following configuration changes manually to your `pg_hba.conf` and `postgresql.conf`
of your external replica database and ensure that you restart PostgreSQL afterwards
for the changes to take effect:
```plaintext
##
## Geo Secondary Role
## - pg_hba.conf
##
host all all < trusted secondary IP > /32 md5
host replication gitlab_replicator < trusted secondary IP > /32 md5
host all all < trusted primary IP > /24 md5
```
```plaintext
##
## Geo Secondary Role
## - postgresql.conf
##
wal_level = hot_standby
max_wal_senders = 10
wal_keep_segments = 10
hot_standby = on
```
2022-07-15 05:10:09 -04:00
### Configure **secondary** site to use the external read-replica
2020-09-17 11:09:24 -04:00
With Omnibus, the
[`geo_secondary_role` ](https://docs.gitlab.com/omnibus/roles/#gitlab-geo-roles )
has three main functions:
1. Configure the replica database.
1. Configure the tracking database.
1. Enable the [Geo Log Cursor ](../index.md#geo-log-cursor ) (not covered in this section).
To configure the connection to the external read-replica database and enable Log Cursor:
2022-07-15 05:10:09 -04:00
1. SSH into each **Rails, Sidekiq and Geo Log Cursor** node on your **secondary** site and login as root:
2020-09-17 11:09:24 -04:00
```shell
sudo -i
```
1. Edit `/etc/gitlab/gitlab.rb` and add the following
```ruby
##
## Geo Secondary role
## - configure dependent flags automatically to enable Geo
##
roles ['geo_secondary_role']
# note this is shared between both databases,
# make sure you define the same password in both
gitlab_rails['db_password'] = '< your_password_here > '
gitlab_rails['db_username'] = 'gitlab'
gitlab_rails['db_host'] = '< database_read_replica_host > '
# Disable the bundled Omnibus PostgreSQL, since we are
# using an external PostgreSQL
postgresql['enable'] = false
```
1. Save the file and [reconfigure GitLab ](../../restart_gitlab.md#omnibus-gitlab-reconfigure )
### Configure the tracking database
2022-07-15 05:10:09 -04:00
**Secondary** sites use a separate PostgreSQL installation as a tracking
2020-09-17 11:09:24 -04:00
database to keep track of replication status and automatically recover from
potential replication issues. Omnibus automatically configures a tracking database
when `roles ['geo_secondary_role']` is set.
2021-12-31 04:15:53 -05:00
If you want to run this database external to Omnibus GitLab, use the following instructions.
2020-09-17 11:09:24 -04:00
If you are using a cloud-managed service for the tracking database, you may need
to grant additional roles to your tracking database user (by default, this is
`gitlab_geo` ):
- Amazon RDS requires the [`rds_superuser` ](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/Appendix.PostgreSQL.CommonDBATasks.html#Appendix.PostgreSQL.CommonDBATasks.Roles ) role.
2022-06-09 05:09:12 -04:00
- Azure Database for PostgreSQL requires the [`azure_pg_admin` ](https://docs.microsoft.com/en-us/azure/postgresql/single-server/how-to-create-users#how-to-create-additional-admin-users-in-azure-database-for-postgresql ) role.
2021-06-01 14:10:04 -04:00
- Google Cloud SQL requires the [`cloudsqlsuperuser` ](https://cloud.google.com/sql/docs/postgres/users#default-users ) role.
This is for the installation of extensions during installation and upgrades. As an alternative,
[ensure the extensions are installed manually, and read about the problems that may arise during future GitLab upgrades ](../../../install/postgresql_extensions.md ).
2020-09-17 11:09:24 -04:00
2021-06-02 20:09:49 -04:00
To setup an external tracking database, follow the instructions below:
2020-09-17 11:09:24 -04:00
2020-12-04 16:09:29 -05:00
NOTE:
2021-10-25 14:12:16 -04:00
If you want to use Amazon RDS as a tracking database, make sure it has access to
2020-09-17 11:09:24 -04:00
the secondary database. Unfortunately, just assigning the same security group is not enough as
outbound rules do not apply to RDS PostgreSQL databases. Therefore, you need to explicitly add an inbound
rule to the read-replica's security group allowing any TCP traffic from
the tracking database on port 5432.
2021-06-02 20:09:49 -04:00
1. Set up PostgreSQL according to the
[database requirements document ](../../../install/requirements.md#database ).
1. Set up a `gitlab_geo` user with a password of your choice, create the `gitlabhq_geo_production` database, and make the user an owner of the database. You can see an example of this setup in the [installation from source documentation ](../../../install/installation.md#6-database ).
2021-07-01 14:07:29 -04:00
1. If you are **not** using a cloud-managed PostgreSQL database, ensure that your secondary
2022-07-15 05:10:09 -04:00
site can communicate with your tracking database by manually changing the
2021-06-02 20:09:49 -04:00
`pg_hba.conf` that is associated with your tracking database.
2020-09-17 11:09:24 -04:00
Remember to restart PostgreSQL afterwards for the changes to take effect:
```plaintext
##
## Geo Tracking Database Role
## - pg_hba.conf
##
host all all < trusted tracking IP > /32 md5
host all all < trusted secondary IP > /32 md5
```
1. SSH into a GitLab **secondary** server and login as root:
```shell
sudo -i
```
1. Edit `/etc/gitlab/gitlab.rb` with the connection parameters and credentials for
the machine with the PostgreSQL instance:
```ruby
geo_secondary['db_username'] = 'gitlab_geo'
geo_secondary['db_password'] = '< your_password_here > '
geo_secondary['db_host'] = '< tracking_database_host > '
geo_secondary['db_port'] = < tracking_database_port > # change to the correct port
geo_postgresql['enable'] = false # don't use internal managed instance
```
1. Save the file and [reconfigure GitLab ](../../restart_gitlab.md#omnibus-gitlab-reconfigure )
2021-07-28 17:08:53 -04:00
1. The reconfigure should automatically create the database. If needed, you can perform this task manually. This task (whether run by itself or during reconfigure) requires the database user to be a superuser.
2020-09-17 11:09:24 -04:00
```shell
2022-04-21 17:08:43 -04:00
gitlab-rake db:create:geo
2021-06-02 20:09:49 -04:00
```
2021-09-09 11:09:24 -04:00
1. The reconfigure should automatically migrate the database. You can migrate the database manually if needed, for example if `geo_secondary['auto_migrate'] = false` :
2021-06-02 20:09:49 -04:00
```shell
2022-04-21 17:08:43 -04:00
gitlab-rake db:migrate:geo
2020-09-17 11:09:24 -04:00
```