Question

How to configure postgresql using do-block-storage in K8

I have the following configuration

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: postgres-pvc-v1  # Sets name of PVC
  labels:
    app: postgres
spec:
  storageClassName: do-block-storage
  accessModes:
    - ReadWriteOnce  # Sets read and write access
  resources:
    requests:
      storage: 10Gi  # Sets volume size
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: postgres
spec:
  replicas: 1
  selector:
    matchLabels:
      app: postgres
  template:
    metadata:
      labels:
        app: postgres
    spec:
      containers:
        - name: postgres
          image: postgis/postgis # Sets Image
          imagePullPolicy: "IfNotPresent"
          ports:
            - containerPort: 5432  # Exposes container port
          envFrom:
            - configMapRef:
                name: postgres-cfg
          volumeMounts:
            - mountPath: /var/lib/postgresql/data
              name: postgredb
      volumes:
        - name: postgredb
          persistentVolumeClaim:
            claimName: postgres-pvc-v1
---

but in the pod log I have the following error

Data page checksums are disabled.

initdb: error: directory "/var/lib/postgresql/data" exists but is not empty
initdb: detail: It contains a lost+found directory, perhaps due to it being a mount point.
initdb: hint: Using a mount point directly as the data directory is not recommended.
Create a subdirectory under the mount point.

Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Bobby Iliev
Site Moderator
Site Moderator badge
April 5, 2023

Hi there,

To resolve this issue, you can create a subdirectory under the mount point and use that as the PostgreSQL data directory. Modify your Deployment configuration to include the subPath field for the volumeMounts. Here’s an updated configuration:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: postgres
spec:
  replicas: 1
  selector:
    matchLabels:
      app: postgres
  template:
    metadata:
      labels:
        app: postgres
    spec:
      containers:
        - name: postgres
          image: postgis/postgis # Sets Image
          imagePullPolicy: "IfNotPresent"
          ports:
            - containerPort: 5432  # Exposes container port
          envFrom:
            - configMapRef:
                name: postgres-cfg
          volumeMounts:
            - mountPath: /var/lib/postgresql/data
              subPath: pgdata
              name: postgredb
      volumes:
        - name: postgredb
          persistentVolumeClaim:
            claimName: postgres-pvc-v1

For more info on subPath, you can check out the docs here:

https://kubernetes.io/docs/concepts/storage/volumes/#using-subpath

With this change, PostgreSQL will store its data in the pgdata subdirectory of the mounted volume, avoiding the issue with the lost+found directory.

However, on another note, I would personally use a managed Postgres database instead of running it on the Kubernetes cluster directly:

https://docs.digitalocean.com/products/databases/postgresql/

Hope that this helps!

Best,

Bobby

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up