Kubernetes
Minikube
Local Development
Postgres
Database Connection

During local development with Kubernetes/minikube, how should I connect to postgres database running on localhost?

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Introduction

A pod running in Minikube cannot treat localhost as your host machine. Inside the pod, localhost means the pod itself. So if PostgreSQL is running on your developer machine and your application runs in Minikube, you need a host-reachable address or a different development topology. The best answer usually depends on whether you want convenience, fidelity to production, or clean environment isolation.

Why localhost Does Not Work from the Pod

Kubernetes pods have their own network namespaces. When code inside a pod connects to 127.0.0.1 or localhost, it is targeting the pod container network, not the laptop or workstation that runs Minikube.

That is the core networking fact behind the problem. Once you accept that, the solution becomes a routing question rather than a PostgreSQL question.

Option 1: Use the Host Alias Exposed by Minikube

In many local setups, Minikube exposes a stable hostname for reaching the host machine from inside the cluster. A common one is:

text
host.minikube.internal

If PostgreSQL is listening on your host and accessible beyond 127.0.0.1, your application can connect using that host alias instead of localhost.

A sample connection string might be:

text
postgresql://appuser:[email protected]:5432/appdb

This is often the simplest local-development answer.

Make Sure PostgreSQL Actually Listens for External Host Access

Even if Minikube can reach the host, PostgreSQL itself must be configured to accept connections from outside the host loopback interface.

That typically means checking:

  • 'listen_addresses in postgresql.conf'
  • host rules in pg_hba.conf
  • local firewall rules

If PostgreSQL only listens on 127.0.0.1, the cluster still will not be able to reach it even with the correct hostname.

Option 2: Run PostgreSQL Inside Kubernetes for Development

A more Kubernetes-native development approach is to run PostgreSQL inside the cluster and connect to it through a normal Service.

yaml
1apiVersion: v1
2kind: Service
3metadata:
4  name: postgres
5spec:
6  selector:
7    app: postgres
8  ports:
9    - port: 5432
10      targetPort: 5432

Now your app can use the service DNS name:

text
postgresql://appuser:secret@postgres:5432/appdb

This is often a better approximation of production-style networking and avoids special host-routing rules.

Which Option Is Better?

A useful rule of thumb is:

  • use the host alias when you want fast local reuse of an existing database
  • run PostgreSQL in Minikube when you want environment consistency and fewer host-specific exceptions

There is no universal winner. The better choice depends on whether your priority is convenience or reproducibility.

Avoid Hard-Coding One Environment's Answer

Your app should not bake the development database host directly into the code. Use environment variables or Kubernetes config instead.

For example, in a Deployment:

yaml
1env:
2  - name: DB_HOST
3    value: host.minikube.internal
4  - name: DB_PORT
5    value: "5432"

This makes it easy to switch between:

  • host-based local PostgreSQL
  • in-cluster PostgreSQL
  • a shared development database

without rewriting application code.

Debug Connectivity from Inside the Pod

If the connection still fails, test from inside a running pod:

bash
kubectl exec -it <pod-name> -- sh

Then try to reach the database host and port with tools available in the image, or add a temporary diagnostic image if needed.

This helps you determine whether the failure is:

  • DNS or routing
  • PostgreSQL listener configuration
  • authentication
  • firewall

That separation is much faster than changing random settings blindly.

Common Pitfalls

The most common mistake is using localhost in the pod and expecting it to mean the developer machine. It never does.

Another mistake is using host.minikube.internal while PostgreSQL still listens only on the host loopback interface.

Developers also hard-code one development hostname in the application instead of externalizing database configuration.

Summary

  • From a pod, localhost refers to the pod itself, not the machine running Minikube.
  • A common local-development solution is to connect to PostgreSQL through host.minikube.internal.
  • PostgreSQL must be configured to listen on an address reachable from Minikube, not only on 127.0.0.1.
  • Running PostgreSQL inside the cluster is often cleaner when you want a more Kubernetes-like setup.
  • Keep the database host configurable so local and cluster-based development can use different connection targets.

Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design