<p>I have the following pod setup:</p>
<p>Secret:</p>
<pre class="lang-yml prettyprint-override"><code>apiVersion: v1
kind: Secret
metadata:
  name: postgres-secret
  labels:
    app: postgres
type: Opaque
data:
  postgres_user: &lt;base64&gt;
  postgres_password: &lt;base64&gt;
</code></pre>
<p>ConfigMap:</p>
<pre class="lang-yml prettyprint-override"><code>apiVersion: v1
kind: ConfigMap
metadata:
  name: postgres-config
  labels:
    app: postgres
data:
  postgres_dbname: mydb
  postgres_url: postgres
</code></pre>
<p>Deployment/service:</p>
<pre class="lang-yml prettyprint-override"><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: postgres
spec:
  selector:
    matchLabels:
      app: postgres
  replicas: 1
  template:
    metadata:
      labels:
        app: postgres
    spec:
      containers:
        - name: postgres
          image: postgres
          imagePullPolicy: &quot;IfNotPresent&quot;
          ports:
            - containerPort: 5432
          env:
            - name: POSTGRES_USER
              valueFrom:
                secretKeyRef:
                  name: postgres-secret
                  key: postgres_user
            - name: POSTGRES_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: postgres-secret
                  key: postgres_password
            - name: POSTGRES_DB
              valueFrom:
                configMapKeyRef:
                  name: postgres-config
                  key: postgres_dbname

          volumeMounts:
            - mountPath: /var/lib/postgresql/data
              name: postgredb
      volumes:
        - name: postgredb
          persistentVolumeClaim:
            claimName: postgres-pv-claim
---
kind: PersistentVolume
apiVersion: v1
metadata:
  name: postgres-pv-volume
  labels:
    type: local
    app: postgres
spec:
  storageClassName: manual
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteMany
  hostPath:
    path: &quot;/mnt/data&quot;
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: postgres-pv-claim
  labels:
    app: postgres
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 1Gi
---
apiVersion: v1
kind: Service
metadata:
  name: postgres
  labels:
    app: postgres
spec:
  selector:
    app: postgres
  ports:
    - name: postgres
      port: 5432
</code></pre>
<p>When I make the deployment and then try to connect, I get the following error:</p>
<blockquote>
<p>PostgreSQL Database directory appears to contain a database; Skipping
initialization</p>
<p>2024-02-19 19:29:13.773 UTC [1] LOG:  starting PostgreSQL 16.2 (Debian
16.2-1.pgdg120+2) on x86_64-pc-linux-gnu, compiled by gcc (Debian 12.2.0-14) 12.2.0, 64-bit 2024-02-19 19:29:13.773 UTC [1] LOG:  listening on IPv4 address &quot;0.0.0.0&quot;, port 5432 2024-02-19 19:29:13.773
UTC [1] LOG:  listening on IPv6 address &quot;::&quot;, port 5432 2024-02-19
19:29:13.777 UTC [1] LOG:  listening on Unix socket
&quot;/var/run/postgresql/.s.PGSQL.5432&quot; 2024-02-19 19:29:13.792 UTC [29]
LOG:  database system was shut down at 2024-02-19 19:28:39 UTC
2024-02-19 19:29:13.810 UTC [1] LOG:  database system is ready to
accept connections 2024-02-19 19:29:23.612 UTC [33] FATAL:  password
authentication failed for user &quot;&quot; 2024-02-19 19:29:23.612 UTC
[33] DETAIL:  Role &quot;&quot; does not exist.
Connection matched file &quot;/var/lib/postgresql/data/pg_hba.conf&quot; line 128: &quot;host all all all scram-sha-256&quot;</p>
</blockquote>
<p>I have tried the following:</p>
<pre class="lang-bash prettyprint-override"><code>kubectl exec -it postgres-795b8b6ccd-nlxsh -- sh
# psql -h postgres -U &lt;username&gt;
Password for user &lt;username&gt;:
psql: error: connection to server at &quot;postgres&quot; (10.109.22.92), port 5432 failed: FATAL:  password authentication failed for user &quot;&lt;username&gt;&quot;
# echo $POSTGRES_USER
&lt;usernam&gt;
</code></pre>
<p>I can see that my env variables are being set but they aren't being used to create the user. What gives?</p>

