Error: PostgreSQL Kubernetes data directory "/var/lib/postgresql/data" has wrong ownership windows

4/12/2020

I am new to kubernetes and stuff. I was going through the tutorials, I encountered a error while using the pstgres database and persistent volume claim. I am pretty sure that all the permissions are being given to the user but still the error suggest that the folder has wrong ownership.

Here are my configuration files

This is the persistent volume claim file

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: database-persistent-volume-claim
spec:
  accessModes: 
    - ReadWriteOnce
  resources:
    requests:
      storage: 2Gi

Here is my postgres deployment file

apiVersion: apps/v1
kind: Deployment
metadata: 
  name: postgres-deployment
spec:
  replicas: 1
  selector:
    matchLabels: 
      component: postgres
  template: 
     metadata:
       labels:
         component: postgres
     spec:
       volumes:
        - name: postgres-storage
          persistentVolumeClaim:
            claimName: database-persistent-volume-claim
       containers:
         - name: postgres
           image: postgres
           ports:
             - containerPort: 5432
           volumeMounts:
             - name: postgres-storage
               mountPath: /var/lib/postgresql/data
               subPath: postgres
           env:
             - name: POSTGRES_HOST_AUTH_METHOD
               value: "trust"
             - name: PGPASSWORD
               valueFrom:
                 secretKeyRef:
                   name: pgpassword
                   key: PGPASSWORD
Here is the error message 
2020-04-12 01:57:11.986 UTC [82] FATAL:  data directory "/var/lib/postgresql/data" has wrong ownership
2020-04-12 01:57:11.986 UTC [82] HINT:  The server must be started by the user that owns the data directory.
child process exited with exit code 1
initdb: removing contents of data directory "/var/lib/postgresql/data"

Any help is appreciated?

-- user3184614
kubernetes
postgresql

1 Answer

4/12/2020

This is working for me check subpath in volume mount

apiVersion: apps/v1
    kind: StatefulSet
    metadata:
    spec:
      podManagementPolicy: OrderedReady
      replicas: 1
      revisionHistoryLimit: 10
      selector:
        matchLabels:
          app: postgres
      serviceName: postgres
      template:
        metadata:
          creationTimestamp: null
          labels:
            app: postgres
        spec:
          containers:
          - env:
            - name: POSTGRES_USER
              value: root
            - name: POSTGRES_PASSWORD
              value: <Password>
            - name: POSTGRES_DB
              value: <DB name>
            - name: PGDATA
              value: /var/lib/postgresql/data/pgdata
            image: postgres:9.5
            imagePullPolicy: IfNotPresent
            name: postgres
            ports:
            - containerPort: 5432
              protocol: TCP
            resources: {}
            terminationMessagePath: /dev/termination-log
            terminationMessagePolicy: File
            volumeMounts:
            - mountPath: /var/lib/postgresql/data
              name: postgres-data
              subPath: pgdata
          dnsPolicy: ClusterFirst
          restartPolicy: Always
          schedulerName: default-scheduler
          securityContext: {}
          terminationGracePeriodSeconds: 60
      updateStrategy:
        rollingUpdate:
          partition: 0
        type: RollingUpdate
      volumeClaimTemplates:
      - metadata:
          creationTimestamp: null
          name: postgres-data
        spec:
          accessModes:
          - ReadWriteOnce
          resources:
            requests:
              storage: 3Gi
          volumeMode: Filesystem
        status:
          phase: Pending
-- Harsh Manvar
Source: StackOverflow