Connecting to a Localhost PostgreSQL Database During Local Development with Kubernetes/Minikube

Connecting to a Localhost PostgreSQL Database During Local Development with Kubernetes/Minikube
In this blog post, we’ll guide you through the process of connecting to a PostgreSQL database running on localhost during local development with Kubernetes and Minikube. This is a common scenario for data scientists who want to leverage the power of Kubernetes for managing their applications while still using a local PostgreSQL database for development purposes.
Prerequisites
Before we start, make sure you have the following installed on your machine:
Step 1: Start Your Local PostgreSQL Database
First, start your PostgreSQL database on localhost. If you’re using PostgreSQL installed on your machine, you can start it with the following command:
pg_ctl -D /usr/local/var/postgres start
Ensure that your PostgreSQL database is running and accessible on localhost:5432
(or whichever port you’ve configured).
Step 2: Start Minikube
Next, start Minikube with the minikube start
command. This will start a local Kubernetes cluster on your machine.
minikube start
Step 3: Enable Minikube Tunnel
To allow Kubernetes to access services running on your localhost, you need to create a tunnel. Minikube provides a command for this: minikube tunnel
.
minikube tunnel
This command will ask for your system password because it modifies your network settings to create a tunnel.
Step 4: Create a Service for Your PostgreSQL Database
Now, you need to create a service in Kubernetes that points to your PostgreSQL database running on localhost. To do this, create a file named postgres-service.yaml
with the following content:
apiVersion: v1
kind: Service
metadata:
name: postgres
spec:
type: ClusterIP
ports:
- port: 5432
targetPort: 5432
---
apiVersion: v1
kind: Endpoints
metadata:
name: postgres
subsets:
- addresses:
- ip: 'host.minikube.internal'
ports:
- port: 5432
This YAML file defines a service named postgres
and an endpoint that points to host.minikube.internal
, which is a special DNS entry created by Minikube to refer to your localhost.
Apply this configuration with the kubectl apply
command:
kubectl apply -f postgres-service.yaml
Step 5: Connect to Your PostgreSQL Database
Finally, you can connect to your PostgreSQL database from any pod in your Kubernetes cluster using the hostname postgres
and port 5432
.
Here’s an example of how to do this in Python using the psycopg2
library:
import psycopg2
conn = psycopg2.connect(
host="postgres",
port=5432,
database="your_database",
user="your_username",
password="your_password"
)
Conclusion
In this blog post, we’ve shown you how to connect to a PostgreSQL database running on localhost during local development with Kubernetes and Minikube. This setup allows you to leverage the power of Kubernetes while still using a local database for development purposes.
Remember, this setup is intended for development purposes only. For production environments, you should consider using a managed PostgreSQL service or running PostgreSQL in a Kubernetes pod.
We hope this guide has been helpful. If you have any questions or run into any issues, feel free to leave a comment below.
Keywords
- Kubernetes
- Minikube
- PostgreSQL
- Localhost
- Local Development
- Database Connection
- Data Science
- Kubernetes Service
- Minikube Tunnel
- PostgreSQL Service
About Saturn Cloud
Saturn Cloud is your all-in-one solution for data science & ML development, deployment, and data pipelines in the cloud. Spin up a notebook with 4TB of RAM, add a GPU, connect to a distributed cluster of workers, and more. Join today and get 150 hours of free compute per month.