# Postgres high availability: replication, failover and point-in-time recovery

> How Postgres high availability works: streaming replication, sync and async standbys, automatic failover, RPO and RTO, point-in-time recovery, and the tools.

- **Published:** 2026-10-27
- **Author:** Abhishek Jaiswal
- **Category:** Engineering
- **Tags:** postgres, high-availability, replication, failover, pitr, managed-postgres
- **Canonical URL:** https://ownkube.io/blog/postgres-high-availability
- **Cover:** https://ownkube.io/blog/postgres-high-availability.png

---
PostgreSQL high availability means a second copy of the database is ready to take over when the first one fails. The usual setup is a primary that accepts writes and one or more standbys that receive its changes through streaming replication. When the primary goes down, a failover manager promotes a standby and moves clients to it.

High availability protects you from a dead server. Point-in-time recovery, from backups and archived WAL, protects you from a bad query, because a standby copies a `DROP TABLE` within milliseconds.

This guide covers how replication and failover work, how to set targets for data loss and downtime, the tools that run HA Postgres, and what a managed database takes off your hands.

**TL;DR**

- Streaming replication sends the primary's write-ahead log (WAL) to standbys, which replay it to stay current.
- Asynchronous replication is the default and can lose the last few transactions on failover. Synchronous replication loses none, at the cost of slower commits.
- Postgres has no built-in automatic failover. Tools such as Patroni, pg_auto_failover and repmgr, or a managed service, decide when to promote a standby.
- High availability and backups solve different problems. You need both: standbys for a failed server, point-in-time recovery for a mistake.
- Two standbys in different zones, a failover manager with a quorum, and tested restores cover most production needs.

## What does high availability mean for Postgres?

A highly available database keeps serving requests when a piece of it fails: a server, a disk or a whole data center zone. For Postgres, that comes down to two numbers.

| Target | Question it answers | Typical HA setup |
|---|---|---|
| RPO (recovery point objective) | How much recent data can you lose? | Seconds with async replication, zero with sync |
| RTO (recovery time objective) | How long can the database be down? | Under a minute or two with automatic failover |

Decide both before you pick a setup. A side project can accept a lost transaction and five minutes of downtime. A payments table cannot.

## How does Postgres replication work?

Every change in Postgres is first written to the write-ahead log. Streaming replication sends that log from the primary to each standby over a network connection, and the standby replays it. The standby is a byte-for-byte copy of the primary and can serve read-only queries as a hot standby.

You create a standby by copying the primary with `pg_basebackup`, then starting it with a `primary_conninfo` that points at the primary. A replication slot on the primary keeps WAL around until the standby has received it, so a standby that falls behind can catch up.

Check replication from the primary:

```sql
SELECT client_addr, state, sync_state,
       pg_wal_lsn_diff(pg_current_wal_lsn(), replay_lsn) AS bytes_behind
FROM pg_stat_replication;
```

### Physical vs logical replication

| | Physical (streaming) | Logical |
|---|---|---|
| What it copies | The whole cluster, block for block | Chosen tables, as row changes |
| Standby can write | No, read-only | Yes |
| Across major versions | No | Yes |
| Used for | High availability and read replicas | Upgrades, migrations, partial copies |

High availability uses physical replication. Logical replication is the tool for moving between major versions or providers with little downtime.

## Synchronous or asynchronous replication?

With asynchronous replication, the primary confirms a commit as soon as it is on its own disk. Standbys usually trail by milliseconds. If the primary dies, the transactions it had not yet sent are lost.

With synchronous replication, the primary waits until a standby confirms it has the change before it tells the client the commit succeeded. Failover loses nothing, and every commit takes at least one network round trip longer.

```sql
-- wait for any one of two standbys before confirming a commit
ALTER SYSTEM SET synchronous_standby_names = 'ANY 1 (standby1, standby2)';
SELECT pg_reload_conf();
```

Use `ANY 1` with two standbys rather than one synchronous standby. With a single synchronous standby, writes stop when that standby goes down. `synchronous_commit` can also be set per transaction, so a payments write can wait for a standby while a log insert does not.

## How does Postgres failover work?

Postgres can promote a standby with `pg_promote()`, but it does not decide on its own that the primary is dead. Something outside has to:

1. Detect the failure. Health checks notice the primary has stopped responding.
2. Agree on it. A quorum of nodes, or a consensus store such as etcd, confirms the primary is gone, so a network blip does not trigger a failover.
3. Fence the old primary. It must stop accepting writes. Two primaries taking writes at once, called split brain, is the failure that loses data for good.
4. Promote the most current standby. It becomes the new primary on a new timeline.
5. Move clients. Connections switch to the new primary through a proxy, a virtual IP, DNS or a Kubernetes service.
6. Rejoin the old primary. Once repaired, it comes back as a standby, often after `pg_rewind`.

Failover tools automate these steps so nobody has to run them by hand at 3 a.m.

## Which tools run high-availability Postgres?

