<p>I'm trying to deploy postgres as a <code>StatefulSet</code> on <code>k3d</code>.</p>
<p>I set the environment variable <code>POSTGRES_USER</code>, but when I try to connect to the db, it isn't taken into account and I see that authentication has failed. I can login with the password <code>pgpassword</code> and default user <code>postgres</code>.</p>
<p>Why isn't the <code>POSTGRES_USER</code> taken into account?</p>
<p>This is my yml:</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: postgresql-db
spec:
  serviceName: postgresql-db-service
  selector:
    matchLabels:
      app: postgresql-db
  replicas: 2
  template:
    metadata:
      labels:
        app: postgresql-db
    spec:
      containers:
        - name: postgresql-db
          image: postgres:latest
          volumeMounts:
            - name: postgresql-db-disk
              mountPath: /data
          env:
            - name: POSTGRES_USER
              value: pguser
            - name: POSTGRES_PASSWORD
              value: pgpassword
            - name: PGDATA
              value: /data/pgdata
      volumes:
        - name: postgresql-db-disk
          hostPath:
            # directory location on host
            path: /opt/x/projects/tilt_test/data/
            # this field is optional
            type: DirectoryOrCreate
</code></pre>

<blockquote>
<p>but when i try to connect to the db, it isn't taken into account, and i see that authentication has failed.</p>
</blockquote>
<p>It works exactly as it should. The <code>POSTGRES_USER</code> and <code>POSTGRES_PASSWORD</code> system variables are used to create the default user and password when creating the database. <strong>If, on the other hand, you intend to log in to an existing user later, you must set and use system variables: <code>PGUSER</code> and <code>PGPASSWORD</code>.</strong> <a href="https://www.postgresql.org/docs/10/libpq-envars.html" rel="nofollow noreferrer">Here</a> is the explanation:</p>
<blockquote>
<ul>
<li><p><code>PGUSER</code>  behaves the same as the  <a href="https://www.postgresql.org/docs/10/libpq-connect.html#LIBPQ-CONNECT-USER" rel="nofollow noreferrer">user</a>  connection parameter.</p>
</li>
<li><p><code>PGPASSWORD</code>  behaves the same as the  <a href="https://www.postgresql.org/docs/10/libpq-connect.html#LIBPQ-CONNECT-PASSWORD" rel="nofollow noreferrer">password</a>  connection parameter. Use of this environment variable is not recommended for security reasons, as some operating systems allow non-root users to see process environment variables via  ps; instead consider using a password file (see  <a href="https://www.postgresql.org/docs/10/libpq-pgpass.html" rel="nofollow noreferrer" title="33.15. The Password File">Section 33.15</a>).</p>
</li>
</ul>
</blockquote>
<p>You can also find a complete tutorial <a href="https://devopscube.com/deploy-postgresql-statefulset/" rel="nofollow noreferrer">How to Deploy PostgreSQL Statefulset in Kubernetes With High Availability</a>. Additionally, you will find an explanation for <code>POSTGRES_USER</code> and <code>POSTGRES_PASSWORD</code> system variables:</p>
<blockquote>
<p>POSTGRES_USER: The user that should be created automatically when the Postgres process starts.
POSTGRES_PASSWORD: The password for the user created by default.</p>
</blockquote>
<p>In this case, logging into the database takes place during readiness and liveness probes.</p>
