Skip to main content

Connect PostgreSQL with Azure Managed Identity

By default, Witboost connects to PostgreSQL using a username and password. If you'd rather avoid storing a database password, Witboost can instead connect to Azure Database for PostgreSQL using an Azure Managed Identity.

warning

This is an advanced, opt-in configuration. It has been validated with AKS (Azure Kubernetes Service) and Azure Database for PostgreSQL Flexible Server.

Enable it for all components that connect to PostgreSQL. Mixing Managed Identity and password-based connections is not supported.

Step 1 — Set up the prerequisites in Azure and PostgreSQL​

Complete the following one-time setup. The az CLI and SQL commands below are one way to complete each step — the Azure Portal, Bicep, Terraform, or any other tool that reaches the same end state work equally well.

1.1 Enable Microsoft Entra authentication on PostgreSQL​

Your Azure Database for PostgreSQL Flexible Server instance must have Microsoft Entra authentication enabled. Check with:

az postgres flexible-server show \
--resource-group <resource-group> \
--name <postgres-server-name> \
--query authConfig

The output must have "activeDirectoryAuth": "Enabled". passwordAuth can stay "Enabled" too if other users still need password-based access.

1.2 Enable Workload Identity on your AKS cluster​

Your AKS cluster must have the OIDC issuer and Workload Identity features enabled. Check with:

az aks show -n <cluster-name> -g <resource-group> \
--query "oidcIssuerProfile.{enabled:enabled, issuerUrl:issuerUrl}" -o table

1.3 Create an Azure Managed Identity​

Create a User-Assigned Managed Identity:

az identity create --name <mi-name> --resource-group <resource-group> --location <location>

# Capture its client ID — you'll need it in Step 2 below
az identity show --name <mi-name> --resource-group <resource-group> --query clientId -o tsv

1.4 Register the Managed Identity in PostgreSQL and grant privileges​

This step registers the Managed Identity as a PostgreSQL role and gives it access to the databases Witboost uses.

note

New installation versus existing deployment

  • New installation: an administrator creates the databases and gives the Managed Identity the grants shown below. Nothing more is needed, Witboost components create their own schemas and tables, so the Managed Identity owns everything it creates. This satisfies the "owner permissions" requirement in the database prerequisites: the Managed Identity does not need to own the databases themselves, only the schemas and tables it creates.
  • Existing deployment: the same grants apply, but the objects already in your databases are still owned by the previous database user. Please complete step 1.6 before deploying.

Before deploying with Managed Identity authentication, make sure every database your deployment needs from the installation guide's database list exists; otherwise the affected component fails to start.

Connect to the PostgreSQL server as admin. The server must be reachable from wherever you run this.

Then, in psql, register the Managed Identity and grant it access. The example uses core and marketplace; replace them with the databases your deployment needs (not every database applies to every installation):

-- Register the Managed Identity as a PostgreSQL role
SELECT pgaadauth_create_principal('<mi-name>', false, false);

-- Grant CONNECT and CREATE on every database you need, e.g.:
GRANT CONNECT ON DATABASE core TO "<mi-name>";
GRANT CREATE ON DATABASE core TO "<mi-name>";
GRANT CONNECT ON DATABASE marketplace TO "<mi-name>";
GRANT CREATE ON DATABASE marketplace TO "<mi-name>";

-- No schema privileges are needed: Witboost components create their own
-- schemas on first start.
note

Extensions

The Managed Identity cannot create the vector extension: it is not a trusted extension on Azure Database for PostgreSQL, so only members of azure_pg_admin can create it, and only if vector is listed in the server's azure.extensions parameter. Create the extensions yourself, as an administrator, before deploying. The vector extension must always be installed in the public schema, regardless of the schema used for application tables or the connection's search_path:

pgvector (PostgreSQL extension vector) must be enabled in the public schema of the core, marketplace, witty, and kgm databases where those databases are used. Run the following commands only for the databases in your deployment, using their configured names:

