<p>These are pretty much the steps I have followed in order. Basically what is outlined in the documentation:</p>
<p><a href="https://learn.microsoft.com/en-us/azure/aks/azure-files-dynamic-pv" rel="nofollow noreferrer">https://learn.microsoft.com/en-us/azure/aks/azure-files-dynamic-pv</a></p>
<ol>
<li><code>azure-storage-claim.yaml</code></li>
</ol>
<pre><code>kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
  name: test-app-sc
provisioner: kubernetes.io/azure-file
mountOptions:
  - dir_mode=0777
  - file_mode=0777
  - uid=1000
  - gid=1000
  - mfsymlinks
  - nobrl
  - cache=none
parameters:
  skuName: Standard_LRS
  location: westus
</code></pre>
<ol start="2">
<li><code>azure-storage.yaml</code></li>
</ol>
<pre><code>apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: test-app-storage
spec:
  accessModes:
    - ReadWriteMany
  storageClassName: test-app-sc
  resources:
    requests:
      storage: 15Gi
</code></pre>
<p>PVC is now setup.</p>
<p>Changed the mountPath per the Postgres image documentation:</p>
<blockquote>
<p>PGDATA</p>
<p>This optional variable can be used to define another location - like a subdirectory - for the database files. The default is /var/lib/postgresql/data, but if the data volume you're using is a filesystem mountpoint (like with GCE persistent disks), Postgres initdb recommends a subdirectory (for example /var/lib/postgresql/data/pgdata ) be created to contain the data.</p>
<p>This is an environment variable that is not Docker specific. Because the variable is used by the postgres server binary (see the PostgreSQL docs), the entrypoint script takes it into account.<br></p>
</blockquote>
<p>Based on that, I have my <code>postgres.yaml</code> setup like the following:</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: postgres-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      component: postgres
  template:
    metadata:
      labels:
        component: postgres
    spec:
      containers:
        - name: postgres
          image: postgres:11-alpine
          ports:
            - containerPort: 5432
          env: 
            - name: POSTGRES_DB
              valueFrom:
                secretKeyRef:
                  name: test-app-secrets
                  key: PGDATABASE
            - name: POSTGRES_USER
              valueFrom:
                secretKeyRef:
                  name: test-app-secrets
                  key: PGUSER
            - name: POSTGRES_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: test-app-secrets
                  key: PGPASSWORD
            - name: POSTGRES_INITDB_ARGS
              value: &quot;-A md5&quot;
            - name: PGDATA
              value: /var/lib/postgresql-data
          volumeMounts:
            - name: test-app-storage
              mountPath: /var/lib/postgresql-data
              subPath: postgres-storage
      volumes:
        - name: test-app-storage
          persistentVolumeClaim:
            claimName: test-app-storage
---
apiVersion: v1
kind: Service
metadata:
  name: postgres-cluster-ip-service
spec:
  type: ClusterIP
  selector:
    component: postgres
  ports:
    - port: 1423
      targetPort: 5432
</code></pre>
<p>You get the error:</p>
<pre><code>chmod: changing permissions of '/var/lib/postgresql-data': Operation not permitted
</code></pre>
<p>So with either of that as the Dockerfile:</p>
<pre><code>FROM postgres:11-alpine
EXPOSE 5432
RUN /bin/bash -c 'chmod 777 /var/lib/postgresql-data'

</code></pre>
<p>Or</p>
<pre><code>FROM postgres:11-alpine
EXPOSE 5432
</code></pre>
<p>It doesn't really matter, you still get the same type of error by doing any the following:</p>
<pre><code>            ...
            - name: POSTGRES_INITDB_ARGS
              value: &quot;-A md5&quot;
          volumeMounts:
            - name: test-app-storage
              mountPath: /var/lib/postgresql-data
              subPath: postgres-storage
      volumes:
        - name: test-app-storage
          persistentVolumeClaim:
            claimName: test-app-storage
     ...
</code></pre>
<p>Results in the following error:</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 If you want to create a new database system, either remove or empty the directory &quot;/var/lib/postgresql-data&quot; or run initdb with an argument other than &quot;/var/lib/postgresql-data&quot;.
</code></pre>
<p>Try this:</p>
<pre><code>            ...
            - name: POSTGRES_INITDB_ARGS
              value: &quot;-A md5&quot;
          volumeMounts:
            - name: test-app-storage
              mountPath: /var/lib/postgresql-data
              subPath: postgres-storage
      volumes:
        - name: test-app-storage
          persistentVolumeClaim:
            claimName: test-app-storage
     ...
