kpg is a narrow, command-first CLI for connecting to Kubernetes-hosted Postgres databases.
It discovers supported Postgres operator resources, reads the generated application credentials, opens a local port-forward to the read-write service, and exposes PG* environment variables for psql, pgcli, or any Postgres-compatible client.
kpg list
kpg list --output json
kpg connect
kpg connect <cluster|namespace/cluster|substring>
kpg connect <provider>:<namespace>/<cluster>
kpg connect <cluster|namespace/cluster|substring> -- psql
kpg connect <cluster|namespace/cluster|substring> --output shell
kpg connect <cluster|namespace/cluster|substring> --user <user> --database <db>
kpg last
kpg last --user <user> --database <db>
kpg last -- psql
kpg version
kpg completion <bash|zsh|fish|powershell>Shared flags:
-c, --context <name> kube context override
-n, --namespace <name> restrict discovery/lookup to one namespace
-p, --local-port <port> use a fixed local port
-o, --output shell|dotenv|json
--request-timeout <duration> timeout for Kubernetes API calls, for example 30s; off by
default so interactive credential plugins are not cut short;
the port-forward itself is never limited
-h, --helpHomebrew on macOS or Linux:
brew install pscheid92/tap/kpgWith Go 1.27 or newer:
go install github.com/pscheid92/kpg@latestPrebuilt binaries for Linux, macOS, and Windows are attached to GitHub Releases.
Requirements:
- A kubeconfig with access to the target cluster
- CloudNativePG or Zalando Postgres Operator resources in the active context
psql,pgcli, or another Postgres client when using command mode
On Windows, --output and command mode work as on other platforms. The subshell
started by a plain kpg connect is SHELL if set, otherwise pwsh, Windows
PowerShell, or COMSPEC (cmd.exe), whichever is found first.
Release builds set kpg version from the tag, commit, and commit date, and are
reproducible from the tag. go install builds report the module version and
the commit recorded by the Go toolchain. Releases are created from tags named
v*, for example:
git tag v0.1.0
git push origin v0.1.0Each release ships, per archive, a pqsign
signature (.pqsig) and an SPDX SBOM (.sbom.json), plus a signed
checksums.txt and a GitHub build provenance attestation. Verify an archive with
the release public key that signed it:
| Releases | Public key | Key ID |
|---|---|---|
| v0.3.0 and later | release.key.pub |
7F732E590ED14C12 |
| v0.1.0 to v0.2.1 | release-keys/kpg-D46757F2C0A16369.pub |
D46757F2C0A16369 |
pqsign verify kpg_0.3.0_linux_amd64.tar.gz -p release.key.pubThe key changed on 2026-10-09 because the secret half of the previous key was lost; it was not compromised. Nothing signed the new key with the old one, so check the key ID against the table above and against the v0.3.0 release notes.
and its provenance with:
gh attestation verify kpg_0.1.0_linux_amd64.tar.gz --owner pscheid92The release workflow expects these GitHub repository secrets:
PQSIGN_SECRET_KEY: base64-encoded encrypted pqsign secret key file matchingrelease.key.pub(key ID7F732E590ED14C12)PQSIGN_PASSWORD: password for that secret keyHOMEBREW_TAP_DEPLOY_KEY(optional): private SSH key whose public half is a write-enabled deploy key onpscheid92/homebrew-tap; without it the Homebrew cask is only rendered intodist/
In an interactive terminal, kpg connect opens a subshell with PG* environment values already exported. Exiting that shell closes the tunnel:
kpg connect app-db
psql
exitWhen run in an interactive terminal with no target, kpg connect opens a searchable target picker before starting the subshell. Type to filter, use the arrow keys or ctrl+j/ctrl+k to move, press Enter to connect, or Esc to cancel. In non-interactive use, pass the target explicitly.
If a command is provided after --, kpg injects the same PG* environment values into that command and stops the tunnel when the command exits. kpg exits with the command's exit code:
kpg connect app-db -- psql
kpg connect app-db -- pgcliCtrl-C inside the subshell or the command is handled by that program, for
example psql cancels the running query and a shell prints a new prompt. kpg
keeps the tunnel open until the program exits. Sending kpg a SIGTERM
forwards it to the program.
The program also receives KPG_TARGET (namespace/cluster) and
KPG_PROVIDER, for example to show the connection in a shell prompt. kpg
removes PGSERVICE, PGSERVICEFILE, and PGHOSTADDR from the program's
environment because libpq would let them override the tunnel.
kpg keeps the local port itself and follows the primary through a
failover or switchover, for every client that connects to it, not only psql:
- Connections that are already open stay on the pod they started on until
the server ends them. During a CloudNativePG failover that happens when the
old primary shuts down; an open session delays that by up to the cluster's
smartShutdownTimeout. - New connections always go to the pod that is currently the ready primary
behind the read-write service. When it changed,
kpgopens a new port-forward for them and says so on stderr. - While no primary is ready, new connections are held open for up to 60
seconds and connected as soon as one is. Clients see a slow connect instead
of an error, so their own reconnect logic succeeds:
psqlreportsAttempting reset: Succeeded, and applications and connection pools get a working connection on their next attempt.
A client with a connect timeout shorter than the failover, for example
PGCONNECT_TIMEOUT or a JDBC connectTimeout, gives up before the primary
is back and has to reconnect afterwards. If no primary becomes ready within
60 seconds, kpg closes the waiting connection and prints a psql command to
reconnect with once the cluster is healthy, for example:
reconnect once the cluster is healthy; in psql run: \c "host=127.0.0.1 port=51664 dbname=secretli user=secretli"
kpg suppresses client-go's internal log lines so they do not interrupt an
interactive session. Set KPG_DEBUG=1 to see them.
Use --output to print connection values instead of entering a subshell or running a command. The values are printed once the tunnel accepts connections, and kpg connect then keeps the tunnel alive in the foreground until Ctrl-C:
export PGHOST=127.0.0.1
export PGPORT=15432
export PGUSER=app
export PGPASSWORD=secret
export PGDATABASE=app
export PGSSLMODE=disablekpg connect app-db --output shell
kpg connect app-db --output dotenv
kpg connect app-db --output jsonUse --user and --database to override the defaults discovered from the
operator resource and generated credentials:
kpg connect app-db --user reporting_user --database reports
kpg last --user reporting_user --database reportsIf no credentials secret exists for the selected user, kpg warns on stderr
and leaves PGPASSWORD unset so the client prompts for a password.
Targets can be written as a cluster name, namespace/cluster, or a unique substring match. If multiple providers expose the same namespace and cluster, use a provider-qualified target:
kpg connect cnpg:app/app-db
kpg connect zalando:postgres/acid-mainAmbiguous matches fail with candidate suggestions. kpg list prints provider information when it is needed to distinguish targets, and says so on stderr when nothing was found. kpg list --output json emits script-friendly target metadata without secrets.
The last successful target is stored as soon as the tunnel is ready, at the XDG state path, normally:
~/.local/state/kpg/last.json
Only the provider, namespace, and cluster are stored. Discovery data and secrets are not cached.
CloudNativePG and Zalando Postgres Operator are supported.
kpg uses Kubernetes API discovery to detect which supported provider
resources are registered, then lists only those resources. It does not list
CustomResourceDefinition objects. By default, discovery tries to list
supported resources across namespaces. If Kubernetes denies an all-namespace
provider list, kpg retries that provider in the current kube context
namespace. Use --namespace <name> to make discovery strictly namespaced, or
pass a namespace-qualified target such as app/app-db.
CloudNativePG:
postgresql.cnpg.io/v1 Cluster
For each cluster:
RW service: status.writeService, normally <cluster>-rw
App secret: spec.bootstrap.<method>.secret.name, normally <cluster>-app
The application database and its owner come from the bootstrap section the
cluster uses, initdb, recovery, or pg_basebackup, with the operator's
defaults (app owned by app) when they are not set. The generated app secret
overrides both when it is readable. Additional users for --user:
postgreswhenspec.enableSuperuserAccessis true, read fromspec.superuserSecret.nameor<cluster>-superuser- declarative roles under
spec.managed.rolesthat can log in and have apasswordSecret; these also appear in the interactive user picker
Any other --user is looked up in <cluster>-<user>.
Zalando Postgres Operator:
acid.zalan.do/v1 postgresql
For each Zalando cluster:
RW service: <cluster>
User secret: <username>.<cluster>.credentials.postgresql.acid.zalan.do
The first database in spec.databases, sorted by name, is used as the default database. Its owner is used as the default user. If spec.databases is empty, the first prepared database in spec.preparedDatabases, sorted by name, is used. If no database is available, the first user in spec.users, sorted by name, is used. Cross-namespace user notation like appspace.db_user is supported for the documented default secret naming convention. When --database selects a database with a known owner and --user is not set, that owner is used as the default user.
kpg needs these Kubernetes permissions in the namespaces it works with:
| Resource | Verbs | Used for |
|---|---|---|
clusters.postgresql.cnpg.io |
list | discovery |
postgresqls.acid.zalan.do |
list | discovery |
secrets |
get | credentials; kpg list shows spec values when denied |
secrets |
list | Zalando user discovery; skipped when denied |
services |
get | locating the read-write service |
pods |
list, get | choosing the pod to forward to |
endpointslices.discovery.k8s.io |
list | services without a selector |
pods/portforward |
create | the tunnel |
namespaces |
list | -n shell completion only |
API discovery (GET /apis/<group>/<version>) is available to every
authenticated user by default. A provider whose list is forbidden is skipped
when another provider works; when every provider is forbidden, kpg says so
and suggests -n.
just check
just coverage
just coverage-html
just fix
just vuln
just cross
just acceptance app/app-db
just release-snapshot
just release-snapshot-signedThe CLI is built with Cobra. Kubernetes access uses client-go: dynamic clients for provider CRDs, typed core clients for Secrets/Services/Pods/Namespaces, and client-go port-forwarding (WebSocket with SPDY fallback, like kubectl) against the selected pod behind each provider's read-write service. Provider rules live in separate files under internal/kube.
just check runs tests, go vet, golangci-lint (v2.14.0, the release CI
pins; building v2.13.x from source with go run fails to read Go 1.27
packages), and go fix -diff.
The interactive pickers can be exercised on a real terminal with the
manual build tag: go test -c -tags manual -o picker.test ./internal/kpg/
and then ./picker.test -test.run TestManualTargetPicker.
just acceptance requires a working kube context, access to the target cluster, and psql on PATH.
just release-snapshot requires GoReleaser and writes unsigned local release artifacts to dist/.
just release-snapshot-signed also requires pqsign and expects PQSIGN_SECRET_KEY_PATH plus PQSIGN_PASSWORD.
CI runs the same checks on Linux, the test suite with the race detector on
Linux and macOS, a cross-compile of every release target, and govulncheck.
Release notes are grouped from commit prefixes: feat: and fix: get their
own sections, docs:, test:, chore:, and ci: are left out.
The Homebrew cask installs bash, zsh and fish completions automatically. For other installs, shell completion is provided by Cobra:
kpg completion zsh
kpg completion bash
kpg completion fishCompletions include:
kpg -c <TAB>for kubeconfig contextskpg -n <TAB>for namespaces in the selected contextkpg connect <TAB>for current Postgres targets
Namespace filtering is honored, for example kpg connect -n app <TAB>.
For a one-off zsh session:
source <(kpg completion zsh)For persistent zsh completion:
just install-completion-zshThen ensure your ~/.zshrc has the completion directory in fpath before compinit:
fpath=("$HOME/.zsh/completions" $fpath)
autoload -Uz compinit
compinitOpen a new shell, or run:
exec zshMIT