<p>I am trying to run a Postgresql database using minikube with a persistent volume claim. These are the yaml specifications:</p>

<p><strong>minikube-persistent-volume.yaml:</strong></p>

<pre><code>kind: PersistentVolume
apiVersion: v1
metadata:
  name: pv0001
  labels:
    type: hostpath
spec:
  capacity:
    storage: 5Gi
  accessModes:
    - ReadWriteMany
  hostPath:
    path: "/Users/jonathan/data"
</code></pre>

<p><strong>postgres-persistent-volume-claim.yaml:</strong></p>

<pre><code>apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pv-postgres
spec:
  accessModes: [ "ReadWriteMany" ]
  resources:
    requests:
      storage: 2Gi
</code></pre>

<p><strong>postgres-deployment.yaml</strong></p>

<pre><code>apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: postgres
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: postgres
    spec:
      containers:
      - image: postgres:9.5
        name: postgres
        ports:
        - containerPort: 5432
          name: postgres
        volumeMounts:
        - mountPath: /var/lib/postgresql/data
          name: postgres-disk
        env:
        - name: PGDATA
          value: /var/lib/postgresql/data/pgdata
        - name: POSTGRES_USER
          value: keycloak
        - name: POSTGRES_DATABASE
          value: keycloak
        - name: POSTGRES_PASSWORD
          value: key
        - name: POSTGRES_ROOT_PASSWORD
          value: masterkey
      volumes:
      - name: postgres-disk
        persistentVolumeClaim:
          claimName: pv-postgres
</code></pre>

<p>when I start this I get the following in the logs from the deployment:</p>

<pre><code>[...]
fixing permissions on existing directory 
/var/lib/postgresql/data/pgdata ... ok
initdb: could not create directory "/var/lib/postgresql/data/pgdata/pg_xlog": Permission denied
initdb: removing contents of data directory "/var/lib/postgresql/data/pgdata"
</code></pre>

<p>Why do I get this Permission denied error and what can I do about it?</p>

<p>Maybe you're having a write permission issue with Virtualbox mounting those host folders.
Instead, use <code>/data/postgres</code> as a path and things will work.</p>

<p>Minikube automatically persists the following directories so they will be preserved even if your VM is rebooted/recreated:</p>

<ul>
<li><code>/data</code></li>
<li><code>/var/lib/localkube</code></li>
<li><code>/var/lib/docker</code></li>
</ul>

<p>Read these sections for more details:</p>

<ol>
<li><a href="https://github.com/kubernetes/minikube#persistent-volumes" rel="noreferrer">https://github.com/kubernetes/minikube#persistent-volumes</a></li>
<li><a href="https://github.com/kubernetes/minikube#mounted-host-folders" rel="noreferrer">https://github.com/kubernetes/minikube#mounted-host-folders</a></li>
</ol>
