<p>I want to connect to a Postgres instance that it is in a pod in GKE.</p>
<p>I think a way to achieve this can be with kubectl port forwarding.</p>
<p>In my local I have &quot;Docker for desktop&quot; and when I apply the yamls files I am able to connect to the database. The yamls I am using in GKE are almost identical</p>
<p><strong>secrets.yaml</strong></p>
<pre><code>    apiVersion: v1
    kind: Secret
    metadata:
      namespace: staging
      name: postgres-secrets
    type: Opaque
    data:
      MYAPPAPI_DATABASE_NAME: XXXENCODEDXXX
      MYAPPAPI_DATABASE_USERNAME: XXXENCODEDXXX
      MYAPPAPI_DATABASE_PASSWORD: XXXENCODEDXXX

</code></pre>
<p><strong>pv.yaml</strong></p>
<pre><code>    apiVersion: v1
    kind: PersistentVolume
    metadata:
      namespace: staging
      name: db-data-pv
      labels:
        type: local
    spec:
      storageClassName: generic
      capacity: 
        storage: 1Gi
      accessModes:
        - ReadWriteMany
      hostPath:
        path: &quot;/var/lib/postgresql/data&quot;
</code></pre>
<p><strong>pvc.yaml</strong></p>
<pre><code>    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      namespace: staging
      name: db-data-pvc
    spec:
      storageClassName: generic
      accessModes:
      - ReadWriteMany
      resources:
        requests:
          storage: 500Mi

</code></pre>
<p><strong>deployment.yaml</strong></p>
<pre><code>    # Deployment
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      namespace: staging
      labels:
        app: postgres-db
      name: postgres-db
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: postgres-db
      template:
        metadata:
          labels:
            app: postgres-db
        spec:
          containers:
            - name: postgres-db
              image: postgres:12.4
              ports:
                - containerPort: 5432
              volumeMounts:
                - mountPath: /var/lib/postgresql/data
                  name: postgres-db
              env:
                - name: POSTGRES_USER
                  valueFrom:
                    secretKeyRef:
                      name: postgres-secrets
                      key: MYAPPAPI_DATABASE_USERNAME

                - name: POSTGRES_DB
                  valueFrom:
                    secretKeyRef:
                      name: postgres-secrets
                      key: MYAPPAPI_DATABASE_NAME

                - name: POSTGRES_PASSWORD
                  valueFrom:
                    secretKeyRef:
                      name: postgres-secrets
                      key: MYAPPAPI_DATABASE_PASSWORD
          volumes:
            - name: postgres-db
              persistentVolumeClaim:
                claimName: db-data-pvc

</code></pre>
<p><strong>svc.yaml</strong></p>
<pre><code>    apiVersion: v1
    kind: Service
    metadata:
      namespace: staging
      labels:
        app: postgres-db
      name: postgresdb-service
    spec:
      type: ClusterIP
      selector:
        app: postgres-db
      ports:
        - port: 5432
</code></pre>
<p>and it seems that everything is working</p>
<p>Then I execute <code>kubectl port-forward postgres-db-podname 5433:5432 -n staging</code> and when I try to connect it throws</p>
<p><code>FATAL:  role &quot;myappuserdb&quot; does not exist</code></p>
<hr />
<p>UPDATE 1</p>
<p>This is from GKE YAML</p>
<pre><code>spec:
      containers:
      - env:
        - name: POSTGRES_DB
          valueFrom:
            secretKeyRef:
              key: MYAPPAPI_DATABASE_NAME
              name: postgres-secrets
        - name: POSTGRES_USER
          valueFrom:
            secretKeyRef:
              key: MYAPPAPI_DATABASE_USERNAME
              name: postgres-secrets
        - name: POSTGRES_PASSWORD
          valueFrom:
            secretKeyRef:
              key: MYAPPAPI_DATABASE_PASSWORD
              name: postgres-secrets
</code></pre>
<hr />
<p>UPDATE 2</p>
<p>I will explain what happened and how I solve this.</p>
<p>The first time I applied the files, <code>kubectl apply -f k8s/</code>, in the deployment, the environment variable POSTGRES_USER was referencing a wrong secret, MYAPPAPI_DATABASE_NAME and it should make reference to MYAPPAPI_DATABASE_USERNAME.</p>
<p>After this first time, everytime I did <code>kubectl delete -f k8s/</code> the resources were deleted. However, when I created the resources again, the data that I created in the previous step was not cleaned.</p>
<p>I deleted the cluster and created a new cluster and everything worked. I need to check if there is a way to clean the data in kubernetes volume.</p>

agusgambina
<p>in your deployment's <code>env</code> spec you have assigned the wrong value for <code>POSTGRES_USER</code>. you have assigned the value <code>POSTGRES_USER = MYAPPAPI_DATABASE_NAME</code>.</p>
<p>but i think it should  be <code>POSTGRES_USER = MYAPPAPI_DATABASE_USERNAME</code> .</p>
<pre><code>              env:
                - name: POSTGRES_USER
                  valueFrom:
                    secretKeyRef:
                      name: postgres-secrets
                      key: MYAPPAPI_DATABASE_NAME #&lt;&lt;&lt;this is the value need to change&gt;&gt;&gt;
</code></pre>
<p>please try this one</p>
<pre><code>              env:
                - name: POSTGRES_USER
                  valueFrom:
                    secretKeyRef:
                      name: postgres-secrets
                      key: MYAPPAPI_DATABASE_USERNAME
</code></pre>