</code></pre>
<p>And it results in this:</p>
<pre><code>chmod: changing permissions of '/var/lib/postgresql-data': Operation not permitted
</code></pre>
<p>Try this:</p>
<pre><code>            ...
            - name: POSTGRES_INITDB_ARGS
              value: &quot;-A md5&quot;
              value: &quot;-D /var/lib/postgresql/data/pgdata&quot;
          volumeMounts:
            - name: test-app-storage
              mountPath: /var/lib/postgresql/data/pgdata
              subPath: postgres-storage
      volumes:
        - name: test-app-storage
          persistentVolumeClaim:
            claimName: test-app-storage
     ...
</code></pre>
<p>And it results in this:</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: 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 ...

So nothing seems to work that I've tried and following the documentation where I can.
</code></pre>
<p>Someone suggested to get rid of the volume mounts like so:</p>
<pre><code>            ...
            - name: POSTGRES_INITDB_ARGS
              value: &quot;-A md5&quot;
            - name: PGDATA
              value: /var/lib/postgresql/data/pgdata
      volumes:
        - name: test-app-storage
          persistentVolumeClaim:
            claimName: test-app-storage
     ...
</code></pre>
<p>Which, hey, that actually works! But doesn't persist data given it just uses the Pod storage so is pretty pointless:</p>
<p><a href="https://i.stack.imgur.com/E8fIG.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/E8fIG.png" alt="enter image description here" /></a></p>
<p>And sure enough when you create a table in Postgres, destroy the Pod, and then redeploy it, of course the table is no longer there.</p>
<p>So more than likely I'm doing something wrong, but I've been following the documentation and seems like this should work.</p>
<p>Where are things going wrong?</p>
<p><strong>EDIT: Permissions in Pod</strong></p>
<p>Apparently it is a permissions issue that occurs when <code>PGDATA</code> is the same directory as <code>mountPath</code>. For example:</p>
<pre><code>  ...
  - name: PGDATA
    value: /var/lib/postgresql-data
volumeMounts:
  - name: test-app-storage
    mountPath: /var/lib/postgresql-data
    subPath: postgres-storage
...

or

  ...
  # if PGDATA is not specified it defaults to /var/lib/postgresql/data
  # - name: PGDATA
  #   value: /var/lib/postgresql-data
volumeMounts:
  - name: test-app-storage
    mountPath: /var/lib/postgresql/data
    subPath: postgres-storage
...

</code></pre>
<p>Something like this where they do not match will create the Pod, but uses Pod storage which I obviously don't want:</p>
<pre><code>  # Thus /var/lib/postgresql/data
  # - name: PGDATA
  #   value: /var/lib/postgresql-data
volumeMounts:
  - name: test-app-storage
    mountPath: /var/lib/postgresql-data
    subPath: postgres-storage
</code></pre>
<p>Permissions <code>ls -l</code> looks like this:</p>
<pre><code>$ ls -l

drwxr-xr-x 1 root     root     4096 Feb  2 06:06 apt
drwxr-xr-x 1 root     root     4096 Feb  2 06:07 dpkg
drwxr-xr-x 2 root     root     4096 Feb  2 06:06 exim4
drwxr-xr-x 2 root     root     4096 Aug 28  2018 logrotate
drwxr-xr-x 2 root     root     4096 Nov 10 12:17 misc
drwxr-xr-x 2 root     root     4096 Jan 30 00:00 pam
drwxr-xr-x 1 postgres postgres 4096 Feb  2 06:07 postgresql
drwxrwxrwx 2     1000     1000    0 Jan 31 21:46 postgresql-data
drwxr-xr-x 1 root     root     4096 Jan 30 00:00 systemd
drwxr-xr-x 3 root     root     4096 Feb  2 06:07 ucf

