Running Dbt Commands

dbt-labs/dbt-agent-skills/skills/dbt/skills/running-dbt-commands

作者 dbt-labs168a2b0b92da59be88866257140907c206ff0e44无许可证731 个星标收录于 2026年10月9日更新于 2026年10月9日仓库昨天更新

Formats and executes dbt CLI commands, selects the correct dbt executable, and structures command parameters. Use when running models, tests, builds, compiles, or show queries via dbt CLI. Use when unsure which dbt executable to use or how to format command parameters.

AI 生成的概览

指导 dbt CLI 命令的格式与执行,包括可执行文件选择、选择器和参数。

功能
该技能提供构建和运行 dbt CLI 命令的说明,涵盖命令模式、选择器语法、资源类型过滤、变量、延迟执行和静态分析。它说明如何在 dbt Core、dbt Fusion 和 dbt Cloud CLI 之间选择,以及如何查看 target/run_results.json 中的运行结果。它还列出常见错误及其修复方法。它产出的是命令指导和分析步骤,而非文件。
适用场景
在通过命令行运行 dbt 模型、测试、构建、编译或 show 查询时使用。在不确定应调用哪个 dbt 可执行文件,或如何格式化命令参数和选择器时使用。
运行要求
需要安装 dbt(dbt Core、dbt Fusion 或 dbt Cloud CLI)以及一个 dbt 项目;部分命令引用 jq 来解析运行结果。不附带脚本。

Running dbt Commands

Preferences

  1. Use MCP tools if available (dbt_build, dbt_run, dbt_show, etc.) - they handle paths, timeouts, and formatting automatically
  2. Always use build — even when users say "run" - When a user asks to "run" a model, recommend dbt build instead. build = run + test in one step, so it catches data quality issues immediately. dbt run alone is almost never the right answer during development.
  3. Always use --quiet with --warn-error-options '{"error": ["NoNodesForSelectionCriteria"]}' to reduce output while catching selector typos
  4. Always use --select - never run the entire project without explicit user approval

Quick Reference

bash
# Standard command patterndbt build --select my_model --quiet --warn-error-options '{"error": ["NoNodesForSelectionCriteria"]}'
# Preview model outputdbt show --select my_model --limit 10
# Run inline SQL querydbt show --inline "select * from {{ ref('orders') }}" --limit 5
# With variables (JSON format for multiple)dbt build --select my_model --vars '{"key": "value"}'
# Full refresh for incremental modelsdbt build --select my_model --full-refresh
# List resources before runningdbt list --select my_model+ --resource-type model

dbt CLI Flavors

Three CLIs exist. Ask the user which one if unsure.

FlavorLocationNotes
dbt CorePython venvpip show dbt-core or uv pip show dbt-core
dbt Fusion~/.local/bin/dbt or dbtfFaster and has stronger SQL comprehension
dbt Cloud CLI~/.local/bin/dbtGo-based, runs on platform

Common setup: Core in venv + Fusion at ~/.local/bin. Running dbt uses Core. Use dbtf or ~/.local/bin/dbt for Fusion.

Selectors

Always provide a selector. Graph operators:

OperatorMeaningExample
model+Model and all downstreamstg_orders+
+modelModel and all upstream+dim_customers
+model+Both directions+orders+
model+NModel and N levels downstreamstg_orders+1
bash
--select my_model              # Single model--select staging.*             # Path pattern--select fqn:*stg_*            # FQN pattern--select model_a model_b       # Union (space)--select tag:x,config.mat:y    # Intersection (comma)--exclude my_model             # Exclude from selection

Resource type filter:

bash
--resource-type model--resource-type test --resource-type unit_test

Valid types: model, test, unit_test, snapshot, seed, source, exposure, metric, semantic_model, saved_query, analysis

Fusion: --resource-type is not supported with dbt test (dbt-fusion#1628). To run unit tests in Fusion:

  • dbt build --select model_name — builds the model first, then runs all tests including unit tests
  • dbt build --select unit_test_name — targets a specific unit test by name
  • dbt list --resource-type unit_test — lists unit test names for use in selectors

List

Use dbt list to preview what will be selected before running. Helpful for validating complex selectors.

bash
dbt list --select my_model+              # Preview selectiondbt list --select my_model+ --resource-type model  # Only modelsdbt list --output json                   # JSON outputdbt list --select my_model --output json --output-keys unique_id name resource_type config

Available output keys for --output json: unique_id, name, resource_type, package_name, original_file_path, path, alias, description, columns, meta, tags, config, depends_on, patch_path, schema, database, relation_name, raw_code, compiled_code, language, docs, group, access, version, fqn, refs, sources, metrics

Show

Preview data with dbt show. Use --inline for arbitrary SQL queries.

bash
dbt show --select my_model --limit 10dbt show --inline "select * from {{ ref('orders') }} where status = 'pending'" --limit 5

Important: Use --limit flag, not SQL LIMIT clause.

Variables

Pass as STRING, not dict. No special characters (\, \n).

bash
--vars 'my_var: value'                              # Single--vars '{"k1": "v1", "k2": 42, "k3": true}'         # Multiple (JSON)

Analyzing Run Results

After a dbt command, check target/run_results.json for detailed execution info:

bash
# Quick status checkcat target/run_results.json | jq '.results[] | {node: .unique_id, status: .status, time: .execution_time}'
# Find failurescat target/run_results.json | jq '.results[] | select(.status != "success")'

Key fields:

  • status: success, error, fail, skipped, warn
  • execution_time: seconds spent executing
  • compiled_code: rendered SQL
  • adapter_response: database metadata (rows affected, bytes processed)

Defer (Skip Upstream Builds)

Reference production data instead of building upstream models:

bash
dbt build --select my_model --defer --state prod-artifacts

Flags:

  • --defer - enable deferral to state manifest
  • --state <path> - path to manifest from previous run (e.g., production artifacts)
  • --favor-state - prefer node definitions from state even if they exist locally
bash
dbt build --select my_model --defer --state prod-artifacts --favor-state

Static Analysis (Fusion Only)

Override SQL analysis for models with dynamic SQL or unrecognized UDFs:

bash
dbt run --static-analysis=offdbt run --static-analysis=unsafe

Common Mistakes

MistakeFix
Using test after model changeUse build - test doesn't refresh the model
Running without --selectAlways specify what to run
Using --quiet without warn-errorAdd --warn-error-options '{"error": ["NoNodesForSelectionCriteria"]}'
Running dbt expecting Fusion when we are in a venvUse dbtf or ~/.local/bin/dbt
Schema errors after changing files in FusionRun dbt clean to clear the stale schema cache, then re-run
Adding LIMIT to SQL in dbt_showUse limit parameter instead
Vars with special charactersPass as simple string, no \ or \n

来源与署名

来源:dbt-labs/dbt-agent-skills位于skills/dbt/skills/running-dbt-commands提交168a2b0

许可证: 无许可证

内容归原作者所有。SourceWeft 从公开仓库中收录这些内容。

举报或申请下架