|
| 1 | +--- |
| 2 | +name: dune-dbt-integration |
| 3 | +description: Troubleshoot and configure Dune + dbt integration. Use when setting up profiles.yml, fixing 401/access denied errors, running dbt against Dune, or querying dbt models in the Dune UI. |
| 4 | +alwaysApply: false |
| 5 | +--- |
| 6 | + |
| 7 | +# Dune dbt Integration |
| 8 | + |
| 9 | +Key learnings for integrating dbt with Dune's Trino connector. |
| 10 | + |
| 11 | +## profiles.yml — Authentication |
| 12 | + |
| 13 | +**Dune uses LDAP, not JWT.** The official template differs from some docs: |
| 14 | + |
| 15 | +```yaml |
| 16 | +# ✅ Correct (from dune-dbt-template) |
| 17 | +method: ldap |
| 18 | +user: dune # Fixed — do not change |
| 19 | +password: "{{ env_var('DUNE_API_KEY') }}" |
| 20 | +catalog: dune # Fixed — do not change |
| 21 | +host: trino.api.dune.com |
| 22 | +port: 443 |
| 23 | +http_scheme: https |
| 24 | +cert: true |
| 25 | +session_properties: |
| 26 | + transformations: true |
| 27 | +``` |
| 28 | +
|
| 29 | +**Do not use:** `method: jwt`, `jwt_token`, or `user: "{{ env_var('DUNE_TEAM_NAME') }}"`. |
| 30 | + |
| 31 | +## Environment Variables |
| 32 | + |
| 33 | +- **dbt does not auto-load `.env`** — run `source .env` before `uv run dbt run`. |
| 34 | +- Required: `DUNE_API_KEY`, `DUNE_TEAM_NAME`. |
| 35 | +- Optional: `DEV_SCHEMA_SUFFIX` for personal dev schemas. |
| 36 | +- Optional: `DUNE_SKIP_VIEW_PROPERTIES=true` — skips `hide_spells()` and `expose_spells()` post-hooks in prod when API key lacks `alter_view_properties` permission. |
| 37 | + |
| 38 | +## Model Config — Dune Restrictions |
| 39 | + |
| 40 | +- **Remove `file_format = 'delta'`** — Dune catalog does not support this property. Causes: `table property 'format' does not exist`. |
| 41 | +- **Never set `format` or `file_format`** in model configs. |
| 42 | + |
| 43 | +## incremental_predicate Macro |
| 44 | + |
| 45 | +Models using `incremental_predicate()` require these vars in `dbt_project.yml`: |
| 46 | + |
| 47 | +```yaml |
| 48 | +vars: |
| 49 | + DBT_ENV_INCREMENTAL_TIME_UNIT: 'day' |
| 50 | + DBT_ENV_INCREMENTAL_TIME: '1' |
| 51 | +``` |
| 52 | + |
| 53 | +Otherwise: `Required var 'DBT_ENV_INCREMENTAL_TIME_UNIT' not found`. |
| 54 | + |
| 55 | +## Schema Naming |
| 56 | + |
| 57 | +| Target | Schema pattern | Example | |
| 58 | +|--------|----------------|---------| |
| 59 | +| dev | `{team}__tmp___{custom}` | `balancer__tmp___balancer_v2_ethereum` | |
| 60 | +| prod | `{team}__{custom}` or `{team}` | `balancer__balancer_v2_ethereum` | |
| 61 | + |
| 62 | +Note: dev uses **three underscores** between `tmp` and the custom schema. |
| 63 | + |
| 64 | +## Querying in Dune UI |
| 65 | + |
| 66 | +Always use the `dune.` catalog prefix: |
| 67 | + |
| 68 | +```sql |
| 69 | +-- Dev |
| 70 | +select * from dune.balancer__tmp___balancer_v2_ethereum.bpt_supply limit 100; |
| 71 | +
|
| 72 | +-- Prod |
| 73 | +select * from dune.balancer.bpt_supply limit 100; |
| 74 | +``` |
| 75 | + |
| 76 | +## Common Errors |
| 77 | + |
| 78 | +| Error | Cause | Fix | |
| 79 | +|-------|-------|-----| |
| 80 | +| 401 Invalid authentication | Wrong auth method or API key | Use LDAP, `user: dune`, `password: DUNE_API_KEY` | |
| 81 | +| Env var not provided | .env not loaded | Run `source .env` before dbt | |
| 82 | +| table property 'format' does not exist | file_format in model | Remove `file_format = 'delta'` | |
| 83 | +| DBT_ENV_INCREMENTAL_TIME_UNIT not found | incremental_predicate macro | Add vars to dbt_project.yml | |
| 84 | +| access denied (prod) | See "Access Denied (Prod)" section below | | |
| 85 | +| Cannot execute procedure dune._internal.alter_view_properties | API key lacks permission for view metadata | Set `DUNE_SKIP_VIEW_PROPERTIES=true` in .env | |
| 86 | + |
| 87 | +## Access Denied (Prod) — Support Recommendation |
| 88 | + |
| 89 | +When support says: *"make sure in your profiles.yml your api key has permissions in your prod target"*, they refer to: |
| 90 | + |
| 91 | +### 1. API key context |
| 92 | + |
| 93 | +- **API key must be from the team account**, not personal. |
| 94 | +- If dev works, the key is team-level — this is not the issue. |
| 95 | +- Prod writes to `{team_name}`; personal keys only have access to `{user}__tmp_*`. |
| 96 | + |
| 97 | +### 2. DUNE_TEAM_NAME exact match |
| 98 | + |
| 99 | +- Prod schema (`{{ env_var('DUNE_TEAM_NAME') }}`) must be **exactly** the team handle on Dune. |
| 100 | +- Docs: *"Verify you're using the correct team namespace"* (Supported SQL Operations). |
| 101 | +- Case-sensitive; no spaces or extra characters. |
| 102 | + |
| 103 | +**Check:** In the Dune UI, what is the exact team handle? Compare with `DUNE_TEAM_NAME` in `.env`. |
| 104 | + |
| 105 | +### 3. Data Transformations enabled |
| 106 | + |
| 107 | +- Docs: *"Dune Enterprise account with Data Transformations enabled"* (prerequisite). |
| 108 | +- *"Verify you're using the correct team namespace and have Data Transformations enabled."* (troubleshooting). |
| 109 | + |
| 110 | +**Check:** Does the team's Enterprise plan have Data Transformations enabled? |
| 111 | + |
| 112 | +### 4. profiles.yml — same key for dev and prod |
| 113 | + |
| 114 | +- Dev and prod use the same `DUNE_API_KEY` (correct). |
| 115 | +- Only difference is schema: dev → `{team}__tmp_*`, prod → `{team}`. |
| 116 | +- `transformations: true` in both (required for writes). |
| 117 | + |
| 118 | +### Checklist for access denied in prod |
| 119 | + |
| 120 | +1. [ ] API key created under **team** context (if dev works, this is done) |
| 121 | +2. [ ] `DUNE_TEAM_NAME` = exact team handle on Dune |
| 122 | +3. [ ] Data Transformations enabled on team's Enterprise plan |
| 123 | +4. [ ] `transformations: true` in session_properties (dev and prod) |
| 124 | +5. [ ] Contact Dune support if all above pass — prod schema may require explicit provisioning |
| 125 | + |
| 126 | +## Deploy Targets |
| 127 | + |
| 128 | +```bash |
| 129 | +# Dev (default) |
| 130 | +uv run dbt run |
| 131 | +
|
| 132 | +# Prod |
| 133 | +uv run dbt run --target prod |
| 134 | +``` |
| 135 | + |
| 136 | +Prod may require additional permissions; dev typically works with a valid org API key. |
| 137 | + |
| 138 | +## Verify API Key |
| 139 | + |
| 140 | +```bash |
| 141 | +curl -X POST -H "X-DUNE-API-KEY: $DUNE_API_KEY" "https://api.dune.com/api/v1/usage" |
| 142 | +``` |
| 143 | + |
| 144 | +Success = JSON with `credits_used`, `billing_periods`. Use **POST**, not GET. |
| 145 | + |
| 146 | +## Prerequisites |
| 147 | + |
| 148 | +- Dune Enterprise account with **Data Transformations** enabled. |
| 149 | +- API key from the **team/org** (not personal) for team schemas. |
0 commit comments