PostgreSQL

Deploy self-hosted PostgreSQL with Patroni or CloudNativePG, pgBackRest point-in-time recovery, PgBouncer pooling, TLS and SCRAM, and pg_upgrade plans, including PostgreSQL 14 end of life.

On this page

Self-hosted PostgreSQL usually sits under other systems

A production self-hosted PostgreSQL deployment starts with the recovery targets of the systems that depend on it. You then pick a topology, a backup design and an upgrade path to match. The sections below cover each choice, from Patroni and CloudNativePG to pgBackRest, pg_hba.conf and pg_upgrade.

PostgreSQL is an object-relational database released under the PostgreSQL License, which the project calls a liberal open source license similar to BSD or MIT. Anyone can use, modify and distribute it free of charge for any purpose, as the PostgreSQL documentation states, and the core server has no paid edition. The download page lists packages for Debian, Red Hat, Rocky, AlmaLinux, SUSE, Ubuntu, macOS, Windows, OpenBSD, FreeBSD, NetBSD and Solaris, plus source code.

In a self-hosted stack, PostgreSQL often holds the state of other systems. When a cluster stops, the applications on top of it stop too, so the database often carries the strictest recovery requirements in the stack. List what depends on each cluster: the owner, tolerable data loss and downtime, peak connections and required extensions. Those answers decide whether applications share a cluster, whether replication must be synchronous, and how far back point-in-time recovery has to reach.

Choose a PostgreSQL high availability topology for the workload

The right shape depends on recovery targets, the platform you already run and who will operate it.

Single server with backups

One primary with continuous WAL archiving to separate storage. This suits workloads that can accept a restore window, because recovery rests entirely on tested backups. Archiving needs wal_level of replica or higher, archive_mode on and an archive command that returns a zero exit status only on success. Without a standby, a host failure means a restore, so record the measured restore time.

Patroni on virtual machines

Patroni is a template for high availability PostgreSQL built in Python. It manages streaming replicas and automatic failover, with leader election in ZooKeeper, etcd, Consul or Kubernetes. It supports asynchronous, synchronous and quorum commit modes, a REST API, patronictl, watchdog support and standby clusters. Clients reach the primary through a proxy, virtual IP or DNS entry.

CloudNativePG on Kubernetes

CloudNativePG is an Apache-2.0 Kubernetes operator and a CNCF Incubating project. It works without external HA tools such as Patroni, repmgr or Stolon. The operator elects a new primary when the primary fails, and rolling updates change replica pods first before a controlled switchover. It targets vanilla Kubernetes and community PostgreSQL, and it does not support forks.

Air-gapped networks

Packages, images and extensions come from an internal mirror. Backup repositories, monitoring and certificate authorities also live inside the boundary, so plan them with the cluster. pgBackRest can write to S3, Azure, GCS or SFTP repositories, so choose one that exists inside the boundary and confirm every tool in the design can reach its storage without internet access.

What drives PostgreSQL sizing

Measure these with a representative workload before choosing instance sizes, storage and the number of standbys.

DriverWhy it mattersWhat to measure
Working setQueries are fastest when frequently read data fits in shared buffers and the operating system cache.Measure the cache hit ratio and read latency for the busiest tables.
Write volumeWrites generate WAL, which drives replica lag, archive storage and backup size.Measure WAL generated per hour at peak and during batch jobs.
ConnectionsPostgreSQL gives every client connection its own backend process, so idle connections still use memory.Measure peak connections per application and how many are active at once.
Storage latencyCommit time depends on how quickly WAL reaches durable storage.Measure commit latency on the chosen volumes under load.
Recovery windowPoint-in-time recovery needs every WAL file since the oldest retained base backup.Measure archive size for the agreed retention and the time a full restore takes.
Standbys and sync modeEach standby and backup stream needs a WAL sender. The max_wal_senders setting defaults to 10 and changes only at server start. Synchronous replication adds commit latency.Count standbys and backup streams, and compare commit latency with and without a synchronous standby.

Plan PostgreSQL streaming replication and failover before it happens

A standby that cannot be connected to until promoted is a warm standby. One that accepts read-only queries is a hot standby. The PostgreSQL high availability documentation says asynchronous replication can lose transactions on failover, while synchronous replication guarantees a failover will not lose data. It also warns that a fully synchronous solution over a slow network might cut performance by more than half. By default synchronous_standby_names is empty, so synchronous replication is off. It accepts FIRST num_sync for priority lists or ANY num_sync for quorum lists. Choose per cluster, using the data-loss tolerance each application owner signed off.

Back up with pgBackRest for PostgreSQL point-in-time recovery

Point-in-time recovery needs a continuous sequence of archived WAL files extending back at least as far as the start of the base backup. The continuous archiving documentation says to set up and test WAL archiving before taking the first base backup. It calls pg_basebackup the easiest way to take one, and the recovery stop point must fall after the end time of that base backup.

