postgresql

by ScriptonBasestar

Helm chartv0.4.0App 16.11

PostgreSQL relational database with replication support

About this chart

PostgreSQL Helm Chart

PostgreSQL relational database with replication support for Kubernetes.

Features

  • ✅ PostgreSQL 16.11 - Latest stable version
  • ✅ Primary-Replica Replication - Built-in streaming replication support
  • ✅ Production-Ready - StatefulSet with persistent storage
  • ✅ Configuration Management - Full postgresql.conf and pg_hba.conf support
  • ✅ Security - Non-root containers, secret management, configurable authentication
  • ✅ High Availability - Multi-replica support with automatic failover preparation
  • ✅ Operational Tools - 40+ Makefile commands for database operations
  • ✅ Backup & Restore - Built-in backup and restore capabilities
  • ✅ Monitoring Ready - Health probes, metrics endpoints, activity monitoring
  • ✅ Customizable - Initialization scripts, extensions, custom configuration

Table of Contents

  • Installation
    • Prerequisites
    • Quick Start
    • Development Installation
    • Production Installation
  • Configuration
    • PostgreSQL Settings
    • Replication Setup
    • Initialization Scripts
    • Security Configuration
  • Replication
    • Primary-Replica Architecture
    • Enabling Replication
    • Monitoring Replication
  • Operations
    • Database Management
    • Backup and Restore
    • Maintenance
    • Monitoring
  • Values Profiles
  • Troubleshooting
  • Production Recommendations
  • Additional Resources

Installation

Prerequisites

  • Kubernetes 1.19+
  • Helm 3.0+
  • PersistentVolume support
  • (Optional) StorageClass for dynamic provisioning

Quick Start

# Add the Helm repository
helm repo add scripton-charts https://scriptonbasestar-container.github.io/sb-helm-charts
helm repo update

# Install with default values (development)
helm install my-postgres scripton-charts/postgresql

# Get the password
export POSTGRES_PASSWORD=$(kubectl get secret my-postgres-secret -o jsonpath="{.data.postgres-password}" | base64 -d)
echo "Password: $POSTGRES_PASSWORD"

# Connect to PostgreSQL
kubectl port-forward svc/my-postgres 5432:5432
PGPASSWORD="$POSTGRES_PASSWORD" psql -h 127.0.0.1 -U postgres -d postgres

Development Installation

For local development and testing:

helm install my-postgres scripton-charts/postgresql \
  -f https://raw.githubusercontent.com/scriptonbasestar-container/sb-helm-charts/master/charts/postgresql/values-dev.yaml

Features:

  • Single node (replicaCount: 1)
  • Reduced resources (512Mi RAM, 500m CPU limit)
  • Smaller storage (5Gi)
  • Development-friendly logging (log all statements)
  • Simple password authentication

Production Installation

For production environments with HA:

# Create namespace
kubectl create namespace production

# Install with production profile
helm install my-postgres scripton-charts/postgresql \
  -n production \
  -f https://raw.githubusercontent.com/scriptonbasestar-container/sb-helm-charts/master/charts/postgresql/values-small-prod.yaml \
  --set postgresql.password='<STRONG_PASSWORD>' \
  --set postgresql.replication.password='<REPLICATION_PASSWORD>' \
  --set persistence.storageClass='fast-ssd'

Features:

  • Primary + replica (replicaCount: 2)
  • Production resources (4Gi RAM, 2 CPU limit)
  • Larger storage (50Gi SSD)
  • Streaming replication enabled
  • Optimized configuration for SSD
  • Anti-affinity rules

Configuration

PostgreSQL Settings

Key configuration parameters in values.yaml:

postgresql:
  database: "postgres"
  username: "postgres"
  password: ""  # Auto-generated if empty

  # Connection Settings
  maxConnections: 100

  # Memory Settings
  sharedBuffers: "128MB"
  effectiveCacheSize: "4GB"
  workMem: "4MB"
  maintenanceWorkMem: "64MB"

  # WAL Settings
  walLevel: "replica"
  maxWalSenders: 10
  maxReplicationSlots: 10

  # Additional postgresql.conf parameters
  config:
    max_parallel_workers: "4"
    checkpoint_timeout: "15min"

Replication Setup

Enable streaming replication for high availability:

replicaCount: 2  # or more

postgresql:
  replication:
    enabled: true
    user: "replicator"
    password: ""  # Auto-generated if empty

Architecture:

  • Pod 0: Primary (master)
  • Pod 1+: Replicas (slaves)
  • Automatic role assignment based on pod index
  • WAL-based streaming replication

Initialization Scripts

Run SQL scripts on database initialization:

