| name | dbt-commands |
| description | dbt command-line operations, model selection syntax, Jinja patterns, troubleshooting, and debugging. Use this skill when running dbt commands, selecting specific models, debugging compilation errors, using Jinja macros, or troubleshooting dbt execution issues. |
dbt Commands & Operations
Purpose
Transform AI agents into experts on dbt command-line operations, model selection patterns, Jinja
templating, and troubleshooting techniques for efficient dbt development and debugging.
When to Use This Skill
Activate this skill when users ask about:
- Running dbt commands (build, run, test, compile)
- Model selection syntax and patterns
- Debugging compilation or execution errors
- Using Jinja macros and templates
- Troubleshooting dbt issues
- Command-line flags and options
- Generating and serving documentation
- Understanding dbt command output
Official dbt Documentation:
dbt Command Reference
Essential Commands
Setup & Installation
dbt deps
dbt debug
dbt clean
Building Models
dbt build
dbt build --full-refresh
dbt run
dbt run
dbt test
Best Practice: Use dbt build instead of separate run + test commands.
Model Selection Syntax
Basic Selection
dbt run --select model_name
dbt run --select dim_customers
dbt run --select dim_customers fct_orders dim_products
Dependency Selection
dbt run --select +model_name
dbt run --select +dim_customers
dbt run --select model_name+
dbt run --select dim_customers+
dbt run --select +model_name+
dbt run --select +dim_customers+
Visualization:
+model_name = model + all parents
model_name+ = model + all children
+model_name+ = model + all parents + all children
Selection by Tag
dbt run --select tag:bronze
dbt run --select tag:gold
dbt run --select tag:gold tag:critical
dbt run --select tag:gold,tag:silver
Common tags:
tag:bronze - All staging models
tag:silver - All intermediate models
tag:gold - All mart models
Selection by Folder
dbt run --select bronze
dbt run --select gold
dbt run --select bronze.crawl
dbt run --select gold.run
dbt run --select bronze silver
Exclude Models
dbt run --exclude model_name
dbt run --exclude tag:deprecated
dbt run --exclude bronze
dbt run --select tag:gold --exclude fct_large_table
Advanced Selection
dbt run --select state:modified --state ./prod-manifest/
dbt run --select package:dbt_utils
dbt test --select test_type:generic
dbt test --select test_type:singular
dbt run --select tag:gold,tag:critical
Official dbt Docs:
Node Selection Syntax
Testing Commands
dbt test
dbt test --select dim_customers
dbt test --select dim_customers,column:customer_id
dbt test --select test_type:generic
dbt test --select test_type:singular
dbt test --select +dim_customers+
dbt test --store-failures
dbt test --select tag:gold
Documentation Commands
dbt docs generate
dbt docs serve
dbt docs serve --port 8001
dbt docs generate --static
Accessing docs: Navigate to http://localhost:8080 after running dbt docs serve
Debugging Commands
dbt compile --select model_name
cat target/compiled/your_project/models/path/to/model.sql
dbt run --select model_name --debug
dbt run --select model_name --log-level debug
dbt list
dbt list --select tag:gold
dbt list --select +dim_customers+ --output path
Jinja Patterns
Official dbt Documentation: Jinja & Macros
Loops
{% for status in ['pending', 'shipped', 'delivered', 'cancelled'] %}
sum(case when status = '{{ status }}' then 1 else 0 end) as {{ status }}_count
{%- if not loop.last -%},{%- endif %}
{% endfor %}
Output:
sum(case when status = 'pending' then 1 else 0 end) as pending_count,
sum(case when status = 'shipped' then 1 else 0 end) as shipped_count,
sum(case when status = 'delivered' then 1 else 0 end) as delivered_count,
sum(case when status = 'cancelled' then 1 else 0 end) as cancelled_count
Conditionals
{% if target.name == 'prod' %}
where is_active = true
{% else %}
where order_date >= dateadd(day, -7, current_date())
limit 1000
{% endif %}
Macros
Define macro:
{% macro cents_to_dollars(column_name) %}
round({{ column_name }} / 100.0, 2)
{% endmacro %}
Use macro:
select
{{ cents_to_dollars('amount_cents') }} as amount_dollars
from {{ ref('stg_payments') }}
Built-in Jinja Variables
{{ target.name }}
{{ target.schema }}
{{ target.database }}
{{ run_started_at }}
{{ invocation_id }}
{{ this }}
{{ flags.FULL_REFRESH }}
Example usage:
select
*,
'{{ target.name }}' as target_environment,
'{{ run_started_at }}' as dbt_run_timestamp,
'{{ invocation_id }}' as dbt_invocation_id
from {{ ref('stg_customers') }}
Variables
Define in dbt_project.yml:
vars:
lookback_days: 7
default_currency: "USD"
Use in models:
select *
from {{ ref('stg_orders') }}
where order_date >= dateadd(day, -{{ var('lookback_days') }}, current_date())
Override via command line:
dbt run --vars '{"lookback_days": 30}'
dbt_utils Functions
Official dbt_utils Documentation: dbt_utils
{{ dbt_utils.generate_surrogate_key(['customer_id', 'order_id']) }}
{{ dbt_utils.get_column_values(ref('dim_products'), 'product_category') }}
{{ dbt_utils.date_spine(
datepart="day",
start_date="to_date('2020-01-01', 'yyyy-mm-dd')",
end_date="current_date()"
) }}
{{ dbt_utils.union_relations(
relations=[ref('orders_2023'), ref('orders_2024')]
) }}
select
{{ dbt_utils.star(ref('stg_customers'), except=['internal_id', 'ssn']) }}
from {{ ref('stg_customers') }}
Troubleshooting
Connection Issues
Symptom: dbt debug fails
Solution:
dbt debug
ls ~/.dbt/profiles.yml
echo $SNOWFLAKE_ACCOUNT
echo $DBT_ENV_SECRET_SNOWFLAKE_PAT
Compilation Errors
Symptom: Model fails to compile with Jinja/SQL errors
Solution:
dbt compile --select modelname
cat target/compiled/your_project/models/path/to/modelname.sql
dbt run --select modelname --debug
Common issues:
- Missing
{% endif %} or {% endfor %}
- Unclosed Jinja blocks
- Invalid ref() or source() references
Execution Errors
Symptom: Model compiles but fails to run
Solution:
dbt run --select modelname --debug
cat logs/dbt.log | grep ERROR
cat logs/dbt.log | grep modelname
Common issues:
- Invalid SQL syntax
- Division by zero
- Data type mismatches
- Permission errors
Dependency Errors
Symptom: Model can't find upstream dependencies
Solution:
dbt list --select +modelname
dbt list --select upstream_model_name
dbt compile --select modelname
Test Failures
Symptom: Tests fail unexpectedly
Solution:
dbt test --select modelname --store-failures
dbt test --select modelname,column:column_name
cat models/path/_models.yml
Performance Issues
Symptom: Model runs very slowly
Solution:
dbt run --select modelname --debug --log-level debug
cat target/compiled/your_project/models/path/to/modelname.sql
Common Commands Cheatsheet
| Task | Command |
|---|
| Build one model | dbt build --select model_name |
| Build with dependencies | dbt build --select +model_name+ |
| Full refresh incremental | dbt build --full-refresh |
| Test one model | dbt test --select model_name |
| Run by tag | dbt run --select tag:bronze |
| Generate docs | dbt docs generate |
| Debug connection | dbt debug |
| Clean project | dbt clean |
| Compile without running | dbt compile --select model_name |
| List models | dbt list |
Target-Specific Execution
dbt run --target dev
dbt run --target prod
dbt build --select tag:gold --target prod
Define targets in ~/.dbt/profiles.yml:
your_project:
target: dev
outputs:
dev:
type: snowflake
schema: dbt_dev
prod:
type: snowflake
schema: analytics
Helping Users with Commands
Strategy for Assisting Users
When users ask about dbt commands:
- Understand the goal: What are they trying to accomplish?
- Recommend appropriate command: build vs run vs test
- Provide selection syntax: Specific models, tags, or folders
- Include relevant flags: --full-refresh, --debug, --store-failures
- Show expected output: What success looks like
- Offer troubleshooting: Common issues and solutions
Common User Questions
"How do I run just this model?"
dbt build --select model_name
"How do I run this model and everything it depends on?"
dbt build --select +model_name
"How do I test all my gold layer models?"
dbt test --select tag:gold
"How do I debug why my model won't compile?"
dbt compile --select model_name --debug
cat target/compiled/your_project/models/path/to/model.sql
"How do I see what SQL dbt generated?"
dbt compile --select model_name
cat target/compiled/your_project/models/path/to/model.sql
Related Official Documentation
Goal: Transform AI agents into expert dbt operators who efficiently execute commands, select
appropriate models, debug issues, and leverage Jinja patterns for dynamic SQL generation.