Tech Notes

Series Kubernetes & Helm from scratch · Part 7

PostgreSQL with the Bitnami Helm chart

Install a persistent PostgreSQL into the data namespace with the Bitnami Helm chart, an existing Secret and a small values file, then connect the backend to it.

At the end of part 6 we deleted the throwaway PostgreSQL Deployment. It had no volume, its password sat in plain YAML, and every pod restart wiped the users table. A real database on Kubernetes needs a StatefulSet, a PersistentVolumeClaim, a Secret, two Services, probes and sensible security settings. We could write all of that ourselves, but this is exactly where a ready-made chart pays for itself.

In this part we install PostgreSQL with the Bitnami chart into the data namespace. We keep our own settings in a small overrides file in charts/postgresql, keep the passwords in a Secret we create ourselves, check that the data survives a pod being deleted, and point the backend at the new database.

What the chart creates

One helm install creates a handful of objects. It helps to know what they are before running it, because these are the names you’ll type in every debugging command afterwards.

The backend pod in the apps namespace connects through the postgresql Service to the postgresql-0 pod in the data namespace; the pod reads its passwords from the postgres-credentials Secret and stores its data on the data-postgresql-0 PersistentVolumeClaim. namespace: appsbackend podSpring Boot + Flyway namespace: data · release postgresql svc/postgresqlClusterIP · 5432 svc/postgresql-hlheadless, for the StatefulSet pod postgresql-0StatefulSet replica postgres-credentialsSecret we create first pvc data-postgresql-02Gi, ReadWriteOnce PersistentVolumeMinikube "standard" class
The pod can be deleted and recreated at any time; the PVC and the Secret are what make it the same database afterwards.

Two things stand out. The chart uses a StatefulSet, not a Deployment, so the pod gets a stable name (postgresql-0) and its own PVC (data-postgresql-0) that follows it across restarts. And there are two Services: the normal ClusterIP one we connect to, and a headless -hl one that the StatefulSet uses to give each pod a stable DNS name.

Step 1: the Secret comes first

We could let the chart generate passwords, but then they live in a Secret owned by the Helm release and we’d have to fish them out for the backend. Instead we create the Secret ourselves and tell the chart to use it. The chart’s default key names are postgres-password (the postgres superuser) and password (our application user).

PG_ADMIN_PASSWORD="$(openssl rand -base64 24)"
PG_APP_PASSWORD="$(openssl rand -base64 24)"

kubectl create secret generic postgres-credentials -n data \
  --from-literal=postgres-password="$PG_ADMIN_PASSWORD" \
  --from-literal=password="$PG_APP_PASSWORD"

Secrets are namespaced, and the backend runs in apps, so it can’t read this one. We update the backend-secrets Secret from part 6 with the same application password. Piping a --dry-run through kubectl apply replaces the existing Secret instead of failing because it already exists:

kubectl create secret generic backend-secrets -n apps \
  --from-literal=JWT_SECRET="$(kubectl get secret backend-secrets -n apps -o jsonpath='{.data.JWT_SECRET}' | base64 -d)" \
  --from-literal=DB_PASSWORD="$PG_APP_PASSWORD" \
  --dry-run=client -o yaml | kubectl apply -f -

The first --from-literal reads the current JWT secret back out, so existing tokens stay valid. Nothing in either command ends up in a file in the repository.

Step 2: a small overrides file

The Bitnami chart has hundreds of values. We only override the few we care about, in charts/postgresql/values-dev.yaml. Everything else keeps the chart’s defaults, which is what makes upgrading the chart later manageable.

auth:
  database: auth                        # created on first start
  username: auth                        # owner of that database
  existingSecret: postgres-credentials  # use our Secret, don't generate one
  secretKeys:
    adminPasswordKey: postgres-password
    userPasswordKey: password

architecture: standalone                # one primary, no read replicas

primary:
  persistence:
    enabled: true
    size: 2Gi                           # storageClass left empty = cluster default
  resources:
    requests: { cpu: 100m, memory: 256Mi }
    limits: { memory: 512Mi }

Line by line:

  • auth.database and auth.username match the DB_URL and DB_USERNAME the backend already uses, so Flyway’s V1 migration runs against an empty auth database owned by the auth user.
  • existingSecret tells the chart to read passwords from our Secret. When it’s set, the plain auth.password values are ignored, so a password can never sneak into a values file by accident.
  • secretKeys names the keys inside that Secret. These are the defaults, written out so the link between the Secret and the chart is visible.
  • architecture: standalone is a single primary. The alternative, replication, adds read replicas; we’ll come back to that at the end.
  • persistence.size: 2Gi is plenty for a users table. Leaving storageClass unset uses Minikube’s default standard class, which provisions a hostPath volume inside the node.
  • resources replaces the chart’s built-in resource preset with numbers we chose, in line with the requests and limits we set for our own apps in part 6.

To see every value you could set, and the defaults for the version you’re about to install, ask Helm:

helm show values oci://registry-1.docker.io/bitnamicharts/postgresql | less

Step 3: lint, preview, install

Bitnami publishes its charts as OCI artefacts, so there’s no helm repo add; the chart reference is a registry URL. Pin the chart version, so that rerunning the install next month gives the same database. This reads the latest version once and reuses it:

PG_CHART=oci://registry-1.docker.io/bitnamicharts/postgresql
PG_CHART_VERSION="$(helm show chart "$PG_CHART" | awk '/^version:/ {print $2}')"
echo "$PG_CHART_VERSION"   # write this down in charts/postgresql/README.md

