<p>When I deploy postgres like the following everything is ok:</p>
<pre><code>.
.
.
  env:
     - name: POSTGRES_USER
       value: a
.
.
.
</code></pre>
<p>But when I deploy it by configMap or secret for example:</p>
<pre><code>        env:
          - name: POSTGRES_USER
            valueFrom:
              secretKeyRef:
                name: postgres
                key: username
</code></pre>
<p>when I execute psql -U a it returns:
psql: error: could not connect to server: FATAL:  role &quot;a&quot; does not exist</p>
<p>I checked environment <code>POSTGRES_USER</code> and this variable is set to <code>a</code></p>

Mohsen R
<p>Accordingly to the <a href="https://hub.docker.com/_/postgres/" rel="nofollow noreferrer">documentation</a> from the PostgreSQL image:</p>
<blockquote>
<p>The only variable required is POSTGRES_PASSWORD, the rest are
optional.</p>
</blockquote>
<p>You're defining a custom role, which is okay but you're not passing the <code>POSTGRES_PASSWORD</code> which is mandatory. It's possible the role is not created at all if you don't pass the password.</p>
<p>Another important comment from the documentation:</p>
<blockquote>
<p>Warning: the Docker specific variables will only have an effect if you
start the container with a data directory that is empty; any
pre-existing database will be left untouched on container startup.</p>
</blockquote>
<p>If this is not a playground/stateless environment and you're loading existing data, the startup script won't look at the environment variables and you should use credentials that were previously configured.</p>
