<p>I am trying to deploy PostgreSQL to GKE and here is my PersistentVolumeClaim definition:</p>
<pre><code>apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: postgres-pvc
  namespace: db
  labels:
    app: imgress-db
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 400Mi
</code></pre>
<p>and this is deployment/service definition:</p>
<pre><code>---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: imgress-db
  namespace: db
spec:
  serviceName: imgress-db
  replicas: 1
  selector:
    matchLabels:
      app: imgress-db
  template:
    metadata:
      labels:
        app: imgress-db
    spec:
      containers:
        - name: imgress-db
          image: postgres
          env:
            - name: POSTGRES_HOST
              valueFrom:
                configMapKeyRef:
                  name: db-configmap
                  key: DATABASE_HOST
            - name: POSTGRES_DB
              valueFrom:
                configMapKeyRef:
                  name: db-configmap
                  key: POSTGRES_DB
            - name: POSTGRES_USER
              valueFrom:
                configMapKeyRef:
                  name: db-configmap
                  key: POSTGRES_USER
            - name: POSTGRES_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: db-secret
                  key: POSTGRES_PASSWORD
          ports:
            - containerPort: 5432
          volumeMounts:
            - name: postgres-data
              mountPath: /var/lib/postgresql/data
      volumes:
        - name: postgres-data
          persistentVolumeClaim:
            claimName: postgres-pvc
      restartPolicy: Always
---
apiVersion: v1
kind: Service
metadata:
  name: imgress-db
  namespace: db
spec:
  selector:
    app: imgress-db
  ports:
    - name: postgres
      port: 5432
</code></pre>
<p>First I run:</p>
<pre><code>kubectl apply -f postgres-pvc.yaml
</code></pre>
<p>and then:</p>
<pre><code>kubectl apply -f postgres-deployment.yaml
</code></pre>
<p>but I get this notorious error when I run <code>kubectl get pods -A</code>:</p>
<pre><code>NAMESPACE   NAME           READY   STATUS             RESTARTS        AGE
db          imgress-db-0   0/1     CrashLoopBackOff   6 (2m15s ago)   8m26s
</code></pre>
<p>For <code>kubectl describe pvc postgres-pvc -n db</code> I get this result:</p>
<pre><code>Name:          postgres-pvc
Namespace:     db
StorageClass:  standard
Status:        Bound
Volume:        pvc-c6369764-1106-4a7d-887c-0e4009968115
Labels:        app=imgress-db
Annotations:   pv.kubernetes.io/bind-completed: yes
               pv.kubernetes.io/bound-by-controller: yes
               volume.beta.kubernetes.io/storage-provisioner: pd.csi.storage.gke.io
               volume.kubernetes.io/storage-provisioner: pd.csi.storage.gke.io
Finalizers:    [kubernetes.io/pvc-protection]
Capacity:      1Gi
Access Modes:  RWO
VolumeMode:    Filesystem
Used By:       imgress-db-0
Events:
  Type    Reason                 Age   From                                                                                              Message
  ----    ------                 ----  ----                                                                                              -------
  Normal  ExternalProvisioning   31m   persistentvolume-controller                                                                       waiting for a volume to be created, either by external provisioner &quot;pd.csi.storage.gke.io&quot; or manually created by system administrator
  Normal  Provisioning           31m   pd.csi.storage.gke.io_gke-e0f710dc594c4eb5ac14-5c62-e039-vm_ca2409ad-83a8-4139-93b4-4fffbacbf44f  External provisioner is provisioning volume for claim &quot;db/postgres-pvc&quot;
  Normal  ProvisioningSucceeded  31m   pd.csi.storage.gke.io_gke-e0f710dc594c4eb5ac14-5c62-e039-vm_ca2409ad-83a8-4139-93b4-4fffbacbf44f  Successfully provisioned volume pvc-c6369764-1106-4a7d-887c-0e4009968115
</code></pre>
<p>and for <code>kubectl describe pod imgress-db-0 -n db</code> I get this result (please pay attention to <code>Back-off restarting failed container</code> on the last line):</p>
<pre><code>Name:             imgress-db-0
Namespace:        db
Priority:         0
Service Account:  default
Node:             gke-imgress-default-pool-e9bdef38-hjhv/10.156.0.5
Start Time:       Fri, 24 Feb 2023 13:44:15 +0500
Labels:           app=imgress-db
                  controller-revision-hash=imgress-db-7f557d4b88
                  statefulset.kubernetes.io/pod-name=imgress-db-0
Annotations:      &lt;none&gt;
Status:           Running
IP:               10.84.2.49
IPs:
  IP:           10.84.2.49
Controlled By:  StatefulSet/imgress-db
Containers:
  imgress-db:
    Container ID:   containerd://96140ec0b0e369ca97822361a770abcb82e27b7924bc90e17111ab354e51d6aa
    Image:          postgres
    Image ID:       docker.io/library/postgres@sha256:901df890146ec46a5cab7a33f4ac84e81bac2fe92b2c9a14fd649502c4adf954
    Port:           5432/TCP
    Host Port:      0/TCP
    State:          Waiting
      Reason:       CrashLoopBackOff
    Last State:     Terminated
      Reason:       Error
      Exit Code:    1
      Started:      Fri, 24 Feb 2023 13:50:09 +0500
      Finished:     Fri, 24 Feb 2023 13:50:11 +0500
    Ready:          False
    Restart Count:  6
    Environment:
      POSTGRES_HOST:      &lt;set to the key 'DATABASE_HOST' of config map 'db-configmap'&gt;  Optional: false
      POSTGRES_DB:        &lt;set to the key 'POSTGRES_DB' of config map 'db-configmap'&gt;    Optional: false
      POSTGRES_USER:      &lt;set to the key 'POSTGRES_USER' of config map 'db-configmap'&gt;  Optional: false
      POSTGRES_PASSWORD:  &lt;set to the key 'POSTGRES_PASSWORD' in secret 'db-secret'&gt;     Optional: false
    Mounts:
      /var/lib/postgresql/data from postgres-data (rw)
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-tfsf9 (ro)
Conditions:
  Type              Status
  Initialized       True
  Ready             False
  ContainersReady   False
  PodScheduled      True
