0
0
dbtdata~10 mins

Environment management (dev, staging, prod) in dbt - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Environment management (dev, staging, prod)
Start: Choose Environment
Use dev settings
No
Use staging settings
No
Use prod settings
Run dbt commands with chosen environment
Deploy or test changes
End
This flow shows how dbt selects settings based on environment (dev, staging, prod) and runs commands accordingly.
Execution Sample
dbt
profiles:
  my_project:
    target: dev
    outputs:
      dev:
        type: snowflake
        ...
      staging:
        type: snowflake
        ...
      prod:
        type: snowflake
        ...
This dbt profile configures three environments with different connection settings.
Execution Table
StepEnvironment VariableTarget SelectedActionResult
1DBT_ENV=devdevLoad dev settingsConnect to dev database
2Run dbt rundevCompile and run modelsModels built in dev schema
3DBT_ENV=stagingstagingLoad staging settingsConnect to staging database
4Run dbt teststagingRun tests on stagingTests executed on staging schema
5DBT_ENV=prodprodLoad prod settingsConnect to production database
6Run dbt runprodCompile and run modelsModels built in prod schema
7DBT_ENV=unknownprod (default)Load prod settingsConnect to production database
8Run dbt runprodCompile and run modelsModels built in prod schema
💡 Execution stops after running commands in the selected environment.
Variable Tracker
VariableStartAfter Step 1After Step 3After Step 5After Step 7
DBT_ENVundefineddevstagingprodunknown
targetundefineddevstagingprodprod (default)
connectionundefineddev connectionstaging connectionprod connectionprod connection
Key Moments - 3 Insights
Why does dbt use the prod environment when DBT_ENV is unknown?
dbt defaults to the 'prod' target if the environment variable is not set or unknown, as shown in execution_table row 7.
How does dbt know which database to connect to?
dbt reads the target environment from DBT_ENV and loads the matching profile settings, as seen in steps 1, 3, and 5.
Can you run tests in the dev environment?
Yes, you can run tests in any environment by setting DBT_ENV accordingly and running 'dbt test', similar to step 4 for staging.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3. What environment is selected?
Astaging
Bprod
Cdev
Dunknown
💡 Hint
Check the 'Target Selected' column at step 3 in the execution_table.
At which step does dbt run models in the production environment?
AStep 2
BStep 6
CStep 4
DStep 8
💡 Hint
Look for 'prod' in 'Target Selected' and 'Run dbt run' in 'Action' column.
If DBT_ENV is set to 'dev', what will be the connection after step 1 according to variable_tracker?
Astaging connection
Bprod connection
Cdev connection
Dundefined
💡 Hint
Check 'connection' variable value after step 1 in variable_tracker.
Concept Snapshot
Environment management in dbt:
- Use DBT_ENV variable to select environment
- Profiles.yml defines dev, staging, prod targets
- dbt commands run with selected target
- Defaults to prod if unknown
- Enables safe testing and deployment
Full Transcript
This visual execution shows how dbt manages environments like dev, staging, and prod. It starts by reading the DBT_ENV variable to select the target environment. Based on this, dbt loads the corresponding connection settings from profiles.yml. Then, dbt runs commands like 'dbt run' or 'dbt test' in the chosen environment. If DBT_ENV is not set or unknown, dbt defaults to the production environment to avoid accidental use of wrong settings. Variables like DBT_ENV, target, and connection update step-by-step as commands execute. This helps keep development, testing, and production separate and safe.