0
0
DbtHow-ToBeginner ยท 3 min read

How to Use dbt run: Syntax, Example, and Tips

Use the dbt run command to compile and execute your dbt models, transforming raw data into clean tables. Run it in your project directory to build all models or specify particular models with flags like --select.
๐Ÿ“

Syntax

The basic syntax of dbt run is simple. You run it in your dbt project folder to build models.

  • dbt run: Runs all models in the project.
  • --select model_name: Runs only the specified model(s).
  • --exclude model_name: Skips specified model(s).
  • --models is an alias for --select.
bash
dbt run [--select model_name] [--exclude model_name]
๐Ÿ’ป

Example

This example shows how to run all models and then run only a specific model named customers.

bash
cd my_dbt_project
# Run all models
dbt run

# Run only the 'customers' model
dbt run --select customers
Output
Running with dbt=1.4.6 Found 5 models, 0 tests, 0 snapshots, 0 analyses, 0 macros, 0 operations, 0 seed files, 0 sources Running 5 models: 1 of 5 START table model my_dbt_project.customers................ [RUN] 1 of 5 OK created table model my_dbt_project.customers........... [OK in 1.2s] ... Finished running 5 models in 12.3s. Running with dbt=1.4.6 Found 5 models, 0 tests, 0 snapshots, 0 analyses, 0 macros, 0 operations, 0 seed files, 0 sources Running 1 model: 1 of 1 START table model my_dbt_project.customers................ [RUN] 1 of 1 OK created table model my_dbt_project.customers........... [OK in 1.1s] Finished running 1 model in 1.1s.
โš ๏ธ

Common Pitfalls

Some common mistakes when using dbt run include:

  • Running dbt run outside the project directory causes errors because dbt can't find your project files.
  • Not specifying models when you want to run only a subset can lead to long run times.
  • Using outdated model names or typos in --select causes dbt to skip models silently.
  • Not having your profiles.yml configured properly leads to connection errors.
bash
## Wrong: running outside project folder
$ dbt run
# Error: Could not find dbt_project.yml

## Right: change to project folder first
$ cd my_dbt_project
$ dbt run
๐Ÿ“Š

Quick Reference

CommandDescription
dbt runBuilds all models in the project
dbt run --select model_nameBuilds only the specified model(s)
dbt run --exclude model_nameBuilds all models except the specified ones
cd project_folderNavigate to your dbt project directory before running commands
โœ…

Key Takeaways

Run dbt run inside your dbt project folder to build models.
Use --select to run specific models and save time.
Check your profiles.yml to ensure database connection is configured.
Avoid typos in model names when using selection flags.
Running dbt run compiles and executes SQL models to create tables or views.