← Start of tutorial

WIUNIX ACADEMY

PostgreSQL with Patroni: replication, quorum and safe switchover

Page 2 of 6. Review the earlier steps and prerequisites before continuing.

YOUR SYSTEM. YOUR STEPS.

Where are you working?

Choose your OS and version. The prerequisites, commands and troubleshooting follow your environment.

Rocky Linux 10.2Reviewed against official documentation ·

USE YOUR OWN VALUES

Configure this tutorial

Set the example values for your environment; related explanations and copy panels update together. Never enter passwords, private keys or tokens. Values stay in this tab and are not sent to the server or share links. Check actual DNS, certificates and network policy before running commands.

Different IP addresses, domains or ports? Open the settings below, change the values and select “Apply to tutorial”, then copy the updated commands.

Set values for my environment11 editable values
02

LET’S BUILD

Install packages without a default cluster

Fresh VMs only. Masks prevent vendor services racing Patroni; disabling automatic main-cluster creation prevents an unintended database on installation. The existing package configuration is inspected first and belongs only to these disposable machines. The PGDG helper configures the matching signed repository. Stop for a missing candidate, PostgreSQL major other than18, or Patroni older than4.1.3 (the etcd authentication fixes are required). Ubuntu26.10 uses its own4.1.4 Patroni candidate, not a substituted suite. Do not remove masks or use pg_ctlcluster later: the dedicated Patroni unit will own PostgreSQL.

Bash

Disposable database VM; Bash as administrator

(
set -euo pipefail
. /etc/os-release
test "$ID" = 'rocky'
test "$VERSION_ID" = '10.2'
test "$(uname -m)" = x86_64
sudo systemctl mask postgresql.service postgresql-18.service patroni.service
sudo dnf install dnf-plugins-core
sudo dnf config-manager --set-enabled crb
sudo dnf install epel-release
sudo dnf install https://download.postgresql.org/pub/repos/yum/reporpms/EL-10-x86_64/pgdg-redhat-repo-latest.noarch.rpm
# EL10 has no PostgreSQL module reset in this path.
sudo dnf info postgresql18-server patroni patroni-etcd
sudo dnf install postgresql18-server postgresql18 patroni patroni-etcd python3 curl ca-certificates openssl nano policycoreutils-python-utils audit
/usr/pgsql-18/bin/postgres --version
/usr/bin/patroni --version
rpm -q postgresql18-server patroni patroni-etcd python3
test "$(/usr/pgsql-18/bin/postgres --version | awk '{print $3}' | cut -d. -f1)" = 18
sudo systemctl is-active postgresql.service postgresql-18.service patroni.service || true
)
How do I know it worked?

PostgreSQL18 and Patroni≥4.1.3 installed, no main cluster and no vendor daemon running.

03

LET’S BUILD

Install the reviewed etcd binaries

On each fresh VM download the official etcd3.7.3 release and its checksum file over HTTPS, compare before extraction and install only in a new version-specific directory. A same-publisher checksum detects mismatches; it is not independent publisher-signature verification. No service starts here.

Inspect support and upgrade guidance before choosing another release. This is a fresh-install panel, not an in-place replacement of a running DCS. Retain the unique download directory until validation is complete.

Bash

Disposable database VM; Bash as administrator

set -eu
umask 022
test ! -e /opt/academy-etcd-3.7.3
test ! -L /opt/academy-etcd-3.7.3
download=$(mktemp -d)
cd "$download"
curl -fLO https://github.com/etcd-io/etcd/releases/download/v3.7.3/etcd-v3.7.3-linux-amd64.tar.gz
curl -fLO https://github.com/etcd-io/etcd/releases/download/v3.7.3/SHA256SUMS
grep ' etcd-v3.7.3-linux-amd64.tar.gz$' SHA256SUMS > selected.sha256
test "$(wc -l < selected.sha256)" -eq 1
sha256sum -c selected.sha256
tar -xzf etcd-v3.7.3-linux-amd64.tar.gz
sudo install -d -m 0755 /opt/academy-etcd-3.7.3
sudo install -m 0755 etcd-v3.7.3-linux-amd64/etcd etcd-v3.7.3-linux-amd64/etcdctl /opt/academy-etcd-3.7.3/
/opt/academy-etcd-3.7.3/etcd --version
How do I know it worked?

