<p>I want to simply login to a postgres db from outside my K8 cluster. I'm created the following config:</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: apps/v1
kind: Deployment
metadata:
    name: postgres 
spec:
    replicas: 1
    selector: 
        matchLabels:
            app: postgres 
    template:
        metadata:
            labels:
                app: postgres 
        spec:
            volumes:
                - name: postgres-storage
                  persistentVolumeClaim: 
                    claimName: database-persistent-volume-claim
            containers:
                - name: postgres 
                  image: postgres
                  volumeMounts:
                    - name: postgres-storage
                      mountPath: /var/lib/postgresql/data
                      subPath: postgres
                  env:
                    - name: POSTGRES_USER
                      value: postgres
                    - name: POSTGRES_PORT
                      value: '5432'
                    - name: POSTGRES_DB
                      value: postgres
                    - name: POSTGRES_PASSWORD
                      value: password
                    - name: POSTGRES_HOST_AUTH_METHOD
                      value: trust
---
apiVersion: v1
kind: Service
metadata:
    name: postgres-srv
spec:
    selector:
        app: postgres
    type: NodePort
    ports:
        - name: postgres
          protocol: TCP
          port: 5432
          targetPort: 5432
</code></pre>
<p>Config Map:</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: v1
kind: ConfigMap
metadata:
  name: tcp-services
  namespace: ingress-nginx
data:
  5432: &quot;default/postgres-srv:5432&quot;
</code></pre>
<p>I've checked <code>kubectl get services</code> and attempted to use the endpoint and the cluster-ip. Neith of these worked.</p>
<p><code>psql &quot;postgresql://postgres:password@[ip]:5432/postgres&quot;</code></p>
<p>The pod is running and the logs say everything is ready. Anything I'm missing here? I'm running the cluster in digital ocean.</p>
<p>edit:</p>
<p>I want to be able to access the DB from my host. (sub.domain.com) I've bounced the deployments and still can't get in. The only config I've targeted is what is shown above. I do have an A record for my domain and can access my other exposed pods via my ingress nginx service</p>

Ryan
<p>You can <a href="https://kubernetes.github.io/ingress-nginx/user-guide/exposing-tcp-udp-services/" rel="nofollow noreferrer">expose TCP and UDP services</a> with <code>ingress-nginx</code> configuration.</p>
<p>For example using <a href="https://cloud.google.com/kubernetes-engine" rel="nofollow noreferrer">GKE</a> with <a href="https://kubernetes.github.io/ingress-nginx/" rel="nofollow noreferrer"><code>ingress-nginx</code></a>, <a href="https://github.com/helm/charts/tree/master/stable/nfs-server-provisioner" rel="nofollow noreferrer">nfs-server-provisioner</a> and the <a href="https://bitnami.com/stack/postgresql/helm" rel="nofollow noreferrer"><code>bitnami/postgresql</code></a> <a href="https://helm.sh/" rel="nofollow noreferrer"><code>helm</code></a> charts:</p>
<pre class="lang-sh prettyprint-override"><code>kubectl create secret generic -n default postgresql \
    --from-literal=postgresql-password=$(openssl rand -base64 32) \
    --from-literal=postgresql-replication-password=$(openssl rand -base64 32)
helm repo add bitnami https://charts.bitnami.com/bitnami
helm install -n default postgres bitnami/postgresql \
    --set global.storageClass=nfs-client \
    --set existingSecret=postgresql
</code></pre>
<p>Patch the <code>ingress-nginx</code> <code>tcp-services</code> <a href="https://kubernetes.io/docs/concepts/configuration/configmap/" rel="nofollow noreferrer"><code>ConfigMap</code></a>:</p>
<pre><code>kubectl patch cm -n ingress-nginx tcp-services -p '{&quot;data&quot;: {&quot;5432&quot;: &quot;default/postgres-postgresql:5432&quot;}}'
</code></pre>
<p>Update the controllers <code>Service</code> for the proxied port (i.e. <code>kubectl edit svc -n ingress-nginx ingress-nginx</code>):</p>
<pre class="lang-yaml prettyprint-override"><code>  - name: postgres
    port: 5432
    protocol: TCP
    targetPort: 5432
</code></pre>
<p><strong>Note</strong>: you may have to update the existing <code>ingress-nginx</code> controller deployments <code>args</code> (i.e. <code>kubectl edit deployments.apps -n ingress-nginx nginx-ingress-controller</code>) to include <code>--tcp-services-configmap=ingress-nginx/tcp-services</code> and bounce the <code>ingress-nginx</code> controller if you edit the deployment spec (i.e. <code>kubectl scale deployment -n ingress-nginx --replicas=0 &amp;&amp; kubectl scale deployment -n ingress-nginx --replicas=3</code>).</p>
<p>Test the connection:</p>
<pre class="lang-sh prettyprint-override"><code>export PGPASSWORD=$(kubectl get secrets -n default postgresql -o jsonpath={.data.postgresql-password} |base64 -d)
docker run --rm -it \
    -e PGPASSWORD=${PGPASSWORD} \
    --entrypoint psql \
    --network host \
    postgres:13-alpine -U postgres -d postgres -h example.com
</code></pre>
<p><a href="https://i.stack.imgur.com/RiAJW.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/RiAJW.png" alt="Connection to remote PostgreSQL database using dns resolution and ingress-nginx proxied traffic" /></a></p>
<p><strong>Note</strong>: I manually created an <code>A</code> record in <a href="https://cloud.google.com/dns" rel="nofollow noreferrer">Google Cloud DNS</a> to resolve the hostname to the clusters external IP.</p>
<p><strong>Update</strong>: in addition to creating the <code>ingress-nginx</code> config, installing the <code>bitnami/postgresql</code> chart etc. it was necessary to Disable &quot;Proxy Protocol&quot; on the Load Balancer to get the connections working for a deployment in DigitalOcean (<code>postgres</code> will <code>LOG:  invalid length of startup packet</code> otherwise):</p>
<p><a href="https://i.stack.imgur.com/UOEN1.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/UOEN1.png" alt="Digital Ocean postgres connection through public load balancer" /></a></p>
