<p>I'm trying to setup PostgreSQL on Minikube with data path being my host folder mounted on Minikube (I'd like to keep my data on host). </p>

<p>With the  kubernetes object created (below) I get permission error, the same one as here <a href="https://stackoverflow.com/questions/41856108/how-to-solve-permission-trouble-when-running-postgresql-from-minikube">How to solve permission trouble when running Postgresql from minikube?</a> although the question mentioned doesn't answer the issue. It advises to mount minikube's VM dir instead.</p>

<pre><code>---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: postgres
  labels:
    app: postgres
spec:
  replicas: 1
  selector:
    matchLabels:
      app: postgres
  template:
    metadata:
      labels:
        app: postgres
    spec:
      containers:
      - name: postgres
        image: postgres          
        volumeMounts:
          - mountPath: /var/lib/postgresql/data
            name: storage
        env:
        - name: POSTGRES_PASSWORD
          value: user
        - name: POSTGRES_USER
          value: pass
        - name: POSTGRES_DB
          value: k8s
      volumes:
        - name: storage
          hostPath:
            path: /data/postgres
</code></pre>

<p>Is there any other way to do that other than building own image on top of Postgres and playing with the permissions somehow? I'm on macOS with Minikube 0.30.0 and I'm experiencing that with both Virtualbox and hyperkit drivers for Minikube.</p>

Michal Ostruszka
<p>Look  at these lines from here : <a href="https://kubernetes.io/docs/concepts/storage/volumes/#hostpath" rel="nofollow noreferrer">hostPath</a></p>

<blockquote>
  <p>the files or directories created on the underlying hosts are only writable by root. You either need to run your process as root in a privileged Container or modify the file permissions on the host to be able to write to a <code>hostPath</code> volume</p>
</blockquote>

<p>So, either you have to run as root or you have to change the file permission of <code>/data/postgres</code> directory.</p>

<p>However, you can run your Postgres container as root without rebuilding docker image.</p>

<p>You have to add following to your container:</p>

<pre><code>securityContext:
  runAsUser: 0
</code></pre>

<p>Your yaml should look like this:</p>

<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: postgres
  labels:
    app: postgres
spec:
  replicas: 1
  selector:
    matchLabels:
      app: postgres
  template:
    metadata:
      labels:
        app: postgres
    spec:
      containers:
      - name: postgres
        image: postgres          
        volumeMounts:
        - mountPath: /var/lib/postgresql/data
          name: storage
        env:
        - name: POSTGRES_PASSWORD
          value: user
        - name: POSTGRES_USER
          value: pass
        - name: POSTGRES_DB
          value: k8s
        securityContext:
          runAsUser: 0
      volumes:
      - name: storage
        hostPath:
          path: /data/postgres
</code></pre>
