> Source: https://docs.cyberelements.io/bastion/4.6/Installation/Dependencies/PostgreSQLCluster/PostgreSQLClusterIntroduction/

# Introduction

A virtual appliance can be used to deploy a PostgreSQL cluster.
This PostgreSQL cluster is typically operated with CyberElements Bastion platforms with cluster architecture to ensure end-to-end redundancy.

> **Note**
>
> Please note that the operation and updating of virtual appliances provided by Systancia are the responsibility of the Customer as defined in [the product lifecycle](https://docs.cyberelements.io/ProductLifeCycle/OSAndThirdPartySoftwareSupport/#virtual-appliances).

The virtual appliance includes all the components needed to deploy a PostgreSQL cluster, as well as a utility to facilitate its installation. It can be downloaded from the Systancia Marketplace (requires creation of a customer account): [Systancia Marketplace](https://www.systancia.com/en/my-systancia/marketplace/systancia-cleanroom/downloads-cleanroom/)

The PostgreSQL cluster is based on three PostgreSQL nodes and the use of a virtual IP address.
The use of three nodes ensures service resilience even if one node is lost (two active nodes being the minimum). This works using the following components:

- [Patroni](https://github.com/patroni/patroni)
- [Etcd](https://etcd.io/)
- [Keepalived](https://keepalived.org/)

Together, they allow automated management of virtual IP and replication.

This PostgreSQL cluster does not have a node that is always designated as `LEADER`; the `LEADER` alternates depending on the circumstances encountered by the cluster. If the `LEADER` were to fail, a new `LEADER` would be elected by absolute majority and the virtual IP would switch to the latter. Note that it takes approximately 20 seconds to reliably identify the loss of the `LEADER` and switch the virtual IP to the new `LEADER`.
The `LEADER` node is the only one with read/write access to the PostgreSQL database; the `REPLICA` nodes are read-only.

The typical architecture of a CyberElements Bastion cluster based on a PostgreSQL cluster is as follows:

```mermaid
flowchart LR
    subgraph Cloud or DMZ
        MED-M(Mediation Controller<br/>MASTER)
        MED-S(Mediation Controller<br/>SLAVE)
    end
    subgraph LAN
        GW-1(Edge Gateway 1)
        GW-2(Edge Gateway 2)
        subgraph PSQL [PostgreSQL Cluster]
            direction TB
            PSQL-1(PostgreSQL<br/>node 1)
            PSQL-2(PostgreSQL<br/>node 2)
            PSQL-3(PostgreSQL<br/>node 3)
        end
    end

    MED-M <--> |Cluster interconnection| MED-S
    GW-1 & GW-2 --> |TLS| MED-M & MED-S
    MED-M & MED-S ~~~ GW-1 & GW-2
    GW-1 & GW-2 -..-> |SQL| PSQL

    PSQL-1 <--> |Cluster interconnection| PSQL-2 <--> |Cluster interconnection| PSQL-3 <--> |Cluster interconnection| PSQL-1
```

To install a PostgreSQL cluster with CyberElements virtual appliances, please follow the steps below:

- (1) Prerequisites

  ---

  Defining the prerequisites for the PostgreSQL cluster.

  [→ See more](https://docs.cyberelements.io/bastion/4.6/Installation/Dependencies/PostgreSQLCluster/Installation/Prerequisites/)
- (2) Importing the virtual appliance

  ---

  The virtual appliance must be imported three times to prepare for the installation of the PostgreSQL cluster.

  [→ See more](https://docs.cyberelements.io/bastion/4.6/Installation/Dependencies/PostgreSQLCluster/Installation/Importation/)
- (3) Installing the PostgreSQL cluster

  ---

  Instructions for configuring and installing the PostgreSQL cluster.

  [→ See more](https://docs.cyberelements.io/bastion/4.6/Installation/Dependencies/PostgreSQLCluster/Installation/Install/)
