Question: How can you set up PostgreSQL replication on CentOS 7?
Answer
Setting up PostgreSQL replication on CentOS 7 involves configuring a primary server (master) and one or more standby servers (slaves). This setup ensures data redundancy and increases the availability of your database system. The following steps outline how to configure streaming replication.
Prerequisites
- Two CentOS 7 servers with PostgreSQL installed.
- Network connectivity between both servers.
- Sudo privileges on both servers.
Step 1: Configure the Primary Server
-
Edit the
postgresql.conf
file:sudo vim /var/lib/pgsql/data/postgresql.conf
-
Ensure that the settings allow connections and enable WAL archiving:
listen_addresses = '*' wal_level = replica max_wal_senders = 3 wal_keep_segments = 64 archive_mode = on archive_command = 'cp %p /path/to/archive/%f'
-
Edit
pg_hba.conf
to allow the standby server to connect:sudo vim /var/lib/pgsql/data/pg_hba.conf
Add the following line, replacing
standby_ip
with the IP address of your standby server:host replication all standby_ip/32 trust
-
Restart PostgreSQL:
sudo systemctl restart postgresql
-
Create a replication user:
sudo -u postgres psql -c "CREATE USER replicator REPLICATION LOGIN ENCRYPTED PASSWORD 'yourpassword';"
Step 2: Configure the Standby Server
-
Stop PostgreSQL:
sudo systemctl stop postgresql
-
Remove the existing data directory:
sudo rm -rf /var/lib/pgsql/data/*
-
Use
pg_basebackup
to clone the primary server:sudo -u postgres pg_basebackup -h primary_ip -D /var/lib/pgsql/data -U replicator -vP -W
Replace
primary_ip
with the IP address of your primary server. -
Create a
recovery.conf
file in the data directory:sudo vim /var/lib/pgsql/data/recovery.conf
Insert the following lines, adjusting parameters as necessary:
standby_mode = 'on' primary_conninfo = 'host=primary_ip port=5432 user=replicator password=yourpassword' trigger_file = '/tmp/postgresql.trigger'
-
Start PostgreSQL:
sudo systemctl start postgresql
Step 3: Verify Replication
Check the replication status by running the following on the primary server:
sudo -u postgres psql -c "select * from pg_stat_replication;"
If configured correctly, you'll see information about the standby server(s) connected to your primary server.
Conclusion
You've now set up basic streaming replication for PostgreSQL on CentOS 7. This setup provides a robust foundation for ensuring data availability and redundancy. For high availability setups, consider exploring additional tools like repmgr or Patroni for automatic failover and management.
Was this content helpful?
Other Common PostgreSQL Questions (and Answers)
- How do you manage Postgres replication lag?
- How can I limit the number of rows updated in a PostgreSQL query?
- How does sharding work in PostgreSQL?
- How do you limit the number of rows deleted in PostgreSQL?
- How do you use the PARTITION OVER clause in PostgreSQL?
- What are PostgreSQL replication slots and how do they work?
- How can you partition an existing table in PostgreSQL?
- How do you partition a table by multiple columns in PostgreSQL?
- How do you check the replication status in PostgreSQL?
- What are the scaling limits of PostgreSQL?
- How do you scale Azure PostgreSQL?
- How do you use the limit clause in PostgreSQL to get the top N rows of a query result?
Free System Design on AWS E-Book
Download this early release of O'Reilly's latest cloud infrastructure e-book: System Design on AWS.
Switch & save up to 80%
Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement. Instantly experience up to a 25X boost in performance and 80% reduction in cost