03. Managing Environments
Safe Deployments: Managing Environments
In legacy data workflows, analysts often tested SQL code directly against production tables, leading to catastrophic broken dashboards.
DBT prevents this by strictly enforcing environment isolation. You can safely develop and break things in a "Development" environment, while BI tools read safely from the "Production" environment.
️ 1. How Isolation Works (Schema Level)
Data warehouses like Snowflake do not require you to spin up entirely separate physical servers for environments. Instead, isolation is handled at the Schema or Database level.
When Alice runs dbt run locally on her laptop, DBT builds the tables inside dbt_alice_schema. When the automated CI/CD pipeline runs overnight, it builds safely into analytics_schema.
️ 2. Configuring Environments in profiles.yml
The environments are mapped as targets inside your ~/.dbt/profiles.yml file.
my_project:
target: dev # The default target used if no flag is provided
outputs:
dev: # Environment 1: Local Developer
type: snowflake
database: RAW_DEV
schema: dbt_alice # Sandboxed to Alice
prod: # Environment 2: Automated Production
type: snowflake
database: ANALYTICS_PROD
schema: public # The real production schema
3. Switching Environments via the CLI
By default, executing dbt run will use the default target (usually dev). If you want to deploy to production, you override the target using a CLI flag:
# Safely builds to the exact schema defined in the 'dev' target
dbt run
# Forces compilation and building in the 'prod' target
dbt run --target prod
In a mature setup, human developers should not have the password for the prod target on their laptops. The prod target should only be executed by a secure CI/CD system (like GitHub Actions or DBT Cloud) using dedicated service accounts.
Summary
Separating development from production guarantees that your analysts can test wild new logic without ever impacting the dashboards that your CEO looks at.