In the world of databases, ensuring data availability is a key priority for businesses. Setting up replication in PostgreSQL is an excellent strategy to achieve high availability. This process involves copying data from one PostgreSQL server (the primary) to another (the standby), ensuring that the standby server is always ready to take over if the primary fails. Here’s a step-by-step guide on how to set up replication in PostgreSQL for high availability.
First, you’ll need to adjust the configuration settings on your primary server. Open the postgresql.conf
file and ensure the following settings are enabled:
1 2 3 4 |
wal_level = replica archive_mode = on archive_command = 'cp %p /path_to_archive/%f' max_wal_senders = 3 |
Additionally, configure the pg_hba.conf
file to allow connections from the replication user:
1
|
host replication replicator 192.168.1.0/24 md5 |
On the standby server, stop the PostgreSQL service and clear any existing data:
1 2 |
systemctl stop postgresql rm -rf /var/lib/postgresql/data/* |
Then, use the pg_basebackup
utility to copy data from the primary server:
1
|
pg_basebackup -h primary_host_address -D /var/lib/postgresql/data -P -U replicator |
After the pg_basebackup
process, create a recovery.conf
file in the standby server’s data directory with the following lines:
1 2 3 |
standby_mode = 'on' primary_conninfo = 'host=primary_host_address port=5432 user=replicator password=your_password' trigger_file = '/path_to/trigger.file' |
Finally, you can start the standby server with:
1
|
systemctl start postgresql |
Your replication setup is now complete, ensuring your PostgreSQL database remains highly available.
For more insights on optimizing PostgreSQL, check out our specific guides:
By following these steps, you’ll bolster your PostgreSQL setup for increased reliability and performance, ensuring that your system remains responsive and accessible under various conditions.