<p>I got my own cluster which has a control-plane and worker. i trying to connect to PostgreSQL in the cluster using <code>psql -h &lt;ingress-url&gt; -p 5432 -U postgres -W</code>, but it occur an error:</p>
<pre><code>psql: error: server closed the connection unexpectedly
        This probably means the server terminated abnormally
        before or while processing the request.
</code></pre>
<p>But <code>curl &lt;ingress-url&gt;</code> response like this:</p>
<pre><code>2022-05-27 04:00:50.406 UTC [208] LOG:  invalid length of startup packet
</code></pre>
<p>That response mean my request have reached to PostgreSQL Server, but why i cannot connect to my PostgreSQL Server?</p>
<p>Here are my resources:</p>
<p>Ingress:</p>
<pre><code>apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: preflight
  labels:
    helm.sh/chart: &quot;preflight&quot;
    helm.sh/version: &quot;0.1.0&quot;
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
    nginx.ingress.kubernetes.io/ssl-redirect: &quot;false&quot;
spec:
  ingressClassName: nginx
  rules:
  - http:
      paths:
      - path: /
        pathType: Exact
        backend:
          service:
            name: preflight
            port:
              number: 5432
</code></pre>
<p>Deployment:</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: preflight
  labels:
    helm.sh/chart: &quot;preflight&quot;
    helm.sh/version: &quot;0.1.0&quot;
spec:
  replicas: 1
  selector:
    matchLabels:
      helm.sh/chart: &quot;preflight&quot;
      helm.sh/version: &quot;0.1.0&quot;
  template:
    metadata:
      labels:
        helm.sh/chart: &quot;preflight&quot;
        helm.sh/version: &quot;0.1.0&quot;
    spec:
      containers:
      - name: preflight
        image: postgres:14
        env:
        - name: POSTGRES_PASSWORD
          valueFrom:
            configMapKeyRef:
              name: preflight
              key: postgresPassword
        ports:
        - containerPort: 5432
</code></pre>
<p>Service:</p>
<pre><code>apiVersion: v1
kind: Service
metadata:
  name: preflight
  labels:
    helm.sh/chart: &quot;preflight&quot;
    helm.sh/version: &quot;0.1.0&quot;
spec:
  type: ClusterIP
  selector:
    helm.sh/chart: &quot;preflight&quot;
    helm.sh/version: &quot;0.1.0&quot;
  ports:
  - port: 5432
    targetPort: 5432
</code></pre>
<p>ConfigMap values is a <code>POSTGRES_PASSWORD=postgres</code>.</p>

<p>Nginx ingress controller is an HTTP proxy. You are trying to route PGSQL traffic over HTTP, and that simply can't work.</p>
<p>What you need to do is expose a TCP service through nginx ingress controller. See <a href="https://kubernetes.github.io/ingress-nginx/user-guide/exposing-tcp-udp-services/" rel="nofollow noreferrer">this page</a>.</p>
<p>In a nutshell, you need to create a configmap like this:</p>
<pre><code>apiVersion: v1
kind: ConfigMap
metadata:
  name: tcp-services
  namespace: &lt;namespace where you deployed ingress controller&gt;
data:
  5432: &quot;default/preflight:5432&quot;
</code></pre>
<p>then ensure your nginx ingress controller starts with the <code>--tcp-services-configmap=tcp-services</code> flag.</p>
<p>Finally, ensure the nginx ingress controller Service (the one with type == <code>LoadBalancer</code>) exposes port 5432:</p>
<pre><code>apiVersion: v1
kind: Service
metadata:
  name: ingress-nginx
...
spec:
  type: LoadBalancer
  ports:
    ...
    - name: pgsql
      port: 5432
      targetPort: 5432
      protocol: TCP
...
</code></pre>
<p>Please note that your provider's Load Balancer should support TCP backends (most should do, but worth mentioning).</p>
