<p>I'm aiming to mount a persistent volume onto a postgres stateful set.  To test persistence, I:</p>
<ul>
<li>deploy the stateful set, with mounted volume (<code>kubectl apply -f postgres.yml</code>)</li>
<li>create a table and insert a row</li>
<li>rescale the stateful set to 0, then delete it (<code>kubectl scale statefulset/postgresql-db --replicas 0 &amp;&amp; kubectl delete statefulset/postgresql-db</code>)</li>
<li>redeploy the stateful set (<code>kubectl apply -f postgres.yml</code>)</li>
</ul>
<p>In my new deployment the db is empty, I expected the table and data to be persistent, why is this?</p>
<p>I've been following a few articles to do this, notably:</p>
<ul>
<li><a href="https://www.bmc.com/blogs/kubernetes-postgresql/" rel="nofollow noreferrer">https://www.bmc.com/blogs/kubernetes-postgresql/</a></li>
<li><a href="https://medium.com/@xcoulon/storing-data-into-persistent-volumes-on-kubernetes-fb155da16666" rel="nofollow noreferrer">https://medium.com/@xcoulon/storing-data-into-persistent-volumes-on-kubernetes-fb155da16666</a></li>
</ul>
<pre class="lang-yaml prettyprint-override"><code># kubectl apply -f postgres.yml
apiVersion: v1
kind: PersistentVolume
metadata:
  name: postgres-pv
  labels:
    type: local
spec:
  storageClassName: manual
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: &quot;/home/tp/projects/bitbuyer&quot;
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: postgres-pvc
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: postgresql-db
spec:
  serviceName: postgresql-db-service
  selector:
    matchLabels:
      app: postgresql-db
  replicas: 1
  template:
    metadata:
      labels:
        app: postgresql-db
    spec:
      containers:
        - name: postgresql-db
          image: postgres:latest
          volumeMounts:
            - name: postgres-pv
              mountPath: /data
          env:
            - name: POSTGRES_PASSWORD
              value: pgpassword
            - name: PGDATA
              value: /data/pgdata
      volumes:
        - name: postgres-pv
          persistentVolumeClaim:
            claimName: postgres-pvc
</code></pre>

<p>Issue could be possible due to</p>
<pre><code>volumeMounts:
- name: postgres-data
  mountPath: /var/lib/postgresql/data
  subPath: pgdata
</code></pre>
<p>however this is what i am using for postgres</p>
<p><a href="https://github.com/harsh4870/Keycloack-postgres-kubernetes-deployment/blob/main/postgres.yaml" rel="nofollow noreferrer">https://github.com/harsh4870/Keycloack-postgres-kubernetes-deployment/blob/main/postgres.yaml</a></p>
<pre><code>apiVersion: v1
kind: Service
metadata:
  name: postgres
spec:
  ports:
  - name: pgql
    port: 5432
    targetPort: 5432
    protocol: TCP
  selector:
    app: postgres
---
apiVersion: apps/v1  
kind: StatefulSet
metadata:
  name: postgres
spec:
  serviceName: &quot;postgres&quot;
  replicas: 1
  selector:
    matchLabels:
      app: postgres
  template:
    metadata:
      labels:
        app: postgres
    spec:
      containers:
      - name: postgres
        image: postgres:9.5
        volumeMounts:
        - name: postgres-data
          mountPath: /var/lib/postgresql/data
          subPath: pgdata
        env:
        - name: POSTGRES_USER
          value: root
        - name: POSTGRES_PASSWORD
          value: password
        - name: POSTGRES_DB
          value: kong
        - name: PGDATA
          value: /var/lib/postgresql/data/pgdata
        ports:
        - containerPort: 5432
      terminationGracePeriodSeconds: 60
  volumeClaimTemplates:
  - metadata:
      name: postgres-data
    spec:
      accessModes:
      - &quot;ReadWriteOnce&quot;
      resources:
        requests:
          storage: 10Gi
</code></pre>
