Questions
How do you set up replication in PostgreSQL?
The Scenario
You are a database administrator at a social media company. You are responsible for a PostgreSQL database that is critical to the business.
You need to set up replication to a secondary database to provide high availability and disaster recovery.
The Challenge
Explain how you would set up replication in PostgreSQL. What are the different types of replication, and what are the trade-offs between them?
A junior engineer might not be aware of replication. They might try to solve this problem by just taking periodic backups of the database, which would not provide high availability.
A senior engineer would know that replication is a critical part of database administration. They would be able to explain the different types of replication and would have a clear plan for how to set up replication for a production database.
Step 1: Understand the Different Types of Replication
| Replication Type | Description |
|---|---|
| Streaming Replication | The primary server sends a stream of WAL (Write-Ahead Logging) records to the secondary server. The secondary server then replays the WAL records to keep itself up-to-date. |
| Logical Replication | The primary server sends a stream of logical changes to the secondary server. The secondary server then applies the logical changes to its own data. |
For our use case, we will use streaming replication. It is the most common type of replication, and it is well-suited for providing high availability and disaster recovery.
Step 2: Configure the Primary Server
The first step is to configure the primary server. We need to edit the postgresql.conf file and set the following parameters:
wal_level = replica
max_wal_senders = 10
wal_keep_segments = 64We also need to edit the pg_hba.conf file to allow the secondary server to connect to the primary server.
host replication all <secondary_server_ip>/32 md5Step 3: Create a Base Backup
The next step is to create a base backup of the primary server. We can use the pg_basebackup utility to do this.
pg_basebackup -h <primary_server_ip> -D /var/lib/postgresql/data -U replication -P -vStep 4: Configure the Secondary Server
The final step is to configure the secondary server. We need to create a recovery.conf file in the data directory with the following content:
standby_mode = 'on'
primary_conninfo = 'host=<primary_server_ip> port=5432 user=replication password=...'Step 5: Start the Secondary Server
Now we can start the secondary server. It will connect to the primary server and start replaying the WAL records.
Practice Question
You want to replicate only a subset of the tables in your database. Which type of replication would you use?