Attivazione Standby
Istruzioni per creare uno standby di PostgreSQL 15 su Oracle Linux 9
Configurazione del server primario
1. Configurare PostgreSQL Modificare il file postgresql.conf (di solito in /var/lib/pgsql/15/data/postgresql.conf)
cat <<EOL | sudo tee -a /var/lib/pgsql/15/data/postgresql.conf wal_level = replica archive_mode = on archive_command = 'cp %p /var/lib/pgsql/15/archive/%f' max_wal_senders = 3 wal_keep_size = 512MB hot_standby = on EOL
- 4. Modificare il file pg_hba.conf per consentire la replica
cat <<EOL | sudo tee -a /var/lib/pgsql/15/data/pg_hba.conf host replication replica_user standby_ip/32 md5 EOL
- 5. Creare un utente per la replica
sudo -u postgres psql -c "CREATE USER replica_user REPLICATION LOGIN ENCRYPTED PASSWORD 'replica_password';"
- 6. Riavviare PostgreSQL per applicare le modifiche
sudo systemctl restart postgresql-15
- Configurazione del server standby
- 1. Installare PostgreSQL 15 sul server standby
- Install the repository RPM:
sudo dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-9-x86_64/pgdg-redhat-repo-latest.noarch.rpm
- Disable the built-in PostgreSQL module:
sudo dnf -qy module disable postgresql <-- Importante!!
- Install PostgreSQL:
sudo dnf install -y postgresql15-server postgresql15
- 2. Fermare il servizio PostgreSQL sul server standby
- Optionally initialize the database and enable automatic start:
sudo /usr/pgsql-15/bin/postgresql-15-setup initdb sudo systemctl enable postgresql-15 sudo systemctl start postgresql-15
- 3. Eliminare il contenuto della directory dei dati
sudo rm -rf /var/lib/pgsql/15/data/*
- 4. Eseguire il backup dei dati dal server primario
sudo -u postgres pg_basebackup -h primary_ip -D /var/lib/pgsql/15/data -U replica_user -P -R
- 5. Configurare il file recovery.conf (creato automaticamente da pg_basebackup)
echo "primary_conninfo = 'host=primary_ip port=5432 user=replica_user password=replica_password'" | sudo tee -a /var/lib/pgsql/15/data/postgresql.auto.conf
- 6. Avviare PostgreSQL sul server standby
sudo systemctl start postgresql-15
- Verifica dello stato della replica
sudo -u postgres psql -c "SELECT client_addr, state FROM pg_stat_replication;"