PostgreSQL Streaming Replication Setup Guide

·Updated Apr 3, 2026··

Level: intermediate · ~12 min read · Intent: informational

Audience: backend developers, database engineers, technical teams

Prerequisites

  • basic familiarity with PostgreSQL

Key takeaways

  • PostgreSQL streaming replication works by having a standby continuously receive and replay WAL records from the primary, which makes it a core building block for read replicas and high availability.
  • A successful streaming replication setup depends on more than one command. It requires correct WAL settings, a dedicated replication role, secure network access, a clean base backup, and ongoing lag monitoring.

FAQ

What is PostgreSQL streaming replication?
PostgreSQL streaming replication is a setup where a standby server continuously receives WAL changes from a primary server and replays them to stay closely synchronized.
Does streaming replication automatically provide failover?
Not by itself. Streaming replication creates the data-copying relationship between primary and standby, but failover also requires promotion logic, routing changes, and operational procedures.
0

PostgreSQL streaming replication is one of the most important building blocks for modern PostgreSQL operations.

It is commonly used for:

  • read replicas
  • standby servers
  • high availability setups
  • disaster recovery preparation
  • and reducing risk when a primary server fails

At a high level, streaming replication lets a standby PostgreSQL server stay closely synchronized with a primary by continuously receiving and replaying WAL records.

That sounds simple, but getting it right in production depends on more than enabling one setting.

A real setup needs:

  • correct WAL configuration
  • a replication user
  • secure network access
  • a clean base backup
  • standby configuration
  • and operational monitoring to make sure the standby stays healthy and close enough to the primary

This guide explains PostgreSQL streaming replication in practical terms and walks through the main setup flow, the settings that matter most, and the mistakes teams should avoid.

The Most Important Streaming Replication Rule

Before going deeper, remember this:

Streaming replication is not just a database copy. It is an ongoing WAL delivery and replay relationship that must be configured, secured, and monitored like part of the production system.

That matters because teams sometimes treat replication as:

  • a one-time setup
  • a backup substitute
  • or a checkbox for high availability

It is none of those by itself.

Streaming replication is extremely useful, but it only stays useful if:

  • the standby can keep receiving WAL
  • the standby can replay it consistently
  • the network path stays healthy
  • the primary retains what the standby still needs
  • and the team monitors lag and failure conditions

That is the right mindset for setup.

1. What PostgreSQL Streaming Replication Is

Streaming replication is the mechanism PostgreSQL uses to send WAL changes from a primary server to one or more standby servers.

WAL means Write-Ahead Log.

Every committed change on the primary is recorded in WAL. A standby receives those WAL changes and replays them so its data stays aligned with the primary.

This means the standby is not re-running application logic. It is applying the primary’s recorded database changes.

That is why a standby can remain very close to the primary without independently processing every business operation from scratch.

In practical terms:

  • the primary accepts writes
  • the standby follows the primary’s WAL stream
  • and the standby can usually serve read-only workloads or sit ready as a failover candidate

2. Why Teams Set Up Streaming Replication

Streaming replication is useful because it solves several common needs.

Read scaling

Standby servers can sometimes serve read-only traffic so the primary is not responsible for every read.

High availability preparation

A standby can be promoted if the primary fails, which is a major part of HA design.

Operational redundancy

Having another near-current copy of the database reduces dependence on a single server.

Disaster recovery readiness

Replication supports recovery posture, though it should not be confused with a full backup strategy.

These are the main reasons teams invest in it.

3. Streaming Replication Does Not Replace Backups

This is one of the most important warnings.

A standby is not the same as a backup.

Why?

Because the standby usually follows the primary closely. If the primary:

  • drops a table
  • deletes critical data
  • corrupts something logically
  • or runs a bad migration

the standby may reproduce those changes too.

That means streaming replication helps with:

  • node failure
  • hardware failure
  • standby promotion
  • read distribution

but it does not replace:

  • point-in-time recovery planning
  • backup retention
  • restore testing
  • archival strategy

A healthy PostgreSQL production design uses both:

  • replication
  • and backups

not one instead of the other.

4. Primary and Standby Roles in the Architecture

A streaming replication setup usually has:

  • one primary
  • one or more standbys

The primary:

  • accepts writes
  • generates WAL
  • sends WAL to standbys

The standby:

  • receives WAL
  • replays WAL
  • stays read-only unless promoted

That means roles are asymmetric.

The primary is the source of truth while it is active. The standby follows the source and depends on access to the primary’s WAL stream or WAL history.

This is why standby health depends on both:

  • its own configuration
  • and the primary’s replication configuration

5. WAL Is the Core of Replication

To understand streaming replication, you need a basic mental model of WAL.

PostgreSQL writes changes to WAL before those changes are fully reflected in the database data files. That log exists for durability and recovery.

Streaming replication uses that same WAL stream as the change feed for standbys.

So the standby stays current by consuming the WAL produced by the primary.

That is why replication setup is closely tied to WAL configuration. If WAL generation, retention, or transport is misconfigured, replication becomes fragile.

