<p>I have the <em>postgres</em> container running in a <code>Pod</code> on <strong>GKE</strong> and a <code>PersistentVolume</code> set up to store the data. However, all of the data in the database is lost if the cluster reboots or if the <code>Pod</code> is deleted. </p>

<p>If I run <code>kubectl delete &lt;postgres_pod&gt;</code> to delete the existing <code>Pod</code> and check the newly created (by <em>kubernetes</em>) <code>Pod</code> to replace the deleted one, the respective database has not the data that it had before the <code>Pod</code> being deleted.</p>

<p>Here are the <em>yaml</em> files I used to deploy <em>postgres</em>. </p>

<pre><code>apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: custom-storage
parameters:
  type: pd-standard
provisioner: kubernetes.io/gce-pd
reclaimPolicy: Retain
volumeBindingMode: Immediate
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: postgres-volume-claim
spec:
  storageClassName: custom-storage
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 5Gi
</code></pre>

<p>deployment.yaml</p>

<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: postgres
spec:
  selector:
    matchLabels:
      app: postgres
  template:
    metadata:
      labels:
        app: postgres
    spec:
      containers:
      - name: postgres
        image: postgres:11.5
        resources: {}
        ports:
        - containerPort: 5432
        env:
          - name: POSTGRES_DB
            value: "dbname"
          - name: POSTGRES_USER
            value: "user"
          - name: POSTGRES_PASSWORD
            value: "password"
        volumeMounts:
          - mountPath: /var/lib/postgresql/
            name: postgresdb
      volumes:
        - name: postgresdb
          persistentVolumeClaim:
            claimName: postgres-volume-claim
</code></pre>

<p>I double checked that the <code>persistentVolumeReclaimPolicy</code> has value <code>Retain</code>. </p>

<p>What am I missing?</p>

rasthiya
<p>Is the cluster creating a new volume each time you delete a pod?  Check with <code>kubectl get pv</code>.  </p>

<p>Is this a multi-zone cluster?  Your storage class is not provisionig regional disks, so you might be getting a new disk when the pod moves from one zone to another.</p>

<p>Possibly related to your problem, the postgres container reference recommends mounting at <code>/var/lib/postgresql/data/pgdata</code> and setting the <code>PGDATA</code> env variable:
<a href="https://hub.docker.com/_/postgres#pgdata" rel="noreferrer">https://hub.docker.com/_/postgres#pgdata</a></p>
