---
title: Query from SQL editor in the workspace
description: Query your Lakebase database from the workspace SQL editor by connecting directly to Lakebase compute or registering in Unity Catalog.
canonical_url: https://docs.databricks.com/gcp/en/oltp/projects/query-sql-editor
---

# Query from SQL editor in the workspace

This page describes how to query databases in your Lakebase project from the SQL editor in the analytics and AI workspace using two different connection methods.

The SQL editor in the workspace is a collaborative SQL workspace where you can author queries, find data catalogs, create visualizations, and share results with colleagues. It has advanced features for analytics, collaboration, and automation.

> [!NOTE]
>
> For Postgres-native features like `EXPLAIN`/`ANALYZE` and meta-commands (`\dt`, `\d`, `\l`), use the Lakebase SQL Editor. See [Query from Lakebase SQL Editor](https://docs.databricks.com/gcp/en/oltp/projects/sql-editor.md).

For complete information about SQL editor capabilities, see [Write queries and explore data in the SQL editor](https://docs.databricks.com/gcp/en/sql/user/sql-editor/index.md).

## Connection methods

You can connect to your Lakebase database from the SQL editor using two methods:

1. **[Direct compute connection](https://docs.databricks.com/gcp/en/oltp/projects/query-sql-editor.md#connect-directly-lakebase-compute)**: Connect directly to your Lakebase compute for immediate query access to a specific project and branch.
2. **[Unity Catalog registration](https://docs.databricks.com/gcp/en/oltp/projects/query-sql-editor.md#register-database-unity-catalog)**: Register your database in Unity Catalog to enable federated queries, unified governance, and cross-source analytics.

### Choose your connection method

| Feature              | Direct compute connection                                                         | Unity Catalog registration                                                   |
| -------------------- | --------------------------------------------------------------------------------- | ---------------------------------------------------------------------------- |
| **Data access**      | Full read-write access to your Lakebase database                                  | Read-only access through Unity Catalog                                       |
| **Best for**         | Working primarily with Lakebase data without combining it with other data sources | Combining Lakebase data with other Unity Catalog tables in federated queries |
| **Setup complexity** | Immediate access to query a specific database without registration                | Requires catalog registration and Unity Catalog privileges                   |
| **Governance**       | No Unity Catalog governance features                                              | Unified governance controls (permissions, lineage, audit logs)               |
| **Access control**   | Postgres role-based permissions only                                              | Centralized discovery and access control across multiple data sources        |
| **Use cases**        | Direct database operations and modifications                                      | Dashboards and applications that integrate transactional and analytical data |

## <a id="connect-directly-lakebase-compute" />Method 1: Connect directly to Lakebase compute

Use this method to connect directly to your Lakebase compute for immediate query access.

### Prerequisites

If you're not the project owner, verify you have:

* **Postgres role**: A corresponding Postgres role to access the database. See [Manage Postgres roles](https://docs.databricks.com/gcp/en/oltp/projects/postgres-roles.md).
* **Database permissions**: The necessary permissions to access the database, schema, or table. See [Manage permissions](https://docs.databricks.com/gcp/en/oltp/projects/manage-roles-permissions.md).

If you lack the necessary role or permissions, contact the project owner to request access.

> [!NOTE]
>
> The Lakebase compute must be in an active state to attach to it. If the compute is idle, you can't attach. Computes typically become inactive when [scale to zero](https://docs.databricks.com/gcp/en/oltp/projects/scale-to-zero.md) is enabled and there's no database activity.
>
> To activate an idle compute, run a query in the Lakebase SQL Editor or disable scale to zero to ensure your compute remains active. See [Scale to zero](https://docs.databricks.com/gcp/en/oltp/projects/scale-to-zero.md).

To connect directly to your Lakebase compute:

1. Use the app switcher to go to **Analytics and AI**.
2. Open the SQL editor by clicking ![SQL Editor Icon](https://docs.databricks.com/gcp/en/images/sql/editor/sql-editor-icon.png) **SQL Editor** in the sidebar.
3. From the compute drop-down menu, select **More...**

![Connect menu](https://docs.databricks.com/gcp/en/images/oltp/dbx-sql-editor-select-more.png)

4. On the **Attach to an existing compute resource** dialog, select **Lakebase Postgres**, and then select your **Project** and **Branch**.

![Attach to LKB project and branch](https://docs.databricks.com/gcp/en/images/oltp/dbx-sql-editor-attach.png)

5. Click **Attach** to connect.

### Example queries with direct connection

After connecting, you can run standard Postgres SQL queries against your Lakebase database.

**Create a table, insert data, and query it:**

```sql title="PostgreSQL"
CREATE TABLE IF NOT EXISTS playing_with_lakebase(id SERIAL PRIMARY KEY, name TEXT NOT NULL, value REAL);
INSERT INTO playing_with_lakebase(name, value)
SELECT LEFT(md5(i::TEXT), 10), random() FROM generate_series(1, 10) s(i);
SELECT * FROM playing_with_lakebase;
```

**Query a specific table:**

```sql title="PostgreSQL"
SELECT * FROM public.playing_with_lakebase;
```

> [!IMPORTANT]
>
> **Limitations with direct connection:**
>
> * **Federated queries not supported**: You can only query the connected Lakebase project and branch. You can't combine Lakebase data with other Unity Catalog tables in a single query.
> * **Postgres meta-commands not supported**: The [Postgres meta-commands](https://docs.databricks.com/gcp/en/oltp/projects/connect-psql.md#meta-commands) (like `\dt`, `\d`, `\l`) that work in the Lakebase SQL Editor don't work in the SQL editor (Lakehouse).

## <a id="register-database-unity-catalog" />Method 2: Register database in Unity Catalog

Use this method to register your Lakebase database in Unity Catalog, enabling federated queries and unified governance across your data sources.

### Prerequisites

Verify you have:

* **Postgres role**: A corresponding Postgres role to access the database. See [Manage Postgres roles](https://docs.databricks.com/gcp/en/oltp/projects/postgres-roles.md).
* **Database permissions**: The necessary permissions to access the database, schema, or table. See [Manage permissions](https://docs.databricks.com/gcp/en/oltp/projects/manage-roles-permissions.md).
* **Unity Catalog privileges**: `CREATE CATALOG` privileges on the Unity Catalog metastore to register the database
* **Compute**: A serverless SQL warehouse or [serverless compute for notebooks and jobs](https://docs.databricks.com/gcp/en/compute/serverless/index.md). Pro and classic SQL warehouses and classic compute aren't supported and return a `PERMISSION_DENIED` error.

If you lack the necessary role or permissions, contact the project owner to request access.

> [!IMPORTANT]
>
> Lakebase catalogs require serverless compute. If you see a `PERMISSION_DENIED` error when querying from the SQL editor, open the warehouse drop-down and switch to a serverless SQL warehouse.

### Register your database

To register a Lakebase database in Unity Catalog:

1. Use the app switcher to go to **Analytics and AI**.
2. In Catalog Explorer, click the plus button and **Create a catalog**.
3. Enter a catalog name (for example, `lakebase_catalog`).
4. Select **Lakebase Postgres** as the catalog type, then choose the **Autoscaling** option.
5. Select your project, branch, and Postgres database.
6. Click **Create**.

The catalog appears in Catalog Explorer, where you can find schemas, tables, and views.

### Query through Unity Catalog

After registration, query your Lakebase database from a serverless SQL warehouse or from notebooks and jobs running on serverless compute.

**Query a registered database:**

```sql title="Databricks SQL"
-- Query a registered Lakebase database
SELECT * FROM chatbot_catalog.public.conversations
WHERE created_at >= current_date - INTERVAL 7 DAYS;
```

**Combine Lakebase and lakehouse data:**

```sql title="Databricks SQL"
-- Join Lakebase and Unity Catalog data
SELECT
  c.conversation_id,
  c.user_id,
  c.agent_response,
  u.user_name,
  u.subscription_tier
FROM chatbot_catalog.public.conversations c
JOIN main.user_analytics.users u
  ON c.user_id = u.user_id;
```

For information about Unity Catalog permissions and granting access to other users, see [Permissions and access control](https://docs.databricks.com/gcp/en/oltp/projects/register-uc.md#permissions-and-access-control).

> [!IMPORTANT]
>
> **Limitations with Unity Catalog registration:**
>
> * **Read-only access**: Catalogs registered from Lakebase databases are read-only through Unity Catalog. To modify data, use the Lakebase SQL Editor or connect directly to your database.
> * **Single database per catalog**: Each Unity Catalog catalog represents one Lakebase database. To expose multiple databases, register each one separately.
> * **Metadata sync**: Unity Catalog caches metadata to reduce Postgres requests. New objects may not appear immediately. Click ![Refresh Schema Icon](https://docs.databricks.com/gcp/en/images/sql/refresh-schema-icon.png) to trigger a full refresh.
> * **Branched database registration**: Registering a branched database as a separate catalog is not yet supported. Branches inherit Unity Catalog registration metadata from their parent, which causes new catalog registration to fail.

For complete information about registering databases in Unity Catalog, see [Register a Lakebase database in Unity Catalog](https://docs.databricks.com/gcp/en/oltp/projects/register-uc.md).

## <a id="additional-resources" /> Additional resources

* [Use the Lakebase SQL Editor](https://docs.databricks.com/gcp/en/oltp/projects/sql-editor.md)
* [Manage Postgres roles](https://docs.databricks.com/gcp/en/oltp/projects/postgres-roles.md)
* [Configure scale to zero](https://docs.databricks.com/gcp/en/oltp/projects/scale-to-zero.md)
