<p>trying to open Postgres port <code>5432</code> so it can be accessible from my local machine. the port is only open from kubernetes pods.</p>
<pre><code>127.0.0.1:5432 &lt;====&gt; kubernetes-pod &lt;=====&gt; Postgres Server
</code></pre>
<p>basically I want to make port 5432 accessible via my local machine, the port is only accessible from kubernetes-pod. how do I do that.</p>
<p>I tried which I think this would work if postgres server is running on the same pod which is not in my case:</p>
<pre><code>kubectl port-forward pod-5bf5c7df9b-xcjsf 5433:5432
</code></pre>
<p>Note: The PostgreSQL server runs as standalone db server and only the pod can access the port and I want to access the PostgreSQL db port from my local machine .</p>

<p>The easiest and best way to accomplish this is using <code>socat</code> in a pod.</p>
<p>You can use the <a href="https://hub.docker.com/r/alpine/socat" rel="noreferrer"><code>alpine/socat</code> container image</a> to create the pod. Then use port-forward into this pod which will forward the connections to the target db.</p>
<p>Here are the steps:</p>
<ol>
<li>Create a file <code>my-postgresql-socat.yaml</code>:</li>
</ol>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: v1
kind: Pod
metadata:
  name: my-postgresql-socat
  labels:
    name: my-postgresql-socat
spec:
  containers:
  - name: my-postgresql-socat
    image: alpine/socat
    command: [&quot;socat&quot;, &quot;-dd&quot;, &quot;tcp4-listen:5432,fork,reuseaddr&quot;, &quot;tcp4:my-postgresql-host:5432&quot;]
    resources:
      limits:
        memory: &quot;64Mi&quot;
        cpu: &quot;50m&quot;
    ports:
      - containerPort: 5432
</code></pre>
<ol start="2">
<li>Create a pod with</li>
</ol>
<pre class="lang-sh prettyprint-override"><code>kubectl apply -f my-postgresql-socat.yaml
</code></pre>
<ol start="3">
<li>Run a local port-forward:</li>
</ol>
<pre class="lang-sh prettyprint-override"><code>oc port-forward my-postgresql-socat 5432:5432
</code></pre>
<p>You can access your database now via <code>localhost:5432</code></p>
<ol start="4">
<li>When finished, delete the pod:</li>
</ol>
<pre class="lang-sh prettyprint-override"><code>kubectl delete pod my-postgresql-socat
</code></pre>