6. The Primary Needs Replication-Friendly Settings

A PostgreSQL primary must be configured to support replication.

The exact configuration depends on version and environment, but conceptually the primary needs to:

  • generate enough WAL detail for replication
  • allow enough WAL sender processes
  • keep enough WAL around so standbys do not fall behind irrecoverably
  • and accept replication connections from authorized standbys

The settings usually revolve around:

  • WAL level
  • WAL sender count
  • replication slots or WAL retention strategy
  • and network/authentication rules

This is the part many teams underestimate. Streaming replication starts at the primary.

7. wal_level Must Support Replication

The primary must generate WAL in a mode that supports standby replication.

In practice, that means using a WAL level appropriate for replication, typically:

  • replica

Without a replication-capable WAL level, the standby will not receive the information it needs to stay synchronized.

This is one of the most fundamental setup requirements.

If the primary is not producing the right WAL information, nothing else in the streaming replication setup can compensate for that.

8. max_wal_senders Must Allow Replication Senders

The primary needs enough WAL sender capacity to serve its standbys.

That is where max_wal_senders matters.

This setting controls how many WAL sender processes PostgreSQL can run to support replication connections.

If you expect:

  • one standby
  • two standbys
  • or more complex topologies later

then this setting needs to reflect that.

Teams often leave too little headroom here. A more practical setup usually leaves room for:

  • current standbys
  • reconnects
  • maintenance
  • or future standby growth

Replication capacity should not be sized so tightly that one change breaks the topology.

9. WAL Retention Matters More Than Many Teams Expect

A standby does not just need WAL transport. It needs the required WAL to remain available long enough to catch up.

If the standby falls behind and the primary removes WAL that the standby still needs, replication can break and require reinitialization or another recovery step.

This is why WAL retention planning matters.

Operationally, teams need to think about:

  • temporary network interruptions
  • standby downtime
  • lag spikes
  • maintenance windows
  • and how much WAL can safely be retained

The exact mechanism can vary by setup, but the principle is simple: a standby that is allowed to fall behind still needs access to the history it missed.

10. Use a Dedicated Replication Role

A standby should not connect to the primary using a broad admin account.

A dedicated replication role is the safer and cleaner approach.

This role is typically created specifically for replication connections and granted the appropriate replication capability.

That gives you:

  • clearer security boundaries
  • easier auditing
  • better credential hygiene
  • and less unnecessary privilege

This is part of a good PostgreSQL security posture. Replication should have the permissions it needs, but not much more.

11. Authentication and Access Rules Must Allow Replication

The primary must explicitly allow the standby to connect for replication.

That means network and authentication rules need to define:

  • which standby hosts may connect
  • which replication role may connect
  • how authentication happens
  • and whether TLS or other security controls are required

This is commonly handled through PostgreSQL host-based authentication rules, along with the surrounding network firewall or private network controls.

A clean setup should be:

  • specific
  • minimal
  • secure
  • and easy to reason about during troubleshooting

Replication should not work by accident. It should work because the correct path is deliberately allowed.

12. Take a Clean Base Backup for the Standby

A standby server usually starts from a base backup of the primary.

This is important because the standby needs a consistent starting copy of the database before it can begin replaying WAL.

A common flow is:

  1. prepare the primary for replication
  2. create a replication role and network access
  3. take a base backup from the primary
  4. place that backup into the standby data directory
  5. configure the standby to connect back to the primary
  6. start the standby and let it catch up

The base backup is the foundation of the standby’s data state. If that starting point is inconsistent or incomplete, the rest of the setup will fail or behave unpredictably.

13. pg_basebackup Is the Common Bootstrap Tool

In many PostgreSQL environments, pg_basebackup is the standard tool used to initialize a standby from the primary.

Conceptually, it:

  • copies a consistent base backup from the primary
  • includes the required metadata for replication startup
  • and helps prepare the standby to begin following WAL

This makes it a natural part of the setup workflow.

A typical mental model is:

  • build the standby from a clean primary backup
  • then let streaming replication take over from that point onward

That is much safer than trying to assemble a standby manually from inconsistent file copies.

14. The Standby Must Know How to Reach the Primary

After the base backup is in place, the standby needs connection information for the primary.

This typically includes:

  • primary host
  • port
  • replication role
  • authentication information
  • and sometimes connection options that matter for retry or security behavior

Without correct connection details, the standby has data but no live replication path.

That means streaming replication setup is always two-sided:

  • the primary must allow the standby
  • the standby must know how to reach and authenticate to the primary

Both sides matter equally.

15. A Standby Is Read-Only Until Promotion

Once configured and running normally, a standby usually operates in read-only recovery mode.

That means:

  • it replays WAL
  • it can often serve read-only queries
  • but it does not accept ordinary writes as the active primary would

This distinction is important operationally.

A standby is meant to follow, not diverge. If you need it to become writable, that is usually a promotion event.

That promotion is what turns it from:

  • follower into
  • active primary

