Pass postgres parameter into Kubernetes deployment

3/21/2020

I am trying to set a postgres parameter (shared_buffers) into my postgres database pod. I am trying to set an init container to set the db variable, but it is not working because the init container runs as the root user.

What is the best way to edit the db variable on the pods? I do not have the ability to make the change within the image, because the variable needs to be different for different instances. If it helps, the command I need to run is a "postgres -c" command.

"root" execution of the PostgreSQL server is not permitted.
The server must be started under an unprivileged user ID to prevent
possible system security compromise.  See the documentation for
more information on how to properly start the server.
-- mm_wvu18
containers
deployment
docker
kubernetes
postgresql

1 Answer

3/21/2020

You didn't share your Pod/Deployment definition, but I believe you want to set shared_buffers from the command line of the actual container (not the init container) in your Pod definition. Something like this if you are using a deployment:

apiVersion: v1
kind: Deployment
metadata:
  name: postgres
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: postgres
    spec:
      containers:
        - name: postgres
          image: postgres:12.2
          imagePullPolicy: "IfNotPresent"
          command: ["postgres"] <-- add this
          args: ["-D", "-c", "shared_buffers=128MB] <-- add this
          ports:
            - containerPort: 5432
          securityContext:
            runAsUser: 1000
            runAsGroup: 1000
            fsGroup: 1000
          volumeMounts:
            - mountPath: /var/lib/postgresql/data
              name: postgredb
            - name: postgresql-config-volume <-- use if you are using a ConfigMap (see below)
              mountPath: /var/lib/postgres/data/postgresql.conf
      volumes:
        - name: postgredb
          persistentVolumeClaim:
            claimName: postgres-pv-claim <-- note: you need to have this already predefined
        - name: postgresql-config-volume <-- use if you are using a ConfigMap (see below)
          configMap:
            name: postgresql-config

Notice that if you are using a ConfigMap you can also do this (note that you may want to add more configuration options besides shared_buffers):

apiVersion: v1
kind: ConfigMap
metadata:
  name: postgresql-config
data:
  postgresql.conf: |
    shared_buffers=256MB
-- Rico
Source: StackOverflow