\c core
CREATE EXTENSION IF NOT EXISTS pgcrypto;
CREATE EXTENSION IF NOT EXISTS vector WITH SCHEMA public;
\c marketplace
CREATE EXTENSION IF NOT EXISTS vector WITH SCHEMA public;
\c witty
CREATE EXTENSION IF NOT EXISTS vector WITH SCHEMA public;
\c kgm
CREATE EXTENSION IF NOT EXISTS vector WITH SCHEMA public;

Enabling pgvector in the public schema of all other Witboost databases is strongly recommended to prepare for future migrations. Connect to each additional database and run CREATE EXTENSION IF NOT EXISTS vector WITH SCHEMA public; as an administrator.

IF NOT EXISTS does not move an extension already installed in another schema. For each affected database, have the database administrator run ALTER EXTENSION vector SET SCHEMA public; before deploying.

1.5 Create a federated credential​

Add a federated credential on the Managed Identity so AKS can exchange a Kubernetes ServiceAccount token for an Azure AD token:

az identity federated-credential create \
--name "<credential-name>" \
--identity-name <mi-name> \
--resource-group <resource-group> \
--issuer <aks-oidc-issuer-url> \
--subject "system:serviceaccount:<namespace>:witboost-sqlproxy" \
--audience api://AzureADTokenExchange
  • <namespace>: the namespace Witboost will be deployed into.
  • witboost-sqlproxy: the default name of the shared ServiceAccount used in Step 2 below. If you customize it there (--sa-name), use the same name here.

1.6 Existing deployments only — transfer ownership of the existing objects​

Objects created while Witboost used a username and password (tables, sequences, views, functions, types and the schemas themselves) are owned by that database user, the one in WITBOOST_DB_USERNAME. Granting the Managed Identity privileges on them lets it read and write the data, but Witboost components also run schema migrations when they start, and PostgreSQL only lets the owner of an object alter it. Without this step, a component with a pending migration fails and restarts in a loop with an error such as must be owner of table <name>.

Required outcome: in every Witboost database, every object owned by the previous database user is owned by the Managed Identity. Once that is true, the grants from step 1.4 are all it needs; no table, sequence or schema grants are required for those objects.

Use whichever tooling you prefer, for example ALTER ... OWNER TO "<mi-name>" on each object or statements generated from the catalog, as long as the following holds:

  • Cover every database and every schema of the databases from step 1.4. Do not rely on a fixed list of schemas: some databases have many (the core database has one per plugin).
  • Run it as the server administrator, before deploying with sqlProxy, and while Witboost is not under load, because changing ownership locks each table.
  • Schemas owned by someone else. If some objects live in a schema the previous user does not own (typically public), grant the Managed Identity CREATE on that schema first; PostgreSQL requires it for the ownership change.
  • Verify the result. No object in a non-system schema should remain owned by the previous database user.

Going back to username and password. Transfer the ownership back to the previous database user, then deploy your previous values.yaml. Until the ownership is transferred back, the old user cannot access the transferred schemas.

Step 2 — Update your values.yaml using the Infra CLI​

Once the prerequisites above are complete, run the enable-sqlproxy command from the Witboost Infra CLI to automatically update your values.yaml:

witboost-infra enable-sqlproxy \
--user-values values.yaml \
--mi-client-id <managed-identity-client-id> \
--mi-name <mi-name>

This produces a patched copy of your values.yaml, ready to deploy, with every database-connected component of the Witboost chart configured to authenticate using the Managed Identity set up in Step 1.

See the enable-sqlproxy command reference for the full list of options, including how to customize the shared ServiceAccount name referenced in Step 1.5 above.

Step 3 — Deploy​

Review the patched file, then deploy it as you normally would (e.g. helm upgrade referencing the patched values.yaml). If you are converting an existing deployment, complete step 1.6 first.