postgresql:
  initdbScripts:
    01-create-database.sql: |
      CREATE DATABASE myapp;
      CREATE USER myapp WITH PASSWORD 'myapp_password';
      GRANT ALL PRIVILEGES ON DATABASE myapp TO myapp;

    02-create-extensions.sql: |
      \c myapp
      CREATE EXTENSION IF NOT EXISTS pg_stat_statements;
      CREATE EXTENSION IF NOT EXISTS pgcrypto;
      CREATE EXTENSION IF NOT EXISTS "uuid-ossp";

Scripts are executed in alphabetical order during first startup only.

Security Configuration

Password Management

Development:

# Use simple password
helm install my-postgres scripton-charts/postgresql \
  --set postgresql.password='devpass123'

Production:

# Use strong password from environment variable
helm install my-postgres scripton-charts/postgresql \
  --set postgresql.password="${POSTGRES_PASSWORD}" \
  --set postgresql.replication.password="${REPLICATION_PASSWORD}"

External Secret (Best Practice):

postgresql:
  existingSecret: "my-postgres-secret"

Create secret manually:

kubectl create secret generic my-postgres-secret \
  --from-literal=postgres-password='<strong-password>' \
  --from-literal=replication-password='<replication-password>'

Authentication Methods

The chart uses md5 authentication by default. Customize via pg_hba.conf:

postgresql:
  config:
    # Custom pg_hba.conf rules (extends default)

Or use existing ConfigMap:

postgresql:
  existingConfigMap: "my-postgres-config"

Replication

Primary-Replica Architecture

┌─────────────────┐       ┌─────────────────┐
│   Pod 0         │       │   Pod 1         │
│   (Primary)     │──────>│   (Replica)     │
│   Read/Write    │  WAL  │   Read-only     │
└─────────────────┘       └─────────────────┘
        │                          │
        └──────────┬───────────────┘
                   │
            ┌──────▼──────┐
            │   Headless  │
            │   Service   │
            └─────────────┘

Enabling Replication

  1. Set replica count:
replicaCount: 2  # or more
  1. Enable replication:
postgresql:
  replication:
    enabled: true
    user: "replicator"
    password: "strong-replication-password"
  1. Install/Upgrade:
helm upgrade --install my-postgres scripton-charts/postgresql \
  --set replicaCount=2 \
  --set postgresql.replication.enabled=true \
  --set postgresql.replication.password='<password>'

Monitoring Replication

Check replication status:

make -f make/ops/postgresql.mk pg-replication-status

Output:

  pid  | usename    | application_name | client_addr | state      | sync_state
-------+------------+------------------+-------------+------------+------------
 12345 | replicator | walreceiver      | 10.0.1.23   | streaming  | async

Check replication lag:

make -f make/ops/postgresql.mk pg-replication-lag

Manual check on master:

SELECT * FROM pg_stat_replication;

Manual check on replica:

SELECT pg_is_in_recovery();  -- Should return 't' (true)

Operations

The chart includes 40+ operational commands via Makefile. See make/ops/postgresql.mk.

Database Management

# Open psql shell
make -f make/ops/postgresql.mk pg-shell

# List databases
make -f make/ops/postgresql.mk pg-list-databases

# List tables
make -f make/ops/postgresql.mk pg-list-tables

# List users
make -f make/ops/postgresql.mk pg-list-users

# Get database size
make -f make/ops/postgresql.mk pg-database-size

# Get all databases size
make -f make/ops/postgresql.mk pg-all-databases-size

Backup and Restore

Backup single database:

make -f make/ops/postgresql.mk pg-backup
# Saves to: tmp/postgresql-backups/<database>-<timestamp>.sql

Backup all databases:

make -f make/ops/postgresql.mk pg-backup-all
# Saves to: tmp/postgresql-backups/all-databases-<timestamp>.sql

Restore from backup:

make -f make/ops/postgresql.mk pg-restore FILE=tmp/postgresql-backups/backup.sql

Manual backup (custom database):

kubectl exec postgresql-0 -- sh -c 'PGPASSWORD="$POSTGRES_PASSWORD" pg_dump -U postgres myapp' > myapp-backup.sql

Manual restore:

cat myapp-backup.sql | kubectl exec -i postgresql-0 -- sh -c 'PGPASSWORD="$POSTGRES_PASSWORD" psql -U postgres myapp'

Maintenance

VACUUM:

# Regular VACUUM
make -f make/ops/postgresql.mk pg-vacuum

# VACUUM with ANALYZE (updates statistics)
make -f make/ops/postgresql.mk pg-vacuum-analyze

# VACUUM FULL (reclaims space, locks tables)
make -f make/ops/postgresql.mk pg-vacuum-full

ANALYZE:

make -f make/ops/postgresql.mk pg-analyze