pgBackRest is MIT licensed. It supports parallel backup and restore, full, differential and incremental backups, repository encryption, and S3, Azure, GCS and SFTP repositories. Keep the repository outside the cluster's failure domain and encrypt it. This lets you restore to a chosen moment, such as just before a faulty migration ran.

Replicas are a different protection: a dropped table reaches every standby within seconds. pg_dump and pg_dumpall do not produce file-system-level backups and cannot be part of a continuous-archiving solution. They help when moving one database or restoring one table. Most production clusters need physical backups and an occasional logical export.

If the file system holding pg_wal fills up, PostgreSQL does a PANIC shutdown. No committed transactions are lost, but the database stays offline until space is freed, so monitor archiving and disk growth. A backup is proven only by a restore. On a schedule, restore to a chosen timestamp in an isolated environment, start the dependent application against it, and record the duration and any manual steps.

Harden pg_hba.conf, TLS, SCRAM and OAuth authentication

Control who connects, from where and how, in pg_hba.conf. PostgreSQL uses the first record that matches connection type, client address, database and user, with no fall-through if authentication fails. Edits take effect after pg_ctl reload, pg_reload_conf() or SIGHUP. Check changes with the pg_hba_file_rules view, where a non-null error field marks a problem line.

Enable TLS with ssl = on. The server certificate and key default to server.crt and server.key in the data directory, and on Unix server.key must be 0600 or be root-owned with 0640 permissions, as the SSL documentation explains. To require client certificates, add clientcert=verify-ca or clientcert=verify-full to hostssl lines in pg_hba.conf.

The password authentication documentation calls scram-sha-256 the most secure of the provided password methods and says md5 support is deprecated. The PostgreSQL 19 release logs a warning after successful md5 authentication, which md5_password_warnings can turn off, and removes RADIUS authentication. To move from md5 to scram-sha-256, confirm every client library supports SCRAM, set password_encryption to scram-sha-256, have users set new passwords, and change pg_hba.conf entries to scram-sha-256.

The built-in methods include LDAP, GSSAPI, certificate, PAM and OAuth. PostgreSQL 18 added OAuth 2.0 authentication through extensions to ease single sign-on. The OAuth documentation says PostgreSQL provides no authorization server, client support must be enabled at build time, issuer and scope are required in oauth entries, and OAuth should be used only over SSL. Give each application its own least-privilege role, keep superuser access for named administrators, and keep an approved extension list.

PostgreSQL version policy, 14 end of life and pg_upgrade methods

Each major version is supported for five years after its first release, with minor releases at least every three months. PostgreSQL 14 is now unsupported, and PostgreSQL 19 was released November 19, 2026.

ChangeMethodPlan for
Version choiceRead the versioning policy and pick a major version with years of support left.Final release dates: 15 on November 11, 2027, 16 on November 9, 2028, 17 on November 8, 2029, 18 on November 14, 2030 and 19 on November 19, 2031.
Cluster still on PostgreSQL 14Move to a supported major version with pg_upgrade or logical replication.Version 14 is unsupported. Its final release was November 12, 2026, with 14.25 as the last minor.
Minor releaseStop the server, install the updated binaries and restart, one node at a time in a replicated cluster.Read the release notes, because some need extra steps. The project recommends always running the current minor release.
Major version in placepg_upgrade supports --copy (the default), --link, --clone and --swap. Run --check while the old server is still running, and use --jobs to process databases in parallel. The upgrade documentation says --link can finish in minutes.The link, clone and swap modes need old and new data directories on the same file system. After --link, the old cluster cannot be accessed once the new one starts. After --swap begins moving files, the old cluster can no longer be safely started.
After pg_upgradeInstall matching extension shared objects in the new cluster without running CREATE EXTENSION. Run vacuumdb --all --analyze-in-stages --missing-stats-only and vacuumdb --all --analyze-only afterward.PostgreSQL 18 enables page checksums by default in initdb, so initialize with --no-data-checksums when upgrading a cluster without checksums. The pg_upgrade documentation warns that the destination runs arbitrary code chosen by the source superusers.
Major version with replicationLogical replication feeds a new cluster on the newer version, then clients switch over. The upgrade documentation says switchover needs only several seconds of downtime.PostgreSQL 19 raises wal_level to logical when the first logical slot is created and can replicate sequence values with CREATE PUBLICATION ... FOR ALL SEQUENCES. Tables without a primary key need a replica identity.
Dump and restorepg_dumpall into a new cluster. File system level backups will not work for this method.Downtime grows with database size, so it suits small databases or a deliberate cleanup.

PostgreSQL operating ownership

The database platform team runs the clusters. Application teams own their schemas, queries and data. Writing this split down before go-live keeps failover, restore and upgrade work from stalling on questions of ownership.

