# Metabase

Connect [Metabase](https://www.metabase.com) to the database or query engine that reads your Send2Bucket data. Metabase needs a database connection even when the underlying records live in object storage.

| You have | Use |
|---|---|
| Records loaded into PostgreSQL or MySQL | A [PostgreSQL or MySQL connection](#postgresql-or-mysql) |
| BigQuery | A [BigQuery connection](#bigquery) |
| Athena, Trino or Starburst over the bucket or Iceberg table | An [Athena or Starburst connection](#athena-trino-and-starburst) |
| ClickHouse, Redshift or [Snowflake](../../querying/snowflake.md) | [That engine's connection](#clickhouse-redshift-and-snowflake) |
| MotherDuck, or a DuckDB file | The [DuckDB driver](#duckdb-and-motherduck) |

## PostgreSQL or MySQL

Load records on a schedule as in [PostgreSQL](../../querying/postgres.md) or [MySQL](../../querying/mysql.md). Metabase then reads the `s2b_records` table like any other.

1. Create a user that can only read:

   ```sql
   CREATE ROLE metabase LOGIN PASSWORD '<password>';
   GRANT CONNECT ON DATABASE <database> TO metabase;
   GRANT USAGE ON SCHEMA public TO metabase;
   GRANT SELECT ON s2b_records TO metabase;
   ```

2. In Metabase, go to **Admin settings > Databases > Add a database**, choose **PostgreSQL**, and fill in host, port, database name, username and password. Turn on **Use a secure connection (SSL)** for a database outside your network.
3. Under the advanced options, turn off **Unfold JSON Columns**. `data` and `resource` hold differently shaped JSON for each signal, and unfolding them makes every sync scan the table.

Metabase lists `data`, `resource` and `scope` as JSON columns. The query builder cannot reach inside them, so use one of two approaches.

**Native queries.** Open **New > SQL query** and paste any query from the [PostgreSQL example queries](../../querying/postgres.md#example-queries). We ran all four through Metabase's API against the sample data. A `[[ ]]` block with a variable becomes a filter widget on the question or dashboard:

```sql
SELECT data->>'event' AS event, count(*) AS events
FROM s2b_records
WHERE signal = 'event'
  [[AND received_at >= {{since}}]]
GROUP BY event
ORDER BY events DESC;
```

Set the `since` variable type to **Date** in the variables sidebar. Without a value, the bracketed condition is left out.

**A view with columns.** Create a view that pulls out the fields people chart, grant it to the Metabase user, and the query builder treats it as a normal table:

```sql
CREATE VIEW s2b_events AS
SELECT received_at,
       data->>'event'        AS event,
       data->>'user_id'      AS user_id,
       (data->>'cents')::int AS cents
FROM s2b_records
WHERE signal = 'event';

GRANT SELECT ON s2b_events TO metabase;
```

After the next sync, or **Sync database schema now** on the database page, `s2b_events` appears under the connection. A question that counts rows and sums `cents` by `event` returns 53 `order.paid` events and 416100 cents on the sample data.

MySQL works the same way with the [MySQL](../../querying/mysql.md) tables. Its generated columns, such as `event` and `received_at`, already show up as plain columns.

## BigQuery

Create the `s2b.records` view from [BigQuery](../../querying/bigquery.md), then add a **BigQuery** database in Metabase with a service account JSON key. Give the service account the BigQuery Data Viewer and BigQuery Job User roles on the project, and nothing that can write. Metabase runs every question as a BigQuery job billed to that project, so filter on `date` in native queries and dashboard filters.

## Athena, Trino and Starburst

- **Athena**: create the table and view from [Athena](../../querying/athena.md), then add an **Amazon Athena** database with the region, workgroup, S3 staging directory and an access key whose IAM policy allows Athena queries, reads on the Send2Bucket prefix, and writes only to the query results location.
- **Trino or Starburst**: Metabase's **Starburst** driver connects to Trino as well. Fill in host, port, catalog (for example `iceberg` or `hive`), and a user with read access to the tables from [Trino](../../querying/trino.md).

## ClickHouse, Redshift and Snowflake

Add the database type of the same name, with a user that can only read the tables you created by following [ClickHouse](../../querying/clickhouse.md) or [Redshift](../../querying/redshift.md). For Snowflake, use a role with `USAGE` on the database and schema and `SELECT` on the tables or views. The example queries on each engine page run as native queries unchanged.

## DuckDB and MotherDuck

Metabase has no built-in DuckDB driver. MotherDuck maintains a community driver:

1. Download `duckdb.metabase-driver.jar` from the [MotherDuck driver's releases](https://github.com/motherduckdb/metabase_duckdb_driver/releases) into Metabase's plugins directory (`MB_PLUGINS_DIR` for Docker), and restart Metabase. Use a Debian or Ubuntu based image, not Alpine.
2. Add a **DuckDB** database. For **Database file**, enter `md:<database>` and paste a read-scoped MotherDuck token, or give the path of a DuckDB file on the Metabase host.
3. Query the views you created by following [MotherDuck](../motherduck.md). The [DuckDB example queries](../../querying/duckdb.md#example-queries) run as native queries.

Metabase Cloud cannot load custom drivers. There, add a **PostgreSQL** connection to MotherDuck's Postgres endpoint instead, as described in [MotherDuck's Metabase guide](https://motherduck.com/docs/integrations/bi-tools/metabase/). Queries sent that way are DuckDB SQL.

## Notes

- Metabase syncs table and column names hourly and scans column values daily by default. Neither copies your records, and every question queries the database when it runs, unless you set a caching policy under **Admin settings > Performance**.
- Data in a loaded table is as fresh as the last load. Set dashboard auto-refresh no shorter than your load schedule.
- Records in `data` are JSON events and OTLP records mixed together. Filter on `signal` in every question, or use views per signal as above.