$ ls -l postgresql &amp;&amp; ls -l postgresql/data &amp;&amp; ls -l postgresql-data
total 4
drwx------ 19 postgres postgres 4096 Feb  5 23:28 data
total 124
drwx------ 6 postgres postgres  4096 Feb  5 23:28 base
drwx------ 2 postgres postgres  4096 Feb  5 23:29 global
drwx------ 2 postgres postgres  4096 Feb  5 23:28 pg_commit_ts
drwx------ 2 postgres postgres  4096 Feb  5 23:28 pg_dynshmem
-rw------- 1 postgres postgres  4281 Feb  5 23:28 pg_hba.conf
-rw------- 1 postgres postgres  1636 Feb  5 23:28 pg_ident.conf
drwx------ 4 postgres postgres  4096 Feb  5 23:33 pg_logical
drwx------ 4 postgres postgres  4096 Feb  5 23:28 pg_multixact
drwx------ 2 postgres postgres  4096 Feb  5 23:28 pg_notify
drwx------ 2 postgres postgres  4096 Feb  5 23:28 pg_replslot
drwx------ 2 postgres postgres  4096 Feb  5 23:28 pg_serial
drwx------ 2 postgres postgres  4096 Feb  5 23:28 pg_snapshots
drwx------ 2 postgres postgres  4096 Feb  5 23:28 pg_stat
drwx------ 2 postgres postgres  4096 Feb  5 23:51 pg_stat_tmp
drwx------ 2 postgres postgres  4096 Feb  5 23:28 pg_subtrans
drwx------ 2 postgres postgres  4096 Feb  5 23:28 pg_tblspc
drwx------ 2 postgres postgres  4096 Feb  5 23:28 pg_twophase
-rw------- 1 postgres postgres     3 Feb  5 23:28 PG_VERSION
drwx------ 3 postgres postgres  4096 Feb  5 23:28 pg_wal
drwx------ 2 postgres postgres  4096 Feb  5 23:28 pg_xact
-rw------- 1 postgres postgres    88 Feb  5 23:28 postgresql.auto.conf
-rw------- 1 postgres postgres 26588 Feb  5 23:28 postgresql.conf
-rw------- 1 postgres postgres    36 Feb  5 23:28 postmaster.opts
-rw------- 1 postgres postgres    94 Feb  5 23:28 postmaster.pid
total 0
</code></pre>
<p>The permissions for where it creates the data files is <code>postgres</code>. However, doing this, it doesn't map to Azure Files and the PVC. It just stays and is destroyed with the Pod.</p>
<p>I think what is happening is <code>mountPath</code> uses <code>root</code> and <code>PGDATA</code> uses <code>postgres</code>, and somehow <code>mountPath</code> is trying to use <code>postgres</code>???</p>
<p>Really, not sure and still lost as to how to resolve it.</p>
<p><strong>EDIT2</strong></p>
<p>Came across this answer:</p>
<p><a href="https://stackoverflow.com/a/51203031/3123109">https://stackoverflow.com/a/51203031/3123109</a></p>
<p>So added the following to mine:</p>
<pre><code>- name: postgres
  image: postgres
  command: 
  - /bin/chown
  - -R
  - &quot;1000&quot;
  - /var/lib/postgresql/data
</code></pre>
<p>But this generates a new error:</p>
<pre><code>The selected container has not logged any messages yet.
</code></pre>
<p>Progress, I guess.</p>

cjones
<p>In the end, the solution was to use Azure Disks instead. Maybe this has been fixed and you can use Azure Files since the beginning of 2020, but I haven't tried since getting this working with Azure Disks.</p>
<p>My <code>.yaml</code> configs look like the following:</p>
<pre><code>apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: file-storage
spec:
  accessModes:
    - ReadWriteMany
  storageClassName: azurefile
  resources:
    requests:
      storage: 25Gi
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: postgres-storage
spec:
  accessModes:
    - ReadWriteOnce
  storageClassName: default
  resources:
    requests:
      storage: 25Gi
</code></pre>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: postgres-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      component: postgres
  template:
    metadata:
      labels:
        component: postgres

    spec:
      containers:
        - name: postgres
          image: postgres:11-alpine
          ports:
            - containerPort: 5432
          env: 
            - name: POSTGRES_DB
              valueFrom:
                secretKeyRef:
                  name: app-prod-secrets
                  key: PGDATABASE
            - name: POSTGRES_USER
              valueFrom:
                secretKeyRef:
                  name: app-prod-secrets
                  key: PGUSER
            - name: POSTGRES_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: app-prod-secrets
                  key: PGPASSWORD
            - name: POSTGRES_INITDB_ARGS
              value: &quot;-A md5&quot;
            - name: PGDATA
              value: /var/postgresql/data
          volumeMounts:
            - name: postgres-storage
              mountPath: /var/postgresql
      volumes:
        - name: postgres-storage
          persistentVolumeClaim:
            claimName: postgres-storage
---
apiVersion: v1
kind: Service
metadata:
  name: postgres-cluster-ip-service
spec:
  type: ClusterIP
  selector:
    component: postgres
  ports:
    - port: 5432
      targetPort: 5432
</code></pre>