helm lint only accepts a chart directory on disk, not a registry reference, so pull a local copy first and lint that together with our overrides:

helm pull "$PG_CHART" --version "$PG_CHART_VERSION" --untar --untardir /tmp/charts
helm lint /tmp/charts/postgresql -f charts/postgresql/values-dev.yaml

helm template renders the manifests without touching the cluster, which is a quick way to check that existingSecret took effect:

helm template postgresql /tmp/charts/postgresql -n data \
  -f charts/postgresql/values-dev.yaml | grep -n "postgres-credentials"

Then install:

helm install postgresql "$PG_CHART" \
  --version "$PG_CHART_VERSION" \
  --namespace data \
  -f charts/postgresql/values-dev.yaml

kubectl get pods,svc,pvc -n data

The release name postgresql matches the chart name, so the chart doesn’t prefix anything and the Service is simply postgresql. After a minute postgresql-0 should be 1/1 Running and data-postgresql-0 should be Bound. The install output also prints connection notes; they’re worth reading once.

Gotcha: in 2025 Bitnami changed which container images are freely available, moving older versioned image tags to a separate bitnamilegacy repository. If the pod sits in ImagePullBackOff, run kubectl describe pod postgresql-0 -n data to see the image it tried to pull, and check the image section of the chart's own README for the version you installed before overriding image.repository.

Step 4: connect and prove it persists

Every Service gets a DNS name built from the same parts. This is the address the backend will use.

The address postgresql.data.svc.cluster.local:5432 is made of the Service name, the namespace, the fixed svc label, the cluster domain and the port. postgresqlService name .datanamespace .svc"a Service" .cluster.localcluster domain :5432port From apps: the full name, or at least postgresql.dataFrom data: just postgresql
Each pod's resolver searches its own namespace first, which is why short names only work from inside the same namespace.

Let’s connect from a throwaway client pod, using the plain postgres:16 image, which already contains psql:

kubectl run psql-client --rm -it --restart=Never -n data \
  --image=postgres:16 \
  --env=PGPASSWORD="$(kubectl get secret postgres-credentials -n data -o jsonpath='{.data.password}' | base64 -d)" \
  -- psql -h postgresql -U auth -d auth -c "select current_user, version();"

--rm deletes the pod when psql exits, and -h postgresql works because the client runs in data too. Now the important test, the one the part 6 setup would have failed:

  1. Write a row Run the client again with -c "create table smoke(id int); insert into smoke values (1);"
  2. Kill the database pod kubectl delete pod postgresql-0 -n data. The StatefulSet recreates it with the same name.
  3. Watch it reattach kubectl get pods -n data -w until it's 1/1 Running. kubectl get pvc -n data shows the same data-postgresql-0, never a new one.
  4. Read it back, then tidy up -c "select * from smoke; drop table smoke;" returns our row, so the data outlived the pod.

Step 5: point the backend at it

The backend Deployment from part 6 still points at the old postgres Service. Change its URL and let the Deployment roll:

kubectl set env deploy/backend -n apps \
  DB_URL="jdbc:postgresql://postgresql.data.svc.cluster.local:5432/auth"

kubectl rollout status deploy/backend -n apps
kubectl logs -n apps deploy/backend | grep -i flyway

kubectl set env edits the pod template, which triggers a new rollout, and the new pod also picks up the updated DB_PASSWORD from backend-secrets. The logs should show Flyway applying V1 to the empty database. Port-forward the frontend as in part 6 and register a user, then delete postgresql-0 again and log in: the account is still there.

We’ll move the backend into its own Helm chart in part 9, and this URL will become a value instead of a kubectl set env.

Gotcha: the passwords are applied only when the data directory is first initialised. Changing the Secret afterwards doesn't change the password inside PostgreSQL. And helm uninstall leaves the PVC behind on purpose, so a reinstall with a new Secret starts on the old data with the old passwords and fails to authenticate. For a clean slate on Minikube, also run kubectl delete pvc data-postgresql-0 -n data.

How production PostgreSQL differs

What we have is a sound single-instance database for development. In production, a few more things matter:

Replication A standby that can take over when the primary's node dies. architecture: replication adds read replicas, but automatic failover needs the postgresql-ha chart or a PostgreSQL operator.
Connection pooling Every backend replica holds its own connection pool. PgBouncer in front keeps the number of real server connections low as you scale out.
Backups A PVC isn't a backup. You need scheduled dumps or WAL archiving to object storage, and a restore you've actually tested.
Or don't run it Many teams use a managed database (RDS, Cloud SQL, Azure Database). The backend only needs a different DB_URL and Secret.

Storage matters too: a cloud storage class with volume expansion enabled, and a NetworkPolicy so only the apps namespace can reach port 5432. We’ll come back to those in part 14.

Takeaways

  • Use a chart for stateful infrastructure: the Bitnami chart gives you a StatefulSet, PVC, Services and probes from one small values file.
  • Create the Secret yourself and pass auth.existingSecret, so passwords never appear in values files and the backend can share them.
  • Pin the chart version; helm lint needs a local chart, so helm pull --untar first, and use helm template to preview.
  • From another namespace, connect with <service>.<namespace>.svc.cluster.local.
  • The PVC is what makes the pod the same database after a restart, and it outlives helm uninstall, together with the original passwords.
  • Production adds replication, pooling and tested backups, or swaps the whole thing for a managed database.

Next, in part 8, we install Redis for the MFA codes using the same Bitnami pattern, and choose how much persistence a short-lived cache really needs.