How to set up replication in PostgreSQL
Updated 7/20/2026
Replication gives you a second server that receives all changes from the main database in real time. It is used for redundancy (if the primary fails) and to distribute reads.
How it works (streaming replication)
The primary server sends the transaction log (WAL) to the standby server, which applies the same changes.
1. On the primary
In postgresql.conf:
wal_level = replica
max_wal_senders = 3
Create a dedicated replication role and allow the connection in pg_hba.conf:
CREATE ROLE replicator WITH REPLICATION LOGIN PASSWORD 'password';
# pg_hba.conf
host replication replicator STANDBY_IP/32 scram-sha-256
2. On the standby
Stop PostgreSQL, then copy the database from the primary:
pg_basebackup -h PRIMARY_IP -U replicator -D /var/lib/postgresql/data -P -R
The -R option configures the replication connection automatically. Start PostgreSQL on the standby.
3. Verify
On the primary:
SELECT * FROM pg_stat_replication;
For two servers, run the primary and standby on two separate VPS instances, ideally in different locations. Do not forget backups though: replication does not replace a backup.
Put this guide into practice on your own VPS
EU cloud servers, billed hourly, ready in minutes, with one-click installs for dozens of apps.
See pricingRelated tutorials
How to secure MongoDB with authentication
By default, MongoDB runs without a password. Here is how to enable authentication and create an admin user.
Updated 7/20/2026DatabasesHow to migrate a database between servers
Move a MySQL or PostgreSQL database to a new server with minimal downtime, using dump and restore.
Updated 7/20/2026DatabasesHow to connect an app to a remote database
Allow access to a database from another server, securely: bind address, dedicated user and firewall.
Updated 7/20/2026