<p>I'm trying to deploy Postgresql on Azure Kubernetes with data persistency. So I'm using PVC.
I searched lots of posts on here, most of them offered yaml files like below, but it's giving the error below;</p>
<pre><code>chmod: changing permissions of '/var/lib/postgresql/data/pgdata': Operation not permitted
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: could not change permissions of directory &quot;/var/lib/postgresql/data/pgdata&quot;: Operation not permitted
fixing permissions on existing directory /var/lib/postgresql/data/pgdata ...
</code></pre>
<p>deployment yaml file is below;</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: postgresql
spec:
  replicas: 1
  selector:
    matchLabels:
      app: postgresql
  template:
    metadata:
      labels:
        app: postgresql
    spec:
      containers:
        - name: postgresql
          image: postgres:13.2
          securityContext:
            runAsUser: 999
          imagePullPolicy: &quot;IfNotPresent&quot;
          ports:
            - containerPort: 5432
          envFrom:
            - secretRef:
                name: postgresql-secret
          volumeMounts:
            - mountPath: /var/lib/postgresql/data
              name: postgredb-kap
      volumes:
        - name: postgredb-kap
          persistentVolumeClaim:
            claimName: postgresql-pvc
</code></pre>
<p>Secret yaml is below;</p>
<pre><code>apiVersion: v1
kind: Secret
metadata:
  name: postgresql-secret
type: Opaque
data:
  POSTGRES_DB: a2V5sd4=
  POSTGRES_USER: cG9zdGdyZXNhZG1pbg==
  POSTGRES_PASSWORD: c234Rw==
  PGDATA: L3Za234dGF0YQ==
</code></pre>
<p>pvc and sc yaml files are below:</p>
<pre><code>kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: postgresql-pvc
  labels:
    app: postgresql
spec:
  storageClassName: postgresql-sc
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 5Gi
---
allowVolumeExpansion: true
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: postgresql-sc
mountOptions:
- dir_mode=0777
- file_mode=0777
- uid=1000
- gid=1000
parameters:
  skuName: Standard_LRS
provisioner: kubernetes.io/azure-file
reclaimPolicy: Retain
</code></pre>
<p>So when I use the mountpath like &quot;<strong>- mountPath: /var/lib/postgresql/</strong>&quot;, it's working. I can reach the DB and it's good. But when I delete the pod and recreating, there is no DB! So no data persistency.</p>
<p>Can you please help, what am I missing here?</p>
<p>Thanks!</p>

yatta
<p>One thing you could try is to change <code>uid=1000,gid=1000</code> in mount options to 999 since this is the uid of postgres user in postgres conatiner (I didn't test this).</p>
<hr />
<p>Another solution that will for certain solve this issue involves init conatainers.</p>
<p>Postgres container requires to start as root to be able to <code>chown</code> pgdata dir since its mounted as root dir. After it does this, it drops root permisions and runs as postgres user.</p>
<p>But you can use init container (running as root) to chmod the volume dir so that you can run main container as non-root.</p>
<p>Here is an example:</p>
<pre><code>  initContainers:
    - name: init
      image: alpine
      command: [&quot;sh&quot;, &quot;-c&quot;, &quot;chown 999:999 /var/lib/postgresql/data&quot;]
      volumeMounts:
        - mountPath: /var/lib/postgresql/data
          name: postgredb-kap
</code></pre>
