Connect Kubernetes Postgres pod to existing database stored on persistent volume

3/13/2020

I am trying to connect a new Wiki.js app instance running in Kubernetes to a Postgres DB created from a prevous app instance and stored in a persistent volume. I get a CrashLoopBackOff try to start the postgres pod, looking at the logs they show:

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".

As you can see it tries to call initdb which fails since there is already data present... my main question is how can I configure to use the existing data instead of trying to create a new database ???

My kubernetes postgres deployment configuration:

Name:                   postgres
Namespace:              wiki
CreationTimestamp:      Fri, 23 Aug 2019 22:17:44 +0000
Labels:                 app=postgres
Annotations:            deployment.kubernetes.io/revision: 36
Selector:               app=postgres
Replicas:               1 desired | 1 updated | 1 total | 0 available | 1 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  1 max unavailable, 1 max surge
Pod Template:
  Labels:  app=postgres
  Containers:
   postgres:
    Image:      postgres:11.5
    Port:       5432/TCP
    Host Port:  0/TCP
    Environment:
      POSTGRES_USER:      <redacted>
      POSTGRES_PASSWORD:  <redacted>
      POSTGRES_DB:        postgresdb
    Mounts:
      /var/lib/postgresql/data from postgresdb (rw)
  Volumes:
   postgresdb:
    Type:       PersistentVolumeClaim (a reference to a PersistentVolumeClaim in the same namespace)
    ClaimName:  postgres-persistant-storage-postgres-0
    ReadOnly:   false

As mentioned simply trying to point to the existing postgres database instead of having it try to create and initialize a completely new one, any help would be greatly appreciated!


UPDATE

When I bash into the Postgres pod and examine the /var/lib/postgresql/data directory I do see the PG_VERSION file as well as a bunch of other db files (screenshot). The PG_VERSION file itself contains the string 11 which makes sense since we're on Postgres 11.5. Unfortunately it still does not recognize it for some reason :(

enter image description here

Here is also my deployment.yaml spec:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  annotations:
    deployment.kubernetes.io/revision: "60"
  creationTimestamp: "2019-08-23T22:17:44Z"
  generation: 184
  labels:
    app: postgres
  name: postgres
  namespace: wiki
  resourceVersion: "91329506"
  selfLink: /apis/extensions/v1beta1/namespaces/wiki/deployments/postgres
  uid: d441d0d8-c5f3-11e9-bc53-9676559ad387
spec:
  progressDeadlineSeconds: 2147483647
  replicas: 1
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      app: postgres
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 1
    type: RollingUpdate
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: postgres
    spec:
      containers:
      - env:
        - name: POSTGRES_USER
          value: postgresadmin
        - name: POSTGRES_PASSWORD
          value: <redacted>
        - name: POSTGRES_DB
          value: wiki
        image: postgres:11.5
        imagePullPolicy: IfNotPresent
        name: postgres
        ports:
        - containerPort: 5432
          protocol: TCP
        resources: {}
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
        volumeMounts:
        - mountPath: /var/lib/postgresql/data
          name: postgresdb
      dnsPolicy: ClusterFirst
      hostname: postgrespod
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      subdomain: postgres
      terminationGracePeriodSeconds: 30
      volumes:
      - name: postgresdb
        persistentVolumeClaim:
          claimName: wiki-data
-- Ergin
azure-kubernetes
kubernetes
persistent-volumes
postgresql
wiki.js

1 Answer

3/14/2020

It looks like the thing the image checks for is a matching file named $PGDATA/PG_VERSION to decide if it needs to initialize or not. See https://github.com/docker-library/postgres/blob/04f28840134bdaf72e4cd6e4a5300e9ceb5ea013/docker-entrypoint.sh#L211-L214

You'll have to make that file exist in your volume.

-- coderanger
Source: StackOverflow