> Source: https://docs.cyberelements.io/bastion/4.7/sk/Installation/Dependencies/PostgreSQLCluster/Operation/PsqlCertsRenew/

# Renewing PostgreSQL cluster certificates

## Preparations

### Certificate renewal

Before performing any actions on the PostgreSQL cluster nodes, please prepare new certificates for each of the cluster nodes. As a reminder, certificates must comply with several constraints specified in [the PostgreSQL cluster prerequisites](https://docs.cyberelements.io/bastion/4.7/sk/Installation/Dependencies/PostgreSQLCluster/Installation/Prerequisites/#certificates).

If you want to make the change on the fly without impacting production, you must meet the following conditions:

- The certification authority has not been renewed since the last generation of PostgreSQL node certificates
- The certification authority will not expire during the validity period of the new certificates
- You can still generate and sign certificates with the same certification authority that was used when the previous PostgreSQL node certificates were generated
- The PostgreSQL node certificates have not yet expired

If these conditions are met, then you can consider renewing the certificates on the fly without interrupting production.

To regenerate certificates using the `openssl` PKI indicated, optionally, in [the PostgreSQL cluster installation documentation](https://docs.cyberelements.io/bastion/4.7/sk/Installation/Dependencies/PostgreSQLCluster/Installation/Install/#creating-a-pki-to-generate-postgresql-node-certificates), you must reuse the certification authority key and then replay the certificate generation commands. These commands overwrite the files from the previous generation: back them up first, for example with the following commands:

```
1
2
```

If it is not possible to renew the certificates on the fly and you are using a PKI via `openssl`, we recommend that you [create a new one](https://docs.cyberelements.io/bastion/4.7/sk/Installation/Dependencies/PostgreSQLCluster/Installation/Install/#creating-a-pki-to-generate-postgresql-node-certificates).

### Preparing the certificates

If the certification authority has changed, you will need to send in `/tmp/` the root certification authority certificate with a `.crt` extension to each of the PostgreSQL nodes.

You will also need to send in `/tmp/` the certificate and its associated private key to each of the PostgreSQL nodes. If you only have one `.p12` file available, please run the following commands to extract the certificate (psql.crt) and private key (psql.key):

| Value | Variable | Definition |
|---|---|---|
|  | `P12_CERT` | Name of the `.p12` certificate file of the node. |

```
1
2
```

The `-noenc` option writes the key without encryption, as `etcd` and `patroni` use it.

## Certificate renewal

> **Warning!**
>
> If you can and wish to perform a live update of the certificates with minimal production downtime (a few seconds and user logout), work on one node at a time, always keeping at least two PostgreSQL nodes operational, in the following order:
>
> 1. Renew the certificates of the two `REPLICA` nodes, one after the other (see how to [retrieve information from the `LEADER` node](https://docs.cyberelements.io/bastion/4.7/sk/Installation/Dependencies/PostgreSQLCluster/Operation/PsqlGetLeader/)).
> 2. [Switch the `LEADER`](https://docs.cyberelements.io/bastion/4.7/sk/Installation/Dependencies/PostgreSQLCluster/Operation/PsqlChangeLeader/) to one of the nodes whose certificates have already been renewed: this is the action that logs users out and makes the platform unavailable for a few seconds.
> 3. Renew the certificates of the former `LEADER`, now a `REPLICA`.
>
> Restarting a `REPLICA` node does not interrupt the service; a restarted node takes a few minutes to rejoin the cluster.

To simplify the following actions, please customise the variables in the commands below with your values (modify the customisation for each PostgreSQL node):

| Value | Variable | Definition |
|---|---|---|
|  | `PSQL_CERT` | New PostgreSQL node certificate. |
|  | `PSQL_KEY` | Key associated with the new PostgreSQL node certificate. |
|  | `PSQL_CA` | Certificate from the root certification authority that was used to generate the PostgreSQL node certificate. |

Connect via SSH with the `systancia` account to the PostgreSQL node whose certificates you want to update, switch to the `root` account with the `su -` command (the `root` account is not accepted directly over SSH), and run the following commands to replace the certificates:

```
1
2
3
4
5
6
```

If the certification authority has changed, also run the following commands:

```
1
2
3
4
```

To finalise the certificate changes, you must restart the PostgreSQL node:

```
1
```

Before moving on to the next node, wait until the node is back in the cluster with the `streaming` state (or `running` for the `LEADER`):

```
1
```

You can then [check the certificates](https://docs.cyberelements.io/bastion/4.7/sk/Installation/Dependencies/PostgreSQLCluster/Operation/PsqlCertsVerification/) of the node, in particular [the certificate presented by each service](https://docs.cyberelements.io/bastion/4.7/sk/Installation/Dependencies/PostgreSQLCluster/Operation/PsqlCertsVerification/#checking-the-certificate-presented-by-a-service).

Once the node has restarted, delete the files placed in `/tmp/`, since the private key is stored there unencrypted (as well as the `.p12` file, if you placed one there):

```
1
2
```
