Skip to content
← cd ~/writing

# production notes / article

I Tried Running PostgreSQL on Kubernetes. The Database Was the Easy Part.

What Crunchy PGO, CloudNativePG, Patroni, PgBouncer, and a few difficult debugging sessions taught me about stateful workloads and reliability on Kubernetes.

24 October 2026 · 11 min read

Two PostgreSQL operators, a lot of YAML, and some lessons about stateful workloads that no quickstart guide prepared me for.

There are few things more humbling in infrastructure engineering than watching a perfectly healthy Kubernetes cluster host a database that your application can’t use.

I’ve worked with Kubernetes long enough to develop a reasonable level of confidence in it. Deployments, Services, ingress, autoscaling, persistent volumes — eventually, the moving parts start to feel familiar.

So when I started working on PostgreSQL on Kubernetes, the plan sounded almost suspiciously simple:

Install an operator. Define a PostgreSQL cluster. Add replicas. Configure storage. Enable backups. Go home.

Well.

The database started. The learning curve did not stop there.

I spent a considerable amount of time working with Crunchy Postgres Operator (PGO) and evaluating CloudNativePG. Along the way came Patroni, PgBouncer, replication, WAL archiving, Kubernetes storage, credentials, and upgrade constraints.

The biggest lesson was this: getting PostgreSQL running on Kubernetes is not the same thing as making it reliable.

This isn’t a five-command installation guide. It’s what I wish I’d understood before the first green Pod convinced me I was finished.

1. The requirement sounded reasonable

The goal was a PostgreSQL platform with a primary, replicas, automated failover, persistent storage, connection pooling, and a recoverable backup strategy. The broader infrastructure was already running on Kubernetes, so using the existing orchestration platform had an obvious appeal.

In one configuration I worked with, the topology included one primary, one synchronous replica, and two asynchronous replicas.

On paper, that sounds like high availability. In practice, it’s a collection of components whose failure behavior you have to understand.

A PostgreSQL replica is not interchangeable with a stateless web-server Pod. Data has to be replicated, commits have durability guarantees, and a new primary has to be selected without introducing an unsafe split-brain scenario. Meanwhile, the application still needs to find the right endpoint and recover from interrupted connections.

Kubernetes solves some of these problems. The operator solves others. PostgreSQL has opinions of its own.

And none of them are particularly interested in the fact that your deployment window ends in twenty minutes.

2. Two operators, two approaches to the same problem

Crunchy PGO was the primary operator I worked with. Its stack brings together PostgreSQL with Patroni for high-availability orchestration, pgBackRest for backup and recovery, and PgBouncer for connection pooling.

Instead of building those pieces independently, you declare a desired PostgreSQL cluster through Kubernetes custom resources and let the operator reconcile it.

That is genuinely useful. But abstraction doesn’t remove the underlying mechanisms. It means troubleshooting often spans PostgreSQL, Patroni, operator configuration, Secrets, Services, Pods, and storage.

I also evaluated CloudNativePG, which takes a different architectural approach: it uses its own instance manager rather than Patroni to manage PostgreSQL instances and high-availability behavior.

I wouldn’t claim one is categorically better based on a feature checklist. What mattered to me was the operational contract: Who owns failover? How does recovery work? How are upgrades handled? Which resources can I safely customize?

The right operator isn’t necessarily the one whose quickstart deploys fastest. It’s the one whose failure and maintenance model you can actually operate.

3. The operator is another administrator

Kubernetes reconciliation is brilliant until you forget that the operator also has a say in your configuration.

An operator continuously compares what exists with what its custom resource says should exist. Manual changes to generated resources may be overwritten. A perfectly reasonable-looking configuration may be rejected if the installed CRD doesn’t support it.

I hit this while working on read-only PgBouncer connectivity. A configuration field I expected to use wasn’t recognized by the installed Crunchy PGO CRD schema. Separately managing pooling components introduced another collection of startup and compatibility issues.

The important fix wasn’t to fight Kubernetes harder. It was to check the installed operator version, inspect the schema, and decide whether a customization was actually supported or whether I was stepping outside the operator’s management boundary.

If the operator owns a resource, treat the custom resource as the source of truth.

That lesson applies well beyond databases, but PostgreSQL makes the consequences particularly visible.

4. Four PostgreSQL Pods are not a recovery plan

