Skip to content

Databases in Kubernetes

usql opens a database that runs in a Kubernetes cluster from a URL such as k://pg.shop/. usql finds the database and its password, and runs kubectl port-forward to it. A port forward connects a local port to a port of a pod in the cluster. You type no address and no password.

The library dbfwd of usql does this work. This page shows a PostgreSQL server that runs as a Deployment, with a Service named pg, in the namespace shop.

  • kubectl on your PATH.
  • A kubeconfig file that kubectl reads, with access to the namespace of the database. usql uses its current context.
  • The right to read the Secret that holds the password, and to forward a port to the pod.

The URL holds the name of the database in the cluster, a dot, and its namespace:

Terminal window
usql k://pg.shop/
Connected with driver postgres (PostgreSQL 18.7 (Debian 18.7-1.pgdg13+5))
Forwarded to Service pg in context kind-usql-site, namespace shop
Type "help" for help.
pg:app@pg/app=>

The banner names what the forward reached. The prompt shows the name from the URL, pg, in place of the local address and port.

k is the short form of the scheme k8s. The URL has these parts:

k[+TYPE]://[USER@]NAME[.NAMESPACE][:PORT]/[DBNAME][?context=CONTEXT]
Part What it is
TYPE the type of the database, such as postgres or pg, or the name of an operator. Without it, usql finds the type
USER@ a user of the database. Without it, usql uses the user that it finds
NAME the name of a Service, a StatefulSet, a Deployment, a Pod, a Helm release, or a cluster of an operator
NAMESPACE the namespace. Without it, usql uses the namespace of the current context
PORT the port of the database in the cluster. Without it, usql finds the port
DBNAME the database. Without it, usql uses the database that it finds
context the context of the kubeconfig. Without it, usql uses the current context

These URLs all open the same database:

Terminal window
usql k://pg.shop/app
usql k+pg://pg.shop/
usql k8s+postgres://pg.shop/
usql 'k://pg.shop/?context=kind-usql-site'

\c and \copy take the same URLs. A URL can also be the value of a named connection.

The URL holds no password. usql reads the container of the database and finds the user, the password and the database in its environment. When the password comes from a Secret, usql reads the Secret. The PostgreSQL container of this page reads POSTGRES_USER, POSTGRES_DB, and POSTGRES_PASSWORD from the Secret pg.

To read a password from another Secret, give the name of the Secret and its key as the user part, with a colon between them:

Terminal window
usql 'k://pg:password@pg.shop/'

This form reads the key password of the Secret pg. A user part with no colon, such as app@, is a user of the database.

-W gives an error for such a URL, because the password comes from the cluster:

error: a forwarded connection reads its password where the database runs, so -W does not apply

\conninfo shows what the forward reached, the URL that you typed, and the local URL with the password hidden. This is part of its output:

=> \conninfo
Connection Information
Parameter | Value
----------------+------------------------------------------------------
Driver | postgres
Database | app
Client User | app
Host | 127.0.0.1
Server Port | 39431
...
Forward | Service pg in context kind-usql-site, namespace shop
URL | k://pg.shop/
Local URL | postgres://app:xxxxx@127.0.0.1:39431/app

usql stops the forward when it connects to another database with \c, and when it exits. No kubectl process stays after \q.

\k lists the databases that usql can reach, each with the URL that \c opens:

=> \k -A
List of Kubernetes databases in context kind-usql-site, every namespace
Namespace | Name | Type | Kind | Operator | URL
-----------+------+----------+------------+----------+--------------
shop | pg | postgres | Deployment | | k://pg.shop/
(1 row)
Command What it lists
\k the databases of the current namespace
\k -A the databases of every namespace
\k -n shop the databases of the namespace shop
\k pg*.shop the databases whose name starts with pg, in the namespace shop
\k+ adds the roles, the ready pods, the databases, and where the password comes from

--context NAME lists another context. Only * is a wildcard in a scope. \k reads no Secret.

If no object has the name of the URL, usql prints an error that lists each kind of object that it tried, such as Service: no match and Deployment: no match. Make sure that the name and the namespace are right, and that your context is the one that holds the database.

usql finds a database that an operator runs, such as a CloudNativePG cluster, by the name of its cluster resource. The operator gives the user, the password and the database. dbfwd’s URL rules list the types and the operators, and its lookup rules say how it finds a database from a name.