This is why replication setup and failover planning are related, but not identical topics.

16. Monitor Replication Lag From the Start

A standby that exists but falls behind too much is not giving you the resilience you think it is.

That is why replication setup should include monitoring immediately.

Important things to watch include:

  • whether the standby is connected
  • how far behind it is
  • whether WAL is being received
  • whether WAL is being replayed
  • and whether lag is growing over time

Lag is not just a nice-to-know metric. It affects:

  • read freshness
  • failover readiness
  • and overall trust in the standby

A replication setup without lag monitoring is incomplete.

17. Network Stability Matters a Lot

Streaming replication depends on a live path between primary and standby.

So networking affects:

  • connection stability
  • lag
  • reconnect behavior
  • and how quickly standbys recover from transient issues

This means teams should think about:

  • network latency
  • packet loss
  • firewall behavior
  • private routing
  • TLS overhead where relevant
  • and whether standby placement matches the operational goals

A standby in another zone or region may still be perfectly valid, but the network characteristics become part of the replication design.

18. Secure Replication Traffic Like Real Production Traffic

Replication traffic carries sensitive database change information.

That means it should be protected appropriately.

Good posture usually includes:

  • private network paths where possible
  • limited host access
  • a dedicated replication role
  • careful credential storage
  • and transport security where required by the environment

Replication should be treated like production data movement, because that is exactly what it is.

A weakly secured standby path can undermine an otherwise careful PostgreSQL deployment.

19. Streaming Replication Helps HA, But It Is Not Full HA by Itself

A standby that follows the primary is an important piece of high availability, but it is not the entire HA system.

High availability also needs answers for:

  • how failure is detected
  • who decides promotion
  • how the application discovers the new primary
  • what happens to old primary state
  • how split-brain risk is controlled
  • and how recovery happens after failover

So it is important not to confuse:

  • streaming replication setup with
  • complete failover automation and HA architecture

Replication is a foundation, not the whole building.

20. Test Replica Readiness, Not Just Connectivity

A standby can be:

  • connected
  • receiving WAL
  • and still not be operationally ready for the role you expect

So teams should test:

  • can it replay correctly?
  • can it serve the read-only queries expected of it?
  • how much lag does it show under pressure?
  • can it be promoted cleanly when needed?
  • do routing and app behaviors make sense if it becomes primary?

This matters because a standby that only “exists” is not enough. It needs to behave correctly under the real scenarios you care about.

21. Common Setup Flow in Practical Terms

A practical PostgreSQL streaming replication setup usually looks like this:

Step 1

Configure the primary for replication-capable WAL and enough replication capacity.

Step 2

Create a dedicated replication role.

Step 3

Allow replication connections from the standby host through PostgreSQL auth rules and network controls.

Step 4

Take a clean base backup from the primary.

Step 5

Initialize the standby with that base backup.

Step 6

Configure the standby with the primary connection details.

Step 7

Start the standby and confirm it begins receiving and replaying WAL.

Step 8

Monitor lag, connection health, and operational readiness.

That is the big picture.

22. Common PostgreSQL Streaming Replication Mistakes

Treating the standby like a backup replacement

Replication and backups solve different problems.

Using an overpowered account for replication

A dedicated replication role is cleaner and safer.

Forgetting WAL retention strategy

A lagging standby still needs access to the WAL it missed.

Not monitoring lag

A stale standby may be far less useful than expected.

Assuming replication equals automatic failover

Promotion and failover behavior need separate planning.

Ignoring security on replication traffic

Standby connections carry real production data.

FAQ

What is PostgreSQL streaming replication?

PostgreSQL streaming replication is a setup where a standby server continuously receives WAL changes from a primary server and replays them to stay closely synchronized.

Does streaming replication automatically provide failover?

Not by itself. Streaming replication creates the data-copying relationship between primary and standby, but failover also requires promotion logic, routing changes, and operational procedures.

Conclusion

PostgreSQL streaming replication is one of the most useful core features for building:

  • read replicas
  • standby servers
  • high availability foundations
  • and stronger operational resilience

But a good setup is not just about enabling replication settings.

It also depends on:

  • correct WAL configuration
  • secure authentication
  • a dedicated replication role
  • a clean base backup
  • standby connection setup
  • and ongoing lag monitoring

That is why the best way to think about streaming replication is simple:

  • the primary must produce and retain the right WAL
  • the standby must start from a clean copy
  • the standby must keep receiving and replaying changes
  • and the team must watch the relationship like part of production

When those pieces are in place, PostgreSQL streaming replication becomes a strong and dependable foundation for larger database architecture.

PostgreSQL cluster

Explore the connected PostgreSQL guides around tuning, indexing, operations, schema design, scaling, and app integrations.

Pillar guide

PostgreSQL Performance Tuning: Complete Developer Guide

A practical PostgreSQL performance tuning guide for developers covering indexing, query plans, caching, connection pooling, vacuum, schema design, and troubleshooting with real examples.

View all PostgreSQL guides →

Related posts