It’s tempting to check:

kubectl get pods -n <your-postgres-namespace>

Everything says Running. Maybe every readiness probe is green.

Great. What does that tell us about replication correctness, write availability, or potential data loss during failover?

Not nearly enough.

On the primary, I would rather start by inspecting replication state:

SELECT
    application_name,
    client_addr,
    state,
    sync_state,
    sent_lsn,
    write_lsn,
    flush_lsn,
    replay_lsn
FROM pg_stat_replication;

This shows the connected standbys visible to the primary and the progress of WAL transmission and replay. It is a diagnostic starting point, not proof of full high availability.

Synchronous and asynchronous replicas are different tradeoffs. Synchronous replication can make commits wait for the required acknowledgement from an eligible standby, improving durability guarantees at a potential cost to write availability. Asynchronous replication avoids that acknowledgement requirement for commits but can leave recently committed transactions vulnerable if the primary disappears before WAL reaches the standby.

Then there’s the part the application actually experiences: interrupted transactions, broken connections, failover time, and reconnect behavior.

The questions I care about now are:

  • Can the cluster elect or promote a safe primary after the specified failure?
  • What happens to acknowledged commits?
  • How long are writes unavailable?
  • Will applications automatically reconnect, and are retries safe?
  • Are replicas separated across the failure domains that matter?

High availability is a tested behavior, not a replica count.

5. Persistent volumes deserve more respect than they get

A stateless application Pod disappears and a replacement starts elsewhere. The data generally lives outside that Pod.

PostgreSQL is different. The container is replaceable. Its durable state is not.

On AKS, storage decisions forced me to think beyond simply attaching a PVC. What happens if the primary’s node dies? Can its disk be attached elsewhere, and how long does that take? What if the volume itself becomes unavailable? Are the replicas and their disks distributed across meaningful failure domains?

A Pod restart, a node failure, a disk failure, and an availability-zone outage are different scenarios. Not every recovery action works for all of them.

A scheduler can decide where a Pod should run. It cannot make a failed storage device healthy or guarantee that a particular storage class satisfies the durability and recovery objectives of your database.

For a stateful service, storage topology is part of availability topology.

That realization changed how I reviewed Kubernetes architecture. A diagram with three database Pods tells only half the story. I also want to see the underlying volumes, their placement, attachment constraints, and backup destination.

6. A completed backup job is not a restored database

Crunchy PGO’s integration with pgBackRest was an important part of the architecture. In one AKS configuration, I designed weekly full backups, daily differential backups, incremental backups every four hours, and continuous WAL archiving to object storage.

It’s an attractive schedule to put in a design document.

But backup frequency isn’t the same thing as recovery readiness.

A green backup job doesn’t answer:

  • Can I restore a usable database from the stored backup and WAL?
  • Can I recover to a chosen point in time?
  • What happens if the primary and its volume are both lost?
  • Can I recover if the Kubernetes cluster itself is unavailable?
  • How long does it take to make the application usable again?

That brings us to two terms that are often mentioned and less often measured:

RPO — the maximum acceptable data-loss window.

RTO — the maximum acceptable time to restore service.

An incremental backup every four hours does not automatically imply a four-hour RPO. With continuous WAL archiving, the recoverable point depends on what WAL has safely reached durable backup storage and remains available.

Likewise, replica counts don’t establish RTO. Detection, promotion, storage attachment, application reconnection, and validation all consume time.

A backup is a prerequisite. A tested restore is evidence.

7. PgBouncer adds a new failure boundary

Connection pooling is useful, especially when many application replicas can create more PostgreSQL sessions than the database should handle directly.

But placing PgBouncer between your services and PostgreSQL also means your application now depends on a separate connectivity layer.

I ran into configuration and startup problems while exploring read-write and read-only pooling around operator-managed PostgreSQL. Some options were version-dependent; some manually managed components brought their own maintenance burden.

Even after fixing the deployment configuration, the architectural question remains: what happens to the client after a failover?

A new primary may be promoted successfully while old client connections are broken. Connection pools may need to establish new server connections. In-flight transactions may fail. Application retries must account for whether an operation can safely be repeated.

Database failover and application recovery are two different events.

An operator can’t make a non-idempotent application transaction magically safe to retry.