Database platform

  • Monitor replication lag, WAL archiving, disk growth and failover state.
  • Apply minor releases and plan major upgrades ahead of end-of-life dates.
  • Tune pooling, memory and autovacuum with evidence.

Application owners

  • Review schema migrations and slow queries before release.
  • Stay within agreed connection limits and confirm retry behavior.
  • Request extensions through review.

Security

  • Review roles, pg_hba.conf changes and superuser access.
  • Move remaining md5 users to scram-sha-256 and rotate application credentials and certificates.
  • Protect backup encryption keys and audit logs.

Recovery

  • Run scheduled point-in-time restores and failover drills.
  • Keep runbooks for switchover, restore and replica rebuild.
  • Record measured recovery time per dependent application.

PostgreSQL deployment questions

Should several applications share one PostgreSQL cluster?

Sharing means fewer clusters to run but ties maintenance windows, upgrades and failure domains together. A major upgrade or a failover affects every application on the cluster at once. Separate clusters suit applications with different recovery targets, owners or extensions. A shared cluster still needs one database and one least-privilege role per application, with connection limits agreed for each.

Do we need PgBouncer for connection pooling?

A pooler helps when applications open many short-lived or idle connections, because PostgreSQL starts a backend process for each connection and idle connections still use memory. Measure peak and active connections first. Check that the pooling mode suits each application, since transaction pooling changes how session state and some prepared statements behave. A pooler can also give clients one stable endpoint after failover.

Should we choose Patroni or CloudNativePG for Kubernetes or virtual machines?

CloudNativePG runs PostgreSQL as Kubernetes resources and handles failover, rolling updates and backup to object storage without Patroni, so it fits teams already operating persistent volumes and stateful workloads. Patroni on virtual machines remains a sound choice for teams without Kubernetes. It keeps leader election in etcd, Consul, ZooKeeper or Kubernetes. Choose the platform your operators already know.

Should PostgreSQL streaming replication be synchronous?

Synchronous replication guarantees a failover will not lose data, while asynchronous replication can lose transactions. The cost is commit latency, and over a slow network a fully synchronous setup might cut performance by more than half. Synchronous replication is off by default. Decide per cluster from the data-loss tolerance the application owner signed off, and measure commit latency before committing to it.

What does PostgreSQL 14 end of life mean for our clusters?

PostgreSQL 14 is unsupported. Its final release was November 12, 2026, with 14.25 as the last minor. Each major version is supported for five years after its first release, so a cluster still on 14 gets no further minor releases. Plan a move to a supported major version with pg_upgrade or logical replication. Versions 15 through 19 are supported, and 19.0 was released November 19, 2026.

How do we move from md5 to scram-sha-256?

Confirm all client libraries support SCRAM. Set password_encryption to scram-sha-256, then have every user set a new password so a SCRAM verifier is stored. Change the pg_hba.conf entries to scram-sha-256 and verify them in the pg_hba_file_rules view. PostgreSQL 19 logs a warning after successful md5 authentication, which helps you find users who still log in with md5 passwords.

How does the PostgreSQL Docker image behave?

The official postgres image requires a non-empty POSTGRES_PASSWORD. Its latest tag points to 19.0 on Debian trixie, and 18.7, 17.12, 16.16 and 15.20 are also listed. From PostgreSQL 18, PGDATA is version specific, such as /var/lib/postgresql/18/docker, and the declared volume is /var/lib/postgresql. Versions 17 and earlier use /var/lib/postgresql/data. Scripts in /docker-entrypoint-initdb.d run only on an empty data directory.

How do we move an existing database onto the new cluster?

Within the same major version, a physical replica or a restore from backup is simplest. Across versions or from a managed cloud database, use logical replication or dump and restore. Compare extensions, roles, collations and settings first. Plan how sequence values and tables without a primary key will move, since logical replication handles them separately.

Scope and handover for a PostgreSQL deployment

DeployOpen plans the topology with you, deploys PostgreSQL with the chosen high-availability tooling, configures pooling, TLS and authentication, sets up backups and runs a restore test, carries out upgrades, documents runbooks and provides ongoing support if you want it.

Effort depends on the number of clusters and dependent applications, environments, recovery targets, network restrictions and how much data has to move. The dependency list from the first section, with owners, data-loss tolerance, downtime tolerance, peak connections and required extensions, is the most useful input to bring.

Handover includes the cluster inventory with owners, failover design, pg_hba and role model, approved extensions, backup schedule and retention, the latest restore and failover results, alert routes, and the upgrade plan with end-of-life dates.

Sources and further reading

More on PostgreSQL

Talk to our team.

Tell us what you're working on, whether it's a deployment, an audit, a security test or a cyber range. You'll speak with an engineer who can help you scope it.

  • 30-minute call: free, with no obligation.
  • NDA on request: we can sign before you share details.
  • Clear next steps: a scope and plan after the call.