Official checksum matches; etcd3.7.3 binaries are installed without starting a daemon.

Prepare PKI and network restrictions first

Obtain certificates through your existing private PKI, not a public self-signed bypass. On each node provision ca.crt; a node server.crt/server.key with that node’s IP SAN and serverAuth/clientAuth for etcd peer traffic; a patroni.crt/patroni.key client certificate with an empty subject CN (clientAuth); and api.crt/api.key with that node’s IP SAN and serverAuth/clientAuth. Keep CA signing keys off the nodes. Provision a separate etcd admin certificate with CN root in the administrator’s protected store. Never put private key material in browser settings, shell history or Git.

Allow TCP 5432 only between the three database IPs and 10.77.18.50 for this lab; TCP 8008 only between peers and management; TCP 2379 only for DCS clients/admin; TCP 2380 only between voters. Bind explicitly to private IPs, never world-trust or a public management listener. Retain your firewall owner’s existing SSH/console rules. No firewall mutation panel is supplied because the existing network policy must own it.

04

LET’S BUILD

Create only the new lab root

Run on each VM after checking parent ownership and symlinks. The example reuses postgres for lab processes; production should separate DCS identity and privileges. Copy the PKI files with your protected transfer method into the new tls directory; use 0600 on private keys and config, never copy a CA private key. The following checks show certificate metadata only.

For etcd peers, SANs must also match the actual peer connection source. On a multi-homed VM inspect routing and source selection before starting.

Bash

Disposable database VM; Bash as administrator

set -eu
sudo python3 - <<'PY'
from pathlib import Path
import stat,subprocess
root=Path('/srv/academy-pg18')
if not root.is_absolute() or len(root.parts)<3 or not str(root).startswith('/srv/') or str(root)=='/srv/wiunix':
    raise SystemExit('Choose a fresh dedicated child below /srv')
for parent in reversed(root.parents):
    info=parent.lstat()
    if not stat.S_ISDIR(info.st_mode) or info.st_uid!=0 or info.st_mode & 0o022:
        raise SystemExit('Unsafe parent: '+str(parent))
if root.exists() or root.is_symlink():raise SystemExit('Existing lab root; STOP')
subprocess.run(['install','-d','-o','postgres','-g','postgres','-m','0700',str(root)],check=True)
subprocess.run(['install','-d','-o','postgres','-g','postgres','-m','0700',str(root/'tls'),str(root/'socket')],check=True)
PY
# Transfer the reviewed PKI files into tls using the protected channel now.
sudo python3 - <<'PY'
from pathlib import Path
import os,pwd,stat
base=Path('/srv/academy-pg18/tls')
names=('ca.crt','server.crt','server.key','api.crt','api.key','patroni.crt','patroni.key')
# Validate every exact input before changing any ownership or mode.
for path in (base.parent,base):
    if not stat.S_ISDIR(path.lstat().st_mode):raise SystemExit('Unsafe directory: '+str(path))
for name in names:
    if not stat.S_ISREG((base/name).lstat().st_mode):raise SystemExit('Unsafe PKI input: '+name)
account=pwd.getpwnam('postgres')
for name in names:
    path=base/name
    fd=os.open(path,os.O_RDONLY|os.O_NOFOLLOW)
    try:
        if not stat.S_ISREG(os.fstat(fd).st_mode):raise SystemExit('Changed PKI input: '+name)
        os.chown(path,account.pw_uid,account.pw_gid,follow_symlinks=False)
        os.fchmod(fd,0o600 if name.endswith('.key') else 0o644)
    finally:os.close(fd)
