Configuration d’un serveur secondaire
Parcourez les étapes pratiques d’initialisation et de configuration d’un serveur secondaire PostgreSQL pour synchroniser continuellement les données.
Configuration d’un serveur secondaire est une leçon Advanced PostgreSQL: Indexing, Partitioning, Replication gratuite sur CoddyKit. Ceci est la leçon 3 sur 4. Tu peux lire la leçon complète ci-dessous gratuitement — puis la pratiquer en direct dans le navigateur avec un éditeur de code intégré et un tuteur IA 24/7. Elle fait partie du parcours d'apprentissage Advanced PostgreSQL: Indexing, Partitioning, Replication, et ta progression se synchronise sur le web et l'application CoddyKit. Le cours Advanced PostgreSQL: Indexing, Partitioning, Replication comprend 4 leçons au total.
Certaines parties de cette leçon n'ont pas encore été traduites et s'affichent en anglais.
Welcome to Standby Setup
In this lesson, we'll walk through the practical steps to set up a PostgreSQL standby server. This server will continuously synchronize data from your primary server, acting as a read-replica and a crucial part of your disaster recovery strategy.
We'll cover the necessary configurations on both the primary and the standby.
Primary Prep: Replication User
Before initializing our standby, we need a dedicated user on the primary server with replication privileges. This user will be used by the standby to connect and stream WAL (Write-Ahead Log) data.
Run this SQL command on your primary PostgreSQL server:
CREATE USER repluser REPLICATION LOGIN CONNECTION LIMIT 1 ENCRYPTED PASSWORD 'StrongPassword123!';Primary Prep: Access Control
Next, we need to tell the primary server to accept connections from our standby server for replication. Edit the pg_hba.conf file on your primary server.
Add an entry like this, replacing 192.168.1.100 with the actual IP address of your standby server:
host replication repluser 192.168.1.100/32 md5Primary Prep: Apply Changes
After modifying pg_hba.conf, PostgreSQL needs to reload its configuration. You can do this without a full restart in most cases.
Execute this command on your primary server:
sudo systemctl reload postgresqlStandby: Initial Data Copy
Now, let's move to the standby server. The first step is to get a base copy of the primary's data directory. We use pg_basebackup for this. This command connects to the primary, copies all its data, and creates necessary recovery configuration files.
Run this command on your standby server, replacing placeholders:
pg_basebackup -h primary_ip -U repluser -D /var/lib/postgresql/16/main -P -RDecoding pg_basebackup Options
Let's break down the pg_basebackup options:
-h primary_ip: Hostname or IP of the primary server.-U repluser: The replication user we created.-D /path/to/data: The data directory on the standby.-P: Show progress during the backup.-R: Crucial! Creates astandby.signalfile and appendsprimary_conninfotopostgresql.conf, configuring the standby for recovery.
Standby: Essential Configuration
The -R option handles most of the recovery setup. However, ensure hot_standby is enabled in the standby's postgresql.conf if you want to use it for read queries.
Open postgresql.conf on the standby and ensure this line is present and uncommented:
hot_standby = onStandby: Connection String
The -R option should have automatically added a primary_conninfo entry to your standby's postgresql.conf. This tells the standby how to connect to the primary.
Verify it looks similar to this, with your actual primary IP and replication user password:
primary_conninfo = 'host=primary_ip port=5432 user=repluser password=StrongPassword123!'Starting the Standby
With the data copied and configuration in place, you can now start the PostgreSQL service on your standby server. It will automatically begin connecting to the primary and streaming WAL records.
Execute this command on your standby server:
sudo systemctl start postgresqlVerify Replication Status
To confirm that replication is working, connect to your primary server and query the pg_stat_replication view. You should see an entry for your standby server.
A state of 'streaming' indicates success!
SELECT client_addr, state, sync_state FROM pg_stat_replication;Check Your Setup
You've just set up a PostgreSQL standby! Let's test your understanding of a key step.
Standby Setup Summary
Great job! You've successfully configured a PostgreSQL standby server for streaming replication. This involves:
- Creating a replication user on the primary.
- Configuring
pg_hba.confon the primary. - Using
pg_basebackup -Ron the standby for initial data copy and automatic configuration. - Ensuring
hot_standby = onon the standby. - Starting the standby and verifying its status.
Your standby is now ready to receive continuous updates from the primary!
Questions Fréquemment Posées
La leçon « Configuration d’un serveur secondaire » est-elle gratuite ?
Oui — le texte complet de « Configuration d’un serveur secondaire » est gratuit à lire ici sur le web. Pour la pratiquer de manière interactive (un éditeur de code intégré et un tuteur IA 24/7) et déverrouiller le reste du cours Advanced PostgreSQL: Indexing, Partitioning, Replication, passe à CoddyKit PRO. Le cours Advanced PostgreSQL: Indexing, Partitioning, Replication comprend 4 leçons au total.
Qu'est-ce que j'apprendrai dans « Configuration d’un serveur secondaire » ?
Parcourez les étapes pratiques d’initialisation et de configuration d’un serveur secondaire PostgreSQL pour synchroniser continuellement les données. Tu pratiques Advanced PostgreSQL: Indexing, Partitioning, Replication avec du code pratique que tu exécutes directement dans le navigateur, et un tuteur IA 24/7 répond à tes questions au fur et à mesure que tu avances dans la leçon.
Dois-je avoir de l'expérience pour commencer Advanced PostgreSQL: Indexing, Partitioning, Replication ?
Aucune expérience préalable n'est requise. Advanced PostgreSQL: Indexing, Partitioning, Replication sur CoddyKit est structuré pour les débutants jusqu'aux apprenants avancés, donc tu peux commencer ici ou depuis le début et avancer à ton rythme. Ceci est la leçon 3 sur 4.
Combien de temps prend la leçon « Configuration d’un serveur secondaire » ?
La plupart des leçons CoddyKit prennent environ 5–10 minutes. Chacune est courte et interactive, tu progresses régulièrement et tu repiques exactement où tu t'es arrêté sur le web et l'app.
Peux-tu écrire et exécuter du code dans cette leçon Advanced PostgreSQL: Indexing, Partitioning, Replication ?
Oui. Chaque leçon Advanced PostgreSQL: Indexing, Partitioning, Replication inclut un éditeur de code intégré, tu écris et exécutes du vrai code directement dans ton navigateur et tu reçois des retours IA instantanés — aucune configuration locale requise.
Toutes les leçons de ce cours
- Comprendre les concepts de la réplication
- Réplication physique (en continu)
- Configuration d’un serveur secondaire
- Réplication synchrone ou asynchrone