Share via


Add a Lakebase resource to a Databricks app

Databricks Apps supports adding Lakebase database instances as app resources to persist data across deployments. These PostgreSQL-backed resources let your app create and manage schemas and tables that retain state.

Important

Lakebase database instances are in Public Preview. Your workspace admin must enable them before you can add them as an app resource.

Add a database resource

Before you can add a Lakebase database instance as an app resource, check that you meet the prerequisites.

Follow the steps in Add resources to a Databricks app to add a database resource. Select Database as the resource type. After you select a database instance, choose a database within the instance.

When you add a database resource:

  • Azure Databricks creates a PostgreSQL role in the selected database instance. The role name matches the service principal's client ID. If the role already exists, Azure Databricks reuses it.
  • Azure Databricks grants the service principal CONNECT and CREATE privileges on the selected database. These privileges let the app create schemas and tables in the database.

Environment variables

When you deploy an app with a database resource, Azure Databricks sets the following environment variables for the first database resource.

If your app uses multiple databases, these variables only reflect the first one. Use valueFrom with the resource key to retrieve the hostname of the database. See Use environment variables to access resources.

Variable Description
PGAPPNAME App name
PGDATABASE Name of the database
PGHOST Host name of the PostgreSQL server
PGPORT Port for the PostgreSQL server
PGSSLMODE SSL mode for the connection
PGUSER Service principal's client ID and role name

Remove a database resource

If you remove database resources from an app, the app attempts to reassign all objects owned by the service principal to the user removing the resource.

The logic that the app uses primarily depends on whether you have a role in the database instance:

Your permissions Role in database instance? Result
CAN MANAGE Yes Azure Databricks reassigns all objects owned by the service principal to you and deletes the service principal's role.
CAN MANAGE No Azure Databricks creates a role for you, reassigns all objects owned by the service principal’s role to you, and deletes the service principal’s role.
No CAN MANAGE N/A Azure Databricks removes the resource, but does not delete the role or reassign ownership. A warning appears in the UI, and you must manually clean up the role and owned objects later.

Notes

Consider the following when you add databases as app resources:

  • If you revoke CONNECT and CREATE from one database and grant them on another in the same update, Azure Databricks updates the privileges but doesn't recreate the service principal's role.
  • Databases persist state. Any schemas or tables created by an app remain even after you re-deploy or stop the app.