REINDEX:

make -f make/ops/postgresql.mk pg-reindex TABLE=users

Monitoring

Database statistics:

make -f make/ops/postgresql.mk pg-stats

Active connections:

make -f make/ops/postgresql.mk pg-activity

Connection count:

make -f make/ops/postgresql.mk pg-connections

Current locks:

make -f make/ops/postgresql.mk pg-locks

Slow queries (requires pg_stat_statements):

make -f make/ops/postgresql.mk pg-slow-queries

Configuration:

# Show all configuration
make -f make/ops/postgresql.mk pg-config

# Get specific parameter
make -f make/ops/postgresql.mk pg-config-get PARAM=max_connections

# Reload configuration (without restart)
make -f make/ops/postgresql.mk pg-reload

Values Profiles

values-dev.yaml

Development environment profile:

  • Single node (replicaCount: 1)
  • Reduced resources (512Mi RAM, 500m CPU)
  • Smaller storage (5Gi)
  • Debug logging enabled
  • Simple passwords acceptable

values-small-prod.yaml

Small production environment profile:

  • HA setup (replicaCount: 2)
  • Production resources (4Gi RAM, 2 CPU)
  • SSD storage (50Gi)
  • Replication enabled
  • Optimized for SSD
  • Anti-affinity rules
  • Production logging

Troubleshooting

Common Issues

1. Pod not starting - password not set

Error:

Error: password authentication failed

Solution:

helm upgrade my-postgres scripton-charts/postgresql \
  --set postgresql.password='strong-password' \
  --reuse-values

2. Replication not working

Check:

# On master
make -f make/ops/postgresql.mk pg-replication-status

# On replica
kubectl exec postgresql-1 -- sh -c 'PGPASSWORD="$POSTGRES_PASSWORD" psql -U postgres -d postgres -c "SELECT pg_is_in_recovery();"'

Common causes:

  • Replication password not set
  • replicaCount < 2
  • Firewall blocking port 5432

3. Out of disk space

Check disk usage:

kubectl exec postgresql-0 -- df -h /var/lib/postgresql/data

Solutions:

  • Run VACUUM FULL to reclaim space
  • Increase PVC size:
kubectl edit pvc data-postgresql-0
# Edit spec.resources.requests.storage

4. High connection count

Check connections:

make -f make/ops/postgresql.mk pg-connections

Solutions:

  • Increase max_connections
  • Use connection pooling (PgBouncer)
  • Investigate connection leaks

Debugging

View logs:

make -f make/ops/postgresql.mk pg-logs

# All pods
make -f make/ops/postgresql.mk pg-logs-all

Open shell:

make -f make/ops/postgresql.mk pg-bash

Test connection:

make -f make/ops/postgresql.mk pg-ping

Get password:

make -f make/ops/postgresql.mk pg-get-password

Production Recommendations

1. Use Operators for Large Deployments

For production HA with automatic failover, consider PostgreSQL operators:

This chart is designed for:

  • Development environments
  • Small production deployments (1-3 replicas)
  • Deployments where manual failover is acceptable

2. Storage Configuration

Use SSD storage:

persistence:
  storageClass: "fast-ssd"
  size: 100Gi

Set appropriate reclaim policy:

persistence:
  annotations:
    "helm.sh/resource-policy": "keep"

3. Resource Planning

Memory guidelines:

  • shared_buffers: 25% of available RAM
  • effective_cache_size: 75% of available RAM
  • work_mem: shared_buffers / max_connections
  • maintenance_work_mem: 5-10% of available RAM (max 2GB)

Example for 8GB RAM node:

postgresql:
  sharedBuffers: "2GB"
  effectiveCacheSize: "6GB"
  workMem: "20MB"  # 2GB / 100 connections
  maintenanceWorkMem: "512MB"

4. Monitoring

Enable pg_stat_statements:

postgresql:
  initdbScripts:
    00-extensions.sql: |
      CREATE EXTENSION IF NOT EXIS

Deploy postgresql with OpenTenant

Each tenant gets its own release of this chart, with its own namespace, secrets and domain. The builder below already has postgresql on a Hetzner server. Change the host, add apps from the marketplace, then copy the generated files. Providers and prices are samples, and nothing here is sent anywhere.

  1. Pick a serverConnect Hetzner, OVHcloud or AWS. The builder starts on Hetzner, the cheapest sample rate.
  2. Place the chartpostgresql is pinned at v0.4.0 in opentenant.yaml. Drag more apps from the marketplace onto a rack.
  3. ApplySave opentenant.yaml and main.tf from the console, then run the commands below.
# opentenant.yaml, main.tf and dns.tf come from the builder below
opentenant validate opentenant.yaml
terraform init && terraform apply