Volumes:
  postgres-data:
    Type:       PersistentVolumeClaim (a reference to a PersistentVolumeClaim in the same namespace)
    ClaimName:  postgres-pvc
    ReadOnly:   false
  kube-api-access-tfsf9:
    Type:                    Projected (a volume that contains injected data from multiple sources)
    TokenExpirationSeconds:  3607
    ConfigMapName:           kube-root-ca.crt
    ConfigMapOptional:       &lt;nil&gt;
    DownwardAPI:             true
QoS Class:                   BestEffort
Node-Selectors:              &lt;none&gt;
Tolerations:                 node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                             node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
  Type     Reason                  Age                    From                     Message
  ----     ------                  ----                   ----                     -------
  Normal   Scheduled               6m51s                  default-scheduler        Successfully assigned db/imgress-db-0 to gke-imgress-default-pool-e9bdef38-hjhv
  Normal   SuccessfulAttachVolume  6m46s                  attachdetach-controller  AttachVolume.Attach succeeded for volume &quot;pvc-c6369764-1106-4a7d-887c-0e4009968115&quot;
  Normal   Pulled                  6m42s                  kubelet                  Successfully pulled image &quot;postgres&quot; in 365.355391ms
  Normal   Pulled                  6m39s                  kubelet                  Successfully pulled image &quot;postgres&quot; in 236.532115ms
  Normal   Pulled                  6m26s                  kubelet                  Successfully pulled image &quot;postgres&quot; in 278.12995ms
  Normal   Created                 5m59s (x4 over 6m42s)  kubelet                  Created container imgress-db
  Normal   Started                 5m59s (x4 over 6m41s)  kubelet                  Started container imgress-db
  Normal   Pulled                  5m59s                  kubelet                  Successfully pulled image &quot;postgres&quot; in 348.694043ms
  Normal   Pulling                 5m10s (x5 over 6m42s)  kubelet                  Pulling image &quot;postgres&quot;
  Normal   Pulled                  5m9s                   kubelet                  Successfully pulled image &quot;postgres&quot; in 288.848351ms
  Warning  BackOff                 95s (x25 over 6m38s)   kubelet                  Back-off restarting failed container
</code></pre>
<p>Result of <code>kubectl logs imgress-db-0 -n db</code>:</p>
<pre><code>The files belonging to this database system will be owned by user &quot;postgres&quot;.
This user must also own the server process.

The database cluster will be initialized with locale &quot;en_US.utf8&quot;.
The default database encoding has accordingly been set to &quot;UTF8&quot;.
The default text search configuration will be set to &quot;english&quot;.

Data page checksums are disabled.

initdb: error: directory &quot;/var/lib/postgresql/data&quot; exists but is not empty
initdb: detail: It contains a lost+found directory, perhaps due to it being a mount point.
initdb: hint: Using a mount point directly as the data directory is not recommended.
Create a subdirectory under the mount point.
</code></pre>
<p>I've already seen some related questions asked on SO but I guess it can be caused by different problems.</p>
<p><strong>What I tried</strong>:</p>
<ol>
<li>adding user and password as an env variables</li>
<li>deleting everything in the namespace and recreating</li>
<li>deleting livenessprobe and readinessprobe from the description</li>
</ol>

<p>I was able to solve the issue by adding two things to description:</p>
<ol>
<li><p><code>PGDATA</code> env variable inside containers:</p>
<pre><code>  env:
    - name: PGDATA
      value: /var/lib/postgresql/data/pgdata
</code></pre>
</li>
<li><p><code>subPath: postgres</code> under <code>volumeMounts</code></p>
</li>
</ol>
<p>Now my deployment file looks like this:</p>
<pre><code>---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: imgress-db
  namespace: db
spec:
  serviceName: imgress-db
  replicas: 1
  selector:
    matchLabels:
      app: imgress-db
  template:
    metadata:
      labels:
        app: imgress-db
    spec:
      containers:
        - name: imgress-db
          image: postgres
          env:
            - name: POSTGRES_HOST
              valueFrom:
                configMapKeyRef:
                  name: db-configmap
                  key: DATABASE_HOST
            - name: POSTGRES_DB
              valueFrom:
                configMapKeyRef:
                  name: db-configmap
                  key: POSTGRES_DB
            - name: POSTGRES_USER
              valueFrom:
                configMapKeyRef:
                  name: db-configmap
                  key: POSTGRES_USER
            - name: POSTGRES_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: db-secret
                  key: POSTGRES_PASSWORD
            - name: PGDATA
              value: /var/lib/postgresql/data/pgdata
          ports:
            - containerPort: 5432
          volumeMounts:
            - name: postgres-data
              mountPath: /var/lib/postgresql/data
              subPath: postgres
      volumes:
        - name: postgres-data
          persistentVolumeClaim:
            claimName: postgres-pvc
      restartPolicy: Always
---
apiVersion: v1
kind: Service
metadata:
  name: imgress-db
  namespace: db
spec:
  selector:
    app: imgress-db
  ports:
    - name: postgres
      port: 5432
</code></pre>
