PostgreSQL In Kubernetes cannot use DEFAULT_USER from secret but can from value

136 Views Asked by At

I just can't figure out why it is working the way it is. If I

  • use POSTGRES_PASSWORD with hardcoded value in environment it works
  • use POSTGRES_PASSWORD from a secret with valueFrom: secretKeyRef: it works
  • use POSTGRES_USER OR POSTGRES_DB with hardcoded values in environment it works

But if I try to use POSTGRES_USER OR POSTGRES_DB from secret it won't work no matter what I do. And yes after each try I cleared the PV so the pod would be regenerated from scratch.

I want my PostgreSQL Deployment to look something like this:

            containers:
                - name: postgresql
                  image: postgres
                  imagePullPolicy: "IfNotPresent"
                  # ports:
                  #     - containerPort: 5432
                  env:
                  - name: POSTGRES_DB
                    value: whatever
                  - name: POSTGRES_USER
                    # value: login
                    valueFrom:
                        secretKeyRef:
                            name: postgresql-credentials
                            key: username
                  - name: POSTGRES_PASSWORD
                    # value: pass
                    valueFrom:
                        secretKeyRef:
                            name: postgresql-credentials
                            key: password

The secret looks like this:

apiVersion: v1
kind: Secret
data:
    username: dXNlcgo=
    password: cGFzcwo=
metadata:
    name: postgresql-credentials
1

There are 1 best solutions below

0
AudioBubble On

Solved. After looking at the environment of the pod and noticed that POSTGRES_USER had an empty line below it. Basically you need to base64 encode without newline character.