Port forwarding maps a local port to a port on a remote Kubernetes resource, letting localhost connect to a cluster resource without exposing the database publicly. section 8.4.2
Use kubectl to forward traffic to different Kubernetes targets:
- Pod: kubectl port-forward postgres-0 3312:5432 section 8.4.2
- StatefulSet: kubectl port-forward statefulsets/postgres 3312:5432; this selects the first Pod. section 8.4.2
- Service: kubectl port-forward service/postgres 3312:5432. section 8.4.2
Connecting to PostgreSQL locally
After forwarding local port 3312 to PostgreSQL port 5432, connect with psql -h localhost -U postgres -d postgres-db -p 3312. Port forwarding verifies access from the local machine, not container-to-container communication inside the cluster. section 8.4.2




