<p>I have setup a Postgres pod on my Kubernetes cluster, and I am trying to troubleshoot it a bit.</p>
<p>I would like to use the <a href="https://hub.docker.com/_/postgres" rel="nofollow noreferrer">official Postgres image</a> and deploy it to my Kubernetes cluster using <code>kubectl</code>. Given that my Postgres server connection details are:</p>
<pre><code>host: mypostgres
port: 5432
username: postgres
password: 12345
</code></pre>
<p>And given that I <em>think</em> the command will be <em>something</em> like:</p>
<pre><code>kubectl run -i --tty --rm debug --image=postgres --restart=Never -- sh
</code></pre>
<p>What do I need to do so that I can deploy this image to my cluster, connect to my Postgres server and start running SQL command against it (for troubleshooting purposes)?</p>

<p>If your primarily interested in troubleshooting, then you're probably looking for the <code>kubectl port-forward</code> command, which will expose a container port on your local host.  First, you'll need to deploy the Postgres pod; you haven't shown what your pod manifest looks like, so I'm going to assume a <code>Deployment</code> like this:</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: postgres
  name: postgres
  namespace: sandbox
spec:
  replicas: 1
  selector:
    matchLabels:
      app: postgres
  template:
    metadata:
      labels:
        app: postgres
    spec:
      containers:
      - env:
        - name: POSTGRES_PASSWORD
          value: secret
        - name: POSTGRES_USER
          value: example
        - name: POSTGRES_DB
          value: example
        image: docker.io/postgres:13
        name: postgres
        ports:
        - containerPort: 5432
          name: postgres
          protocol: TCP
        volumeMounts:
        - mountPath: /var/lib/postgresql
          name: postgres-data
      strategy: Recreate
      volumes:
      - emptyDir: {}
        name: postgres-data
</code></pre>
<p>Once this is running, you can access postgres with the <code>port-forward</code>
command like this:</p>
<pre><code>kubectl -n sandbox port-forward deploy/postgres 5432:5432
</code></pre>
<p>This should result in:</p>
<pre><code>Forwarding from 127.0.0.1:5432 -&gt; 5432
Forwarding from [::1]:5432 -&gt; 5432
</code></pre>
<p>And now we can connect to Postgres using <code>psql</code> and run queries
against it:</p>
<pre><code>$ psql -h localhost -U example example
psql (13.4)
Type &quot;help&quot; for help.

example=#
</code></pre>
<hr />
<p><code>kubectl port-forward</code> is only useful as a troubleshooting mechanism. If
you were trying to access your <code>postgres</code> pod from another pod, you
would create a <code>Service</code> and then use the service name as the hostname
for your client connections.</p>
<hr />
<p><strong>Update</strong></p>
<p>If your goal is to deploy a <em>client</em> container so that you can log
into it and run <code>psql</code>, the easiest solution is just to <code>kubectl rsh</code>
into the postgres container itself. Assuming you were using the
<code>Deployment</code> shown earlier in this question, you could run:</p>
<pre><code>kubectl rsh deploy/postgres
</code></pre>
<p>This would get you a shell prompt <em>inside</em> the postgres container. You
can run <code>psql</code> and not have to worry about authentication:</p>
<pre><code>$ kubectl rsh deploy/postgres
$ psql -U example example
psql (13.4 (Debian 13.4-1.pgdg100+1))
Type &quot;help&quot; for help.

example=#
</code></pre>
<p>If you want to start up a separate container, you can use the <code>kubectl debug</code> command:</p>
<pre><code>kubectl debug deploy/postgres
</code></pre>
<p>This gets you a root prompt in a debug pod. If you know the ip address
of the postgres pod, you can connect to  it using <code>psql</code>. To get
the address of the pod, run this on your local host:</p>
<pre><code>$ kubectl get pod/postgres-6df4c549f-p2892 -o jsonpath='{.status.podIP}'
10.130.0.11
</code></pre>
<p>And then inside the debug container:</p>
<pre><code>root@postgres-debug:/# psql -h 10.130.0.11 -U example example
</code></pre>
<p>In this case you would have to provide an appropriate password,
because you are accessing postgres from &quot;another machine&quot;, rather than
running directly inside the postgres pod.</p>
<p>Note that in the above answer I've used the shortcut
<code>deploy/&lt;deployment_name</code>, which avoids having to know the name of the
pod created by the <code>Deployment</code>. You can replace that with
<code>pod/&lt;podname&gt;</code> in all cases.</p>
