Add a Lakebase resource to a Databricks app
Add Lakebase database instances as Databricks Apps resources to persist data across deployments. These PostgreSQL-backed resources let your app create and manage schemas and tables that retain state.
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.
- In the App resources section when you create or edit an app, click + Add resource.
- Select Database as the resource type.
- Choose a database instance.
- Choose a database within that instance.
- Select the appropriate permission level for your app. Currently, the only available permission is Can connect and create.
- (Optional) Specify a custom resource key, which is how you reference the database in your app configuration. The default key is
database
.
When you add a database resource:
- 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, Databricks reuses it.
- Databricks grants the service principal
CONNECT
andCREATE
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, 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 |
---|---|
| App name |
| Name of the database |
| Host name of the PostgreSQL server |
| Port for the PostgreSQL server |
| SSL mode for the connection |
| 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 |
---|---|---|
| Yes | Databricks reassigns all objects owned by the service principal to you and deletes the service principal's role. |
| No | 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 | N/A | 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
andCREATE
from one database and grant them on another in the same update, 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.