If I had to reduce this lesson to one sentence: test a real application request through your pooler during failover, not just whether PostgreSQL reports that a primary exists.

8. Major-version upgrades are not image updates

One upgrade-path investigation involved a PostgreSQL 16 to PostgreSQL 18 transition with Crunchy PGO. Pre-upgrade validation exposed a compatibility problem related to data checksums between the existing data directory and the proposed target configuration.

It wasn’t a matter of changing an image tag and watching Kubernetes roll forward.

pg_upgrade has compatibility rules. PostgreSQL data directories carry physical properties that must be respected. Extensions, operator support, backup and fallback plans, and application compatibility all matter.

That incident reinforced an important distinction:

Kubernetes can reconcile desired state. It cannot guarantee that every database state transition is valid.

Before a major PostgreSQL upgrade, I would now insist on checking the operator-supported procedure, validating the target environment, testing an upgrade against representative data, and planning the fallback path separately.

9. Everything was healthy. Authentication still failed.

I also encountered a mismatch between credentials expected by the application and those accepted by an operator-managed PostgreSQL deployment.

The Secret existed. The Service existed. The PostgreSQL Pod was running.

The connection failed anyway.

A Kubernetes Secret containing a password doesn’t prove that PostgreSQL currently accepts it. The actual database credential state and whatever configuration the application received have to match.

The useful debugging path was to reconcile those layers rather than treating Pod readiness as proof that authentication worked.

This is a small example of a much bigger principle:

Infrastructure health is not application correctness.

A green dashboard is comforting. A successful transaction from the application is evidence.

10. What I would do differently now

If I were starting the architecture again, I’d change the order of operations.

First: define the failure model. Do we need to survive a Pod crash, a node failure, a zone outage, accidental deletion, storage corruption, or complete cluster loss? Each one needs an appropriate response.

Second: set recovery objectives. Decide on meaningful RPO and RTO targets before arguing about replica counts.

Third: choose the operator based on operational behavior. Understand the controller’s ownership boundaries, HA mechanism, backup workflow, upgrade support, and debugging interfaces.

Fourth: design placement and storage together. Replicas should not share a hidden dependency that defeats the availability goal.

Fifth: test recovery before declaring victory. Failover, restore, pooling, authentication, and application reconnection should all have explicit tests.

Sixth: monitor PostgreSQL, not only Kubernetes. Replication progress, WAL retention, backup health, transaction state, pooler saturation, storage latency, and application errors belong in the same reliability picture.

I would also be much more cautious about accepting a successful deployment as the conclusion of the work.

For stateful infrastructure, deployment is the beginning of the operating responsibility.

11. Would I run PostgreSQL on Kubernetes again?

Yes — when the organization has a reason to own that complexity.

If you’re already operating Kubernetes well, have the database expertise, and need the control or integration that an operator provides, it can be a sensible architecture.

If what you really need is a reliable PostgreSQL database without owning its entire infrastructure lifecycle, a managed service deserves serious consideration.

Managed databases don’t absolve you of query performance, application connection handling, or data architecture. But they can transfer a meaningful amount of infrastructure and recovery work to the provider.

Self-hosting PostgreSQL on Kubernetes gives you control. It also gives you responsibility for testing the assumptions behind that control.

The question isn’t, Can Kubernetes run PostgreSQL?

It can.

The question is whether the resulting platform meets your requirements more reliably, more operably, and at an acceptable total cost than the alternatives.

Closing thoughts

This work taught me more about systems engineering than any successful kubectl apply could have.

Kubernetes manages workloads. The operator manages a desired database topology. PostgreSQL protects transactions and data. Storage has physical failure boundaries. Connection pools affect application behavior. Backups determine which recovery options exist.

All of those systems must cooperate.

A healthy Pod isn’t necessarily a healthy database. A healthy database isn’t necessarily a usable application. A backup isn’t necessarily a recovery plan.

Reliability isn’t something you get by installing an operator. It’s something you design, test, and continuously validate.

Would I go through the PostgreSQL-on-Kubernetes experience again?

Probably.

Would I celebrate the first green Pod as proof that I was finished?

Absolutely not.

I write about PostgreSQL, Kubernetes, reliability, and the occasional infrastructure decision that seemed perfectly reasonable at the time. More at advaitkeskar.tech.

← Back to all notes · See the systems behind these notes ↗