| Tool | How it works | Good fit |
|---|---|---|
| Patroni | An agent per node, with etcd, Consul or ZooKeeper holding the leader lock | Self-managed clusters on VMs, the most widely used option |
| pg_auto_failover | A monitor node watches a primary and its standbys | Simpler setups that want fewer moving parts |
| repmgr | Replication setup and failover scripts, with a witness node | Teams that want manual control with some automation |
| Kubernetes operators (CloudNativePG, Crunchy, Zalando) | The operator runs replication and failover inside a cluster | Teams already running Postgres on Kubernetes |
| Managed services | The provider runs all of it | Teams with no one whose job is the database |

Clients also need to find the primary after a failover. Common options are HAProxy or a pooler such as PgBouncer in front of the cluster, a DNS name that the failover manager updates, or a libpq connection string listing every host with `target_session_attrs=read-write`.

## Is a standby a backup?

No. A standby copies every change from the primary within milliseconds, including the ones you regret. An `UPDATE` without a `WHERE`, a dropped table or a bad migration reaches every standby before you notice.

Point-in-time recovery (PITR) handles that. It combines a base backup with archived WAL, so you can restore the database to any moment in the retention window, such as one second before the bad query.

| Problem | High availability | Point-in-time recovery |
|---|---|---|
| Server or disk fails | Yes, fails over in seconds to minutes | Yes, but restoring takes longer |
| Availability Zone goes down | Yes, if a standby is in another zone | Yes, if backups are stored outside the zone |
| Someone drops a table | No, the drop replicates | Yes, restore to just before it |
| Data corrupted by an app bug | No | Yes, restore to before the bug shipped |

Self-managed, PITR means `archive_command` or a tool such as pgBackRest or WAL-G shipping WAL to object storage, plus scheduled base backups. A backup only counts once you have restored from it, so test restores on a schedule.

## What does a production HA setup look like?

For most apps, this covers the common failures:

- A primary and two standbys, each in a different Availability Zone.
- A failover manager with a quorum, so one node cannot promote itself alone.
- Synchronous replication to `ANY 1` of the standbys if losing a commit is not acceptable, asynchronous otherwise.
- Continuous WAL archiving to object storage, with a 7-day or longer recovery window.
- A connection path that follows the primary, and apps that retry a failed connection.
- Monitoring on replication lag and on the failover manager itself.
- A failover drill and a restore drill, run before you need them.

## How much does high availability cost?

Each standby is a full copy of the database: the same instance size and its own storage. Two standbys roughly triple the database bill. On managed services:

| Service | How HA is billed |
|---|---|
| Amazon RDS Multi-AZ | A standby billed as a full instance, and storage at twice the Single-AZ rate |
| Supabase | Read replicas billed as their own compute; PITR at $100 a month per 7 days of retention |
| Neon | Read replicas billed as computes on shared storage |
| Ownkube | Each standby billed as its own database box plus its own storage |

See [AWS RDS pricing explained](/compare/aws-rds-pricing-explained) for Multi-AZ numbers and [managed Postgres pricing compared](/compare/managed-postgres-pricing) for more providers.

## How Ownkube runs high-availability Postgres

Ownkube runs managed PostgreSQL 17 and 18. On the Team plan, a database can run a primary plus up to two standbys, with streaming replication and automatic failover. Apps connect through the same connection string after a failover.

Backups with 7-day point-in-time restore are part of the $0.25 per GB storage price on Personal and Team. You turn them on per database, and you can restore to any moment in the window yourself.

Each standby bills as its own database box plus its own storage. A DB 4 primary (1 vCPU, 4 GiB, $28 a month) with one standby and 50 GB of data is $81 a month, backups included. Ownkube Compute runs in the Germany and Mumbai regions.

## FAQ

### Does PostgreSQL have built-in high availability?

It has the building blocks: streaming replication, hot standbys, synchronous commit and `pg_promote()`. It does not detect a failed primary or promote a standby by itself. A tool such as Patroni or a managed service does that.

### What is the difference between a standby and a read replica?

Both receive changes through streaming replication. A standby exists to take over when the primary fails. A read replica exists to serve read queries. One server can do both, and some managed services offer only one or the other.

### How long does a Postgres failover take?

With an automatic failover manager, usually between a few seconds and a minute or two, depending on how long it waits to confirm the primary is gone. Apps also need to reconnect, so retry logic in the client matters as much as the failover itself.

### Can I lose data in a Postgres failover?

With asynchronous replication, yes: the transactions the primary had not sent to a standby are lost. With synchronous replication to at least one standby, a confirmed commit survives the failover.

### Do I need high availability for a small app?

Often not. A single database with point-in-time recovery and a tested restore covers most small apps, with a longer outage if the server fails. Add standbys when an hour of downtime would cost more than the second database.

## Where Ownkube fits

If you want a Postgres that fails over by itself without running Patroni and etcd, Ownkube gives you PostgreSQL 17 or 18 with up to two standbys on Team and point-in-time restore in the storage price, next to the app that uses it. The [add a Postgres database guide](/blog/add-managed-postgres-database) walks through setup. [Deploy my first app](https://app.ownkube.io/login).

## Search

This site has a search endpoint: https://context7.com/api/v2/ask?siteKey=ask_c64dfe8ffaa8d3ad19576838&query=<URL-encoded question>. It returns documentation that answers the question, with a source link for each part. No API key is needed. If nothing matches, it says so.
