docs

Databases

Declare your app's own Postgres database in its folder. ArgoCD creates it; its password is generated in the cluster, never in Git.

YOUR REPO · helm/apps/<app>/Chart.yamladds cnpg-clustervalues.yamldatabase and role: <app>IN NAMESPACE <app><app>-cluster-1the Postgres pod<app>-cluster-rwthe read-write serviceVolumesdata + WAL, one node's diskCloudNativePG

YOUR REPO · helm/apps/<app>/

  • Chart.yamladds cnpg-cluster
  • values.yamldatabase and role: <app>

ArgoCD syncs your app to the cluster

CloudNativePG

In the namespace <app>, you get

  • <app>-cluster-1the Postgres pod
  • <app>-cluster-rwthe read-write service
  • Volumesdata + WAL, one node's disk
Your repo declares the database, ArgoCD applies it and CloudNativePG runs it. The password is generated in the cluster and never goes through Git. Turn on backups to S3 to archive it continuously outside the cluster, in every pack.

Add a database to your app

<app> is your app's folder in helm/apps/ and its namespace. Make it from helm/apps/hello (see Deploy your own app).

  1. Add the chart

    In Chart.yaml, add cnpg-cluster next to app-template.

    apiVersion: v2
    name: <app>
    type: application
    version: 0.1.0
    dependencies:
      - name: app-template
        repository: oci://ghcr.io/dedsxc
        version: 2.0.0
      - name: cnpg-cluster
        repository: oci://ghcr.io/dedsxc
        version: 5.0.0
  2. Declare the database

    Add this block to values.yaml, at the top level, next to app-template:. Replace <app> with your folder name everywhere.

    cnpg-cluster:
      cluster:
        instances: 1
        annotations:
          argocd.argoproj.io/sync-options: SkipDryRunOnMissingResource=true
        storage:
          size: 5Gi
          storageClass: openebs-hostpath
        walStorage:
          size: 1Gi
          storageClass: openebs-hostpath
        enableSuperuserAccess: false
        roles:
          - name: <app>
            ensure: present
            login: true
            superuser: false
            createdb: false
            inherit: true
            connectionLimit: -1
            passwordSecret:
              name: <app>-cnpg
      database:
        list:
          - name: <app>
            owner: <app>
            secretName: <app>-cnpg

    Keep inherit and connectionLimit: without them, ArgoCD shows the cluster OutOfSync forever.

    openebs-hostpath keeps the data on one node's disk. On a cloud cluster, you can set both storageClass keys to a class from kubectl get storageclass. With Enterprise, Rook replicates volumes across your nodes.

  3. Read the secret in your app

    Pass the connection URL to your container as the variable your app reads:

    app-template:
      controller:
        containers:
          main:
            env:
              DATABASE_URL:
                valueFrom:
                  secretKeyRef:
                    name: <app>-cnpg
                    key: DB_CONNECTION_URI

    For separate settings, read DB_HOST, DB_DATABASE, username and password the same way.

  4. Push and check

    git add helm/apps/<app> && git commit -m "feat(<app>): add a Postgres database" && git push

    Once ArgoCD has synced:

    kubectl -n <app> wait cluster/<app>-cluster --for=condition=Ready --timeout=10m
    kubectl -n <app> get secret <app>-cnpg

You get the Postgres cluster <app>-cluster (one pod, <app>-cluster-1), the secret <app>-cnpg and the read-write service <app>-cluster-rw.

With Enterprise, on a cluster of several nodes, the same database runs as a primary and replicas on different nodes and fails over by itself. Your app keeps using <app>-cluster-rw, which always points to the primary.

Backups to S3

In every pack, CloudNativePG can archive each database continuously to an S3 bucket outside your cluster: AWS S3, Scaleway, OVH, Cloudflare R2, Backblaze B2 or a MinIO on another machine. You can then restore a database to any moment of the last 30 days, by default, even on a new cluster.

  • Off until you set a bucketGive its address in cluster.yaml, and its key to the installer. The key never goes in Git.
  • Keep the keys outside the clusterA restore needs them: the installer exports them for you to keep somewhere safe.
  • Volumes and the whole platformWith Enterprise, every volume goes to the same bucket too, and you can rebuild the whole platform on a new cluster.

Connect with psql

kubectl -n <app> exec -it <app>-cluster-1 -c postgres -- psql -d <app>

No password needed inside the pod.

Back up and restore by hand

A quick copy on your machine, with or without backups to S3: take a dump before a schema migration, an app upgrade or a new pack release.

kubectl -n <app> exec <app>-cluster-1 -c postgres -- pg_dump -Fc -d <app> > <app>.dump

To restore, stop your app so nothing writes to the database. Pause ArgoCD first, or it restarts your app:

  1. Pause ArgoCD

    No app syncs until step 4. Keep the restore short.

    kubectl -n argocd scale statefulset argocd-application-controller --replicas=0
  2. Stop your app

    kubectl -n <app> get deploy
    kubectl -n <app> scale deploy <deployment> --replicas=0

    Use the name it prints as <deployment>.

  3. Restore the dump

    kubectl -n <app> exec -i <app>-cluster-1 -c postgres -- \
      pg_restore --clean --if-exists --no-owner --role=<app> -d <app> < <app>.dump
  4. Start your app and resume ArgoCD

    kubectl -n <app> scale deploy <deployment> --replicas=1
    kubectl -n argocd scale statefulset argocd-application-controller --replicas=1

What deletes your data

Turn on backups to S3 to get a database back after any of these. Until then, take a dump first:

  • Removing or renaming the app folderArgoCD deletes the app, its database and volume. A renamed folder is a new app with an empty database.
  • Uninstalling the platforminstall.sh uninstall deletes every ArgoCD app, databases and volumes included, even though the script says PVCs are kept.
  • The demo uninstalltry.sh uninstall deletes the platform namespaces, Infisical's database included. Never run it on a real install.
  • Pack releasesRead the upgrade notes first. When Postgres moved to the cnpg-cluster chart, ArgoCD pruned the old database clusters and volumes, unless you followed docs/FAQ.md first.
  • Turning off CloudNativePGThe cloudnative-pg entry in argocd/applications/platform/appset.yaml installs the operator that runs every database. Keep it on.
  • Losing the node's diskThe volumes are not replicated: restore the database from your bucket. With Enterprise, Rook keeps copies on other nodes.