<p>As <a href="https://hub.docker.com/_/postgres" rel="nofollow noreferrer">the documentation</a> shows, you should be setting the env vars when doing a <code>docker run</code> like the following:</p>

<pre><code>docker run --name some-postgres -e POSTGRES_PASSWORD='foo' POSTGRES_USER='bar'
</code></pre>

<p>This sets the superuser and password to access the database instead of the defaults of <code>POSTGRES_PASSWORD=''</code> and <code>POSTGRES_USER='postgres'</code>.</p>

<p>However, I'm using Skaffold to spin up a k8s cluster and I'm trying to figure out how to do something similar. How does one go about doing this for Kubernetes and Skaffold?</p>

cjones
<p>@P Ekambaram is correct but I would like to go further into this topic and explain the "whys and hows".</p>

<p>When passing passwords on Kubernetes, it's highly recommended to use encryption and you can do this by using secrets. </p>

<p><strong>Creating your own Secrets</strong> (<a href="https://kubernetes.io/docs/concepts/configuration/secret/#creating-your-own-secrets" rel="nofollow noreferrer">Doc</a>)</p>

<p>To be able to use the secrets as described by @P Ekambaram, you need to have a secret in your kubernetes cluster. </p>

<p>To easily create a secret, you can also create a Secret from generators and then apply it to create the object on the Apiserver. The generators should be specified in a <code>kustomization.yaml</code> inside a directory.</p>

<p>For example, to generate a Secret from literals  <code>username=admin</code>  and  <code>password=secret</code>, you can specify the secret generator in  <code>kustomization.yaml</code>  as</p>

<pre class="lang-sh prettyprint-override"><code># Create a kustomization.yaml file with SecretGenerator
$ cat &lt;&lt;EOF &gt;./kustomization.yaml
secretGenerator:
- name: db-user-pass
  literals:
  - username=admin
  - password=secret
EOF
</code></pre>

<p>Apply the kustomization directory to create the Secret object.</p>

<pre class="lang-sh prettyprint-override"><code>$ kubectl apply -k .
secret/db-user-pass-dddghtt9b5 created
</code></pre>

<p><strong>Using Secrets as Environment Variables</strong> (<a href="https://kubernetes.io/docs/concepts/configuration/secret/#using-secrets-as-environment-variables" rel="nofollow noreferrer">Doc</a>)</p>

<p>This is an example of a pod that uses secrets from environment variables:</p>

<pre><code>apiVersion: v1
kind: Pod
metadata:
  name: secret-env-pod
spec:
  containers:
  - name: mycontainer
    image: redis
    env:
      - name: SECRET_USERNAME
        valueFrom:
          secretKeyRef:
            name: mysecret
            key: username
      - name: SECRET_PASSWORD
        valueFrom:
          secretKeyRef:
            name: mysecret
            key: password
  restartPolicy: Never
</code></pre>

<p>Source: <a href="https://kubernetes.io/docs/concepts/configuration/secret/#using-secrets" rel="nofollow noreferrer">here</a> and <a href="https://kubernetes.io/docs/tasks/inject-data-application/define-environment-variable-container/" rel="nofollow noreferrer">here</a>.</p>
