Postgres pod with local directory mounted for data

I have started minikube with --mount --mount-string “/path/to/local/directory:/data” and kvm2 driver. Followed every tutorial blog to a T to get persistent volume created for postgres to work but keep getting the following error:
The files belonging to this database system will be owned by user “postgres”.
This user must also own the server process.

The database cluster will be initialized with locale "en_US.utf8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".

Data page checksums are disabled.

initdb: directory "/var/lib/postgresql/data" exists but is not empty
If you want to create a new database system, either remove or empty
the directory "/var/lib/postgresql/data" or run initdb
with an argument other than "/var/lib/postgresql/data".

The pv and pvc are created successfully and bound. Here’s the yaml section for postgres pod:

spec:
  hostname: postgres
  containers:
  - name: postgres
    image: postgres:9.5
    imagePullPolicy: "IfNotPresent"
    ports:
      - containerPort: 5432
    volumeMounts:
    - mountPath: /var/lib/postgresql/data
      name: postgresdb
  volumes:
  - name: postgresdb
    persistentVolumeClaim:
      claimName: postgres-pv-claim

Is this a postgres issue? minikube issue? or both? Can’t figure out and spent more than couple of weeks trying to. None of the resources on the internet that say it should work have helped (so far).

Thanks!

I understand it is a postgres issue, but not an expert.

My guess is that initdb expects an empty dir for to create it from scratch, if you already have data and want to use it you probably don’t want to call initdb? I don’t know, just a guess :slight_smile:

Which command are you running that generates that output? My uninformed bet would be that you may want to run another command.

Hi rata,

Thanks for your reply. I am making sure the directory is empty before I start deploying postgres pods. So that is not the issue as far as I have understood.

I am running the usual command to create the pod:

kubectl create -f postgres.yml

The yaml file is the one I posted earlier. There is already a PersistentVolume and a PersistentVolumeClaim created.

The moment I don’t provide a PersistentVolume, the pod comes up happily. It just doesn’t agree with a PersistentVolume created from a local directory.

I have other pods (for nginx) and they seem to be okay with PersistentVolume created from local directory.

Sorry, this might not be the smartest question, but just to be sure. The local directory that you mount is empty too? If you connect to the pod and run ls -a is empty too?

Yes and yes. It appears, so far, that there is no way a local directory from host can be mounted to a postgres container running inside a minikube vm.

That being said, I’m an optimist :slight_smile:

Hi,

I am also facing the same issue, but i am running local kubernetes with docker-desktop in win10 Pro.

Any fix for this issue.

HI
I am also facing mount issue with local kubernetes with docker-desktop.
I get below logs
2020-08-17 15:44:13.159 UTC [51] FATAL: data directory “/var/lib/postgresql/data/dbfiles” has wrong ownership
2020-08-17 15:44:13.159 UTC [51] 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/dbfiles”
Any one has fix for this issue.

looks like its docker desktop issue. I was able to make it work on AKS