PostgreSQL objects as Kubernetes custom resources — declared once, kept in sync forever.
Getting started · Resource reference · Architecture · Full docs
kubectl apply a Database, a Role, a Schema, a Grant — kubepg turns
each into real PostgreSQL DDL and then keeps watching. Someone drops a
grant by hand at 2am? The next reconcile puts it back. That's the whole
pitch: the same ground cyrilgdn/terraform-provider-postgresql covers, but
as a live controller instead of a one-shot apply.
apiVersion: kubepg.dvrkn.com/v1
kind: Database
metadata:
name: appdb
spec:
instanceRef: { name: prod }
owner: appkubectl apply -f appdb.yaml
kubectl get database appdb
# NAME INSTANCE READY AGE
# appdb prod True 4s| Terraform provider | kubepg | |
|---|---|---|
| Drift correction | Only on the next apply a human remembers to run |
Automatic, every 5 minutes |
| Where state lives | A .tfstate file you have to protect and share |
The Kubernetes API — kubectl get/describe is the state |
| Who can request a database | Whoever can run terraform apply |
Any team, gated by ordinary Kubernetes RBAC, in its own namespace |
| Failure visibility | A failed CI job | A Ready=False condition with a SQLSTATE code, right on the object |
Instance(cluster-scoped) is the only place connection info lives — host, port, credentials pulled from aSecret, SSL mode. A Postgres server is a cluster-wide fact, so any namespace can reference one by name. Its controller does one thing: probe withSELECT version()and report health.- Everything else —
Database,Role,Schema,Grant, and more — is namespaced, points at oneInstance, and describes one PostgreSQL object. Each reconcile runs idempotentCREATE/ALTER/DROPSQL and self-heals drift on a 5-minute resync. Deleting a CR drops the object through a finalizer (or leaves it alone withdeletionPolicy: Orphan).
Read docs/architecture.md for the full reconcile
loop, the SQL layer (internal/pg), and how Grant maps object types onto
SQL ON <target> clauses.
| Kind | Status | Notes |
|---|---|---|
Instance |
✅ implemented | Connection target + health probe |
Database |
✅ implemented | CREATE/ALTER/DROP DATABASE |
Role |
✅ implemented | CREATE/ALTER/DROP ROLE, password from Secret, memberships |
Schema |
✅ implemented | CREATE/ALTER/DROP SCHEMA |
Grant |
✅ implemented | Revoke-then-grant transaction for exact convergence |
Extension |
🚧 scaffolded | CRD installable; controller pending |
GrantRole |
🚧 scaffolded | |
DefaultPrivileges |
🚧 scaffolded | |
Publication |
🚧 scaffolded | |
Subscription |
🚧 scaffolded | |
Function |
🚧 scaffolded | |
Server (FDW) |
🚧 scaffolded | |
UserMapping |
🚧 scaffolded | |
ReplicationSlot |
🚧 scaffolded | |
PhysicalReplicationSlot |
🚧 scaffolded | |
SecurityLabel |
🚧 scaffolded |
Scaffolded kinds install as CRDs and accept resources, but report
Ready=False, Reason=NotImplemented until their reconcile logic lands —
full field shapes are in docs/resources.md.
make install # install the CRDs into the current kube context
make run # run the controller locally against that context
kubectl apply -k config/samples # apply the samples (edit the Instance host/Secret first)Deploying to a real cluster, Helm chart toggles, and a full worked example
(Instance → Role → Database → Schema → Grant) are in
docs/getting-started.md.
For a throwaway local try-it with k3d:
k3d cluster create pgop
docker build -t kubepg:dev .
k3d image import kubepg:dev -c pgop
helm install kubepg charts/kubepg -n kubepg --create-namespace \
--set controllerManager.container.image.repository=kubepg \
--set controllerManager.container.image.tag=devPasswords and DSNs are supplied only via Secret references, resolved
in-memory, and never written back into a CR's spec/status, events, or logs
— log lines and status messages carry object names and PostgreSQL SQLSTATE
codes, nothing that could embed a credential. Details, RBAC scoping advice,
and the multi-tenancy model are in docs/security.md.
make test # envtest: CRD install + CEL/enum/default validation (no Postgres)
make test-integration # real disposable Postgres via testcontainers, exercises the SQL layer
make test-e2e-pg # envtest + Postgres container + live controllers, full manifest lifecycle
make test-e2e-k3d # deploys the real built image into a k3d cluster, black-boxWhy four separate tiers instead of one suite, and what each one actually proves, is explained in docs/development.md.
- docs/getting-started.md — install, deploy, first resources
- docs/architecture.md — reconcile loop, SQL layer, Grant dispatch
- docs/resources.md — every field on every kind
- docs/security.md — credential flow, RBAC, multi-tenancy
- docs/operations.md — status conditions, drift, deletion policies
- docs/development.md — building, testing, adding a new kind
Kubebuilder ·
controller-runtime ·
jackc/pgx
Apache License 2.0 — see the license header in individual source files.