- Go 88.4%
- Makefile 9%
- Smarty 1.3%
- Dockerfile 0.9%
- Shell 0.4%
| Filename | Latest commit message | Latest commit date |
|---|---|---|
| .devcontainer | ||
| .github/workflows | ||
| api/v1 | ||
| charts/postgres-operator | ||
| cmd | ||
| config | ||
| hack | ||
| internal/controller | ||
| test | ||
| .dockerignore | ||
| .gitignore | ||
| .golangci.yml | ||
| AGENTS.md | ||
| Dockerfile | ||
| go.mod | ||
| go.sum | ||
| Makefile | ||
| mise.toml | ||
| PROJECT | ||
| README.md | ||
Homelab Postgres Operator
This is a Kubernetes operator for managing PostgreSQL instances, designed for homelab and smaller-scale deployments. It provides a simple way to create and manage PostgreSQL clusters and databases using Custom Resource Definitions (CRDs).
Introduction
The operator introduces two CRDs:
PostgresCluster: Represents a PostgreSQL instance.PostgresDatabase: Represents a database and user within aPostgresCluster.
The main goal of this operator is to simplify the management of PostgreSQL in a Kubernetes environment, especially for applications that need their own dedicated databases and users.
PostgresCluster
A PostgresCluster resource deploys a PostgreSQL instance using a StatefulSet. By default, it uses a pgvector/pgvector:pg16 image, making it suitable for applications that require vector embeddings.
When a PostgresCluster is created, the operator automatically:
- Creates a
StatefulSetto run the PostgreSQL pod. - Creates a headless
Serviceto expose the PostgreSQL instance within the cluster. - Creates a
Secretnamed<cluster-name>-admin-secretcontaining the administrative credentials (host,port,user,password,url).
Example PostgresCluster
apiVersion: postgres.homelab.mortenolsen.pro/v1
kind: PostgresCluster
metadata:
name: my-postgres-cluster
spec:
# Optional: specify a different image
# image: postgres:16
PostgresDatabase
A PostgresDatabase resource creates a new database and a corresponding user with a randomly generated password within a specified PostgresCluster.
When a PostgresDatabase is created, the operator:
- Connects to the referenced
PostgresClusterusing its admin credentials. - Creates a new database.
- Creates a new user with a strong, randomly generated password.
- Grants all privileges on the new database to the new user.
- Creates a
Secretnamed<database-name>-connectionwith the connection details (host,port,database,user,password,url) for the new database.
Example PostgresDatabase
apiVersion: postgres.homelab.mortenolsen.pro/v1
kind: PostgresDatabase
metadata:
name: my-app-database
spec:
clusterRef:
name: my-postgres-cluster # Name of the PostgresCluster
databaseName: my_app_db
userName: my_app_user
reclaimPolicy: Retain # Can be Retain or Delete
Reclaim Policy
The reclaimPolicy field in the PostgresDatabase spec controls what happens when a PostgresDatabase resource is deleted.
-
Retain(default): When thePostgresDatabaseresource is deleted, the underlying database and user in PostgreSQL are not removed. This is a safety measure to prevent accidental data loss. The connection secret is also retained. -
Delete: When thePostgresDatabaseresource is deleted, the underlying database and user in PostgreSQL are deleted. Use this with caution. The connection secret is also deleted.