Skip to main content
K8sCalc

kubernetes

PostgreSQL Kubernetes Sizing Calculator

Calculate the right CPU and memory for your PostgreSQL pods on Kubernetes. Based on database size, connection count, and workload type — with postgresql.conf recommendations.

Sizing PostgreSQL on Kubernetes

PostgreSQL memory sizing involves two distinct concerns: caching enough data to avoid disk I/O, and handling the per-connection overhead.

Key PostgreSQL Memory Parameters

ParameterRule of ThumbPurpose
shared_buffers25% of RAMPostgreSQL's own cache
effective_cache_size75% of RAMQuery planner hint (OS cache estimate)
work_memRAM × 0.25 ÷ (max_connections × 4)Per sort/hash operation
maintenance_work_mem256 MB – 2 GBVACUUM, CREATE INDEX

Connection Overhead

Each PostgreSQL backend process uses:

  • ~5 MB base memory
  • Additional memory proportional to query complexity

With 200 connections: 200 × 5 MB = 1 GB just for connection overhead.

PgBouncer in Kubernetes

yaml
# Add PgBouncer as a sidecar or separate Deployment
# Transaction pooling: reduces actual Postgres connections by 10-20×
env:
  - name: POOL_MODE
    value: transaction
  - name: MAX_CLIENT_CONN
    value: "500"
  - name: DEFAULT_POOL_SIZE
    value: "25"

Storage Requirements

Use a dedicated PVC for PostgreSQL data:

yaml
# Size PVC at 2-3× current DB size for growth headroom
storage: 150Gi   # for a 50 GB database

Use Longhorn with 3 replicas for HA storage, or a cloud-managed SSD for managed K8s.

Frequently Asked Questions

How much RAM does PostgreSQL need?

As a baseline, PostgreSQL needs enough RAM for shared_buffers (25% of RAM) to cache your working dataset. For a database that fits in RAM, target: RAM ≥ database_size / 0.75. For write-heavy workloads, this is less critical since the working set is smaller. The calculator combines both the cache requirement and the per-connection overhead (roughly 4–10 MB per connection).

Should I run PostgreSQL directly in Kubernetes?

Yes, with a proper operator. CloudNativePG (CNPG) is the recommended approach — it handles HA, streaming replication, automated backups, and point-in-time recovery. Running Postgres in K8s without an operator is risky. Use a PVC backed by Longhorn or a cloud SSD for the data volume.

What is PgBouncer and should I use it?

PgBouncer is a connection pooler that sits between your app and PostgreSQL. Each PostgreSQL connection consumes ~5–10 MB of RAM and a backend process. With 500 app connections routing through PgBouncer in transaction mode, PostgreSQL only sees 20–50 actual connections — dramatically reducing RAM and CPU usage. Always use PgBouncer in Kubernetes where many app pods connect to the DB.

How do shared_buffers and effective_cache_size differ?

shared_buffers is actual RAM PostgreSQL allocates for its own cache — set to 25% of total RAM. effective_cache_size is just a hint to the query planner about how much OS page cache is available — set to 75% of RAM. Setting effective_cache_size correctly helps the planner choose index scans over sequential scans for large tables.

Related Tools

Related Guides