Skip to main content

04. Key DBT Commands

Command Line Mastery​

DBT is executed entirely via the command-line interface (CLI). Mastering these commands is the key to operating smoothly as an Analytics Engineer.


The Daily Developer Workflow​


1. Building & Execution​

dbt run​

The core command. It reads your .sql files, wraps them in DDL (CREATE TABLE/VIEW), and executes them in your data warehouse.

Advanced Node Selection & Execution​

Running an entire production project can take hours. To rapidly develop and debug locally, you use specific flags to drastically reduce execution time and compute costs.

Targeting Specific Models (--select or -s)​

  • Single Model: dbt run -s stg_customers (Runs exactly one model)
  • Folder Path: dbt run -s marts.finance.* (Runs every model inside a specific folder)
  • Dependencies (Upstream): dbt run -s +fct_orders (Runs the model AND all its upstream parents. Great for testing if your source logic changed)
  • Dependent Models (Downstream): dbt run -s stg_customers+ (Runs the model AND all downstream children that depend on it. Crucial for ensuring your changes don't break downstream reports!)
  • Full Lineage: dbt run -s +stg_customers+ (Runs all upstream models, the model itself, and all downstream models)

Skipping Models (--exclude or -e)​

If you want to run a whole folder but a specific model is extremely heavy or failing, you can skip it.

  • Exclude a specific model: dbt run -s marts.finance.* --exclude fct_heavy_financials
  • Exclude a folder: dbt run -s my_project.* --exclude my_project.deprecated.*

Handling Failures & Debugging Runs​

If a complex 50-model pipeline fails on model #48, you DO NOT want to rerun everything from the beginning.

  • Continue from Failure Checkpoint: In modern DBT versions, simply run dbt retry. This reads your last run's state and instantly resumes execution exactly where the failure occurred.
  • Fail Fast: dbt run --fail-fast (Instantly kills the entire pipeline the very moment the first model errors, rather than wasting time trying to build independent models).

2. Testing & Quality Assurance​

dbt test​

Executes all generic tests (like not_null, unique) and custom data tests against your built models. If any test fails, the command returns an error code, stopping deployment pipelines.

dbt build​

The modern standard. dbt build is a massive quality-of-life improvement. It combines run, test, snapshot, and seed into a single command. Crucially, it runs them in chronological order: it builds a model, immediately tests it, and if it fails, it skips building any downstream models.


3. Formatting & Compilation​

dbt compile​

Sometimes your Jinja macros generate broken SQL, and you want to see what DBT is actually sending to the database without executing it. dbt compile renders your Jinja into raw SQL and saves it in the /target/compiled/ folder for you to inspect.

dbt debug​

If your project suddenly cannot connect to Snowflake, run dbt debug. It tests your network connection, your database credentials, and path configurations, and will isolate exactly where the environment failure is.


4. Documentation​

dbt docs generate​

Parses all your YAML definitions and descriptions and generates an interactive, searchable static HTML website representing your data catalog.

dbt docs serve​

Starts a local web server (typically on localhost:8080) so you can explore the documentation and lineage graphs you just generated.


Summary​

You spend 90% of your time running dbt build --select [model]. Mastering the --select syntax allows you to rapidly iterate on isolated parts of the data pipeline without wasting cloud compute costs.