CC BY-SA 4.0
2024-02-20T00:34:20.917Z
Rather than posting manifests with censored usernames and passwords, post an actual working configuration. That makes it much easier for someone to grab your manifests and try them locally. You don't need to use your *actual* usernames and passwords, but having valid values is much better than `<base64>`.
larsks
CC BY-SA 4.0
2024-02-20T00:42:08.277Z
Remember that those environment variables are only relevant if you're starting postgres with an empty `/var/lib/postgresql/data`. If there was data in that directory from a previous Postgres instance, the postgres container will use the existing configuration and ignore the environment variables.
larsks
CC BY-SA 4.0
2024-02-20T00:44:58.113Z
I am unable to reproduce this problem; see [my test session here](https://asciinema.org/a/Ii5N4iD7rWZhlqElmvAHs3sej). Also, note that it doesn't make sense to create a `ReadWriteMany` volume for postgres (you should use `ReadWriteOnce`).
larsks
<kubernetes><postgresql>
Postgres pod not creating user despite the `env` variable `POSTGRES_USER`
codehitman
# Postgres pod not creating user despite the `env` variable `POSTGRES_USER`

(Asked by: username_0 on 2024-02-19)

I have the following pod setup:
Secret:
apiVersion: v1
kind: Secret
metadata:
  name: postgres-secret
  labels:
    app: postgres
type: Opaque
data:
  postgres_user: 
  postgres_password: 

ConfigMap:
apiVersion: v1
kind: ConfigMap
metadata:
  name: postgres-config
  labels:
    app: postgres
data:
  postgres_dbname: mydb
  postgres_url: postgres

Deployment/service:
apiVersion: apps/v1
kind: Deployment
metadata:
  name: postgres
spec:
  selector:
    matchLabels:
      app: postgres
  replicas: 1
  template:
    metadata:
      labels:
        app: postgres
    spec:
      containers:
        - name: postgres
          image: postgres
          imagePullPolicy: "IfNotPresent"
          ports:
            - containerPort: 5432
          env:
            - name: POSTGRES_USER
              valueFrom:
                secretKeyRef:
                  name: postgres-secret
                  key: postgres_user
            - name: POSTGRES_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: postgres-secret
                  key: postgres_password
            - name: POSTGRES_DB
              valueFrom:
                configMapKeyRef:
                  name: postgres-config
                  key: postgres_dbname

          volumeMounts:
            - mountPath: /var/lib/postgresql/data
              name: postgredb
      volumes:
        - name: postgredb
          persistentVolumeClaim:
            claimName: postgres-pv-claim
---
kind: PersistentVolume
apiVersion: v1
metadata:
  name: postgres-pv-volume
  labels:
    type: local
    app: postgres
spec:
  storageClassName: manual
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteMany
  hostPath:
    path: "/mnt/data"
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: postgres-pv-claim
  labels:
    app: postgres
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 1Gi
---
apiVersion: v1
kind: Service
metadata:
  name: postgres
  labels:
    app: postgres
spec:
  selector:
    app: postgres
  ports:
    - name: postgres
      port: 5432

When I make the deployment and then try to connect, I get the following error:

PostgreSQL Database directory appears to contain a database; Skipping
initialization
2024-02-19 19:29:13.773 UTC [1] LOG:  starting PostgreSQL 16.2 (Debian
16.2-1.pgdg120+2) on x86_64-pc-linux-gnu, compiled by gcc (Debian 12.2.0-14) 12.2.0, 64-bit 2024-02-19 19:29:13.773 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432 2024-02-19 19:29:13.773
UTC [1] LOG:  listening on IPv6 address "::", port 5432 2024-02-19
19:29:13.777 UTC [1] LOG:  listening on Unix socket
"/var/run/postgresql/.s.PGSQL.5432" 2024-02-19 19:29:13.792 UTC [29]
LOG:  database system was shut down at 2024-02-19 19:28:39 UTC
2024-02-19 19:29:13.810 UTC [1] LOG:  database system is ready to
accept connections 2024-02-19 19:29:23.612 UTC [33] FATAL:  password
authentication failed for user "" 2024-02-19 19:29:23.612 UTC
[33] DETAIL:  Role "" does not exist.
Connection matched file "/var/lib/postgresql/data/pg_hba.conf" line 128: "host all all all scram-sha-256"

I have tried the following:
kubectl exec -it postgres-795b8b6ccd-nlxsh -- sh
# psql -h postgres -U 
Password for user :
psql: error: connection to server at "postgres" (10.109.22.92), port 5432 failed: FATAL:  password authentication failed for user ""
# echo $POSTGRES_USER


I can see that my env variables are being set but they aren't being used to create the user. What gives?

**Tags:** kubernetes, postgresql

> **Comments:**
> **username_1**: Rather than posting manifests with censored usernames and passwords, post an actual working configuration. That makes it much easier for someone to grab your manifests and try them locally. You don't need to use your *actual* usernames and passwords, but having valid values is much better than `<base64>`.  
> **username_1**: Remember that those environment variables are only relevant if you're starting postgres with an empty `/var/lib/postgresql/data`. If there was data in that directory from a previous Postgres instance, the postgres container will use the existing configuration and ignore the environment variables.  
> **username_1**: I am unable to reproduce this problem; see [my test session here](https://asciinema.org/a/Ii5N4iD7rWZhlqElmvAHs3sej). Also, note that it doesn't make sense to create a `ReadWriteMany` volume for postgres (you should use `ReadWriteOnce`).  

---

## 0 answers