PY
sudo openssl x509 -in '/srv/academy-pg18/tls/server.crt' -noout -subject -ext subjectAltName
sudo openssl verify -CAfile '/srv/academy-pg18/tls/ca.crt' '/srv/academy-pg18/tls/server.crt' '/srv/academy-pg18/tls/api.crt' '/srv/academy-pg18/tls/patroni.crt'
How do I know it worked?

Fresh private root and certificates with correct node SANs, trust chain and key ownership.

05

LET’S BUILD

Save the guarded configuration generator

Use the next file panel as a Python file, not a terminal command. It writes JSON, a valid YAML subset, and refuses to replace existing configs/data. Run it once per VM with node index1,2 or3. Every generated file describes only that VM; all three share one DCS scope. The root must remain under a dedicated /srv child; the browser path type alone cannot enforce safe operational ownership.

Bash

Disposable database VM; Bash as administrator

sudo -u postgres nano '/srv/academy-pg18/make-config.py'
How do I know it worked?

Editor opened; save the following complete file.

06

LET’S BUILD

File: per-node configuration generator

The example deliberately disables the watchdog only for disposable lab VMs. Production automatic promotion must wait for independently tested fencing/watchdog and timing review. Never load a host-reset watchdog on a shared server just to complete this exercise.

Python · /srv/academy-pg18/make-config.py

Disposable database VM; Bash as administrator

import json, pathlib, sys, ipaddress
root = pathlib.Path('/srv/academy-pg18')
ips = ['10.77.18.11', '10.77.18.12', '10.77.18.13']
network = ipaddress.ip_network('10.77.18.0/24')
if len(set(ips)) != 3:raise SystemExit('Duplicate node addresses')
if not all(ipaddress.ip_address(ip) in network for ip in ips):raise SystemExit('Node outside selected subnet')
if not root.is_absolute() or len(root.parts)<3 or root.is_symlink():raise SystemExit('Invalid dedicated root')
if not str(root).startswith('/srv/') or str(root)=='/srv/wiunix':raise SystemExit('Expected dedicated /srv child')
index = int(sys.argv[1]) - 1
if index not in range(3):raise SystemExit('Node index must be 1, 2 or 3')
name, ip = f'pg{index+1}', ips[index]
pg, api, dc, peer = 5432, 8008, 2379, 2380
if len({pg,api,dc,peer})!=4 or min(pg,api,dc,peer)<1024:raise SystemExit('Use distinct non-privileged listener ports')
tls = str(root / 'tls')
for filename in ('ca.crt', 'server.crt', 'server.key', 'api.crt', 'api.key', 'patroni.crt', 'patroni.key'):
    if not (root/'tls'/filename).is_file() or (root/'tls'/filename).is_symlink():raise SystemExit('Missing or symlink PKI file: '+filename)
for filename in ('patroni.yml', 'etcd.yml', 'data', 'etcd-data'):
    if (root/filename).exists() or (root/filename).is_symlink():raise SystemExit('Existing destination: '+filename)
cluster = ','.join(f'pg{i+1}=https://{address}:{peer}' for i,address in enumerate(ips))
etcd = {
 'name': name, 'data-dir': str(root/'etcd-data'),
 'listen-client-urls': f'https://{ip}:{dc}',
 'advertise-client-urls': f'https://{ip}:{dc}',
 'listen-peer-urls': f'https://{ip}:{peer}',
 'initial-advertise-peer-urls': f'https://{ip}:{peer}',
 'initial-cluster': cluster, 'initial-cluster-state': 'new',
 'initial-cluster-token': 'academy_pg18-dcs',
 'client-transport-security': {'cert-file': tls+'/server.crt', 'key-file': tls+'/server.key', 'trusted-ca-file': tls+'/ca.crt', 'client-cert-auth': True},
 'peer-transport-security': {'cert-file': tls+'/server.crt', 'key-file': tls+'/server.key', 'trusted-ca-file': tls+'/ca.crt', 'client-cert-auth': True}}
hba = ['local all postgres peer']
hba += [f'hostssl replication replicator {address}/32 scram-sha-256' for address in ips]
hba += [f'hostssl all postgres {address}/32 scram-sha-256' for address in ips + ['10.77.18.50']]
config = {
 'scope': 'academy_pg18', 'namespace': '/academy/', 'name': name,
 'restapi': {'listen': f'{ip}:{api}', 'connect_address': f'{ip}:{api}',
    'certfile': tls+'/api.crt', 'keyfile': tls+'/api.key', 'cafile': tls+'/ca.crt',
    'verify_client': 'required', 'authentication': {'username': 'operator', 'password': 'REPLACE_API_SECRET'}},
 'ctl': {'cacert': tls+'/ca.crt', 'certfile': tls+'/api.crt', 'keyfile': tls+'/api.key'},
 'etcd3': {'hosts': [f'{address}:{dc}' for address in ips], 'protocol': 'https', 'username': 'patroni', 'password': 'REPLACE_DCS_SECRET',
    'cacert': tls+'/ca.crt', 'cert': tls+'/patroni.crt', 'key': tls+'/patroni.key'},
 'bootstrap': {
    'dcs': {'ttl': 30, 'loop_wait': 10, 'retry_timeout': 10,
       'maximum_lag_on_failover': 1048576, 'check_timeline': True,
       'synchronous_mode': True, 'synchronous_mode_strict': True, 'synchronous_node_count': 1,
       'postgresql': {'use_pg_rewind': True, 'use_slots': True,
          'parameters': {'wal_level': 'replica', 'hot_standby': 'on',
             'max_wal_senders': 10, 'max_replication_slots': 10,
             'wal_keep_size': '256MB', 'max_slot_wal_keep_size': '2GB',
             'password_encryption': 'scram-sha-256', 'synchronous_commit': 'on'}}},
    'initdb': [{'encoding': 'UTF8'}, {'auth-host': 'scram-sha-256'}, {'auth-local': 'peer'}, 'data-checksums'], 'pg_hba': hba},
 'postgresql': {'listen': f'{ip}:{pg}', 'connect_address': f'{ip}:{pg}',
    'data_dir': str(root/'data'), 'bin_dir': '/usr/pgsql-18/bin',
    'pgpass': str(root/'pgpass'),
    'authentication': {
       'superuser': {'username': 'postgres', 'password': 'REPLACE_POSTGRES_SECRET', 'sslmode': 'verify-full', 'sslrootcert': tls+'/ca.crt'},
       'replication': {'username': 'replicator', 'password': 'REPLACE_REPLICATION_SECRET', 'sslmode': 'verify-full', 'sslrootcert': tls+'/ca.crt'}},
    'parameters': {'unix_socket_directories': str(root/'socket'), 'ssl': 'on',
       'ssl_cert_file': tls+'/server.crt', 'ssl_key_file': tls+'/server.key', 'ssl_ca_file': tls+'/ca.crt'},
    'remove_data_directory_on_rewind_failure': False,
    'remove_data_directory_on_diverged_timelines': False},
 'watchdog': {'mode': 'off'},
 'tags': {'nofailover': False, 'noloadbalance': False, 'nosync': False}}
for filename, data in [('etcd.yml', etcd), ('patroni.yml', config)]:
    with (root/filename).open('x', encoding='utf-8', newline='\n') as f:
        json.dump(data, f, indent=2)
        f.write('\n')
    (root/filename).chmod(0o600)
print(name, ip, 'new configuration files created; edit secrets before starting')
How do I know it worked?

File saved intact with public placeholders resolved; secrets are still sentinel values.

Explore all tutorials ↗