Dev and Prod Targets
Concepts covered: Targets and environments
A model file never says which schema it builds into. stg_orders.sql selects from source('raw', 'orders'), other models reach it through ref('stg_orders'), and dbt writes in the schema when it renders the SQL. That schema comes from outside the project. This lets one set of model files build a copy for an engineer to test and another copy that dashboards read.
The profile holds the targets
dbt_project.yml names a profile, here profile: shop. dbt reads that profile from profiles.yml, a file kept on the machine that runs dbt, usually in the ~/.dbt/ folder of the user's home directory. The file stays out of the project's repository, so the credentials in it are never committed with the models. Each entry under outputs is a target: a warehouse connection plus the schema that models build into. The target: line names the target dbt uses when a command does not ask for another.
shop:
target: dev
outputs:
dev:
schema: dbt_ana
prod:
schema: analytics
Every dbt command accepts --target to choose a target by name. With no flag, dbt run builds into dbt_ana. dbt run --target prod builds the same models into analytics. The SELECT inside each model is identical on both runs. The schema in each relation dbt writes changes, and every ref() resolves inside the chosen schema, so a dev model reads dev parents and a prod model reads prod parents.
| Command | Target | stg_orders is built in | fct_orders reads stg_orders from |
|---|---|---|---|
| dbt run | dev | dbt_ana | dbt_ana |
| dbt run --target prod | prod | analytics | analytics |
A private schema for every engineer
Each person gets a dev schema of their own: dbt_ana, dbt_raj, dbt_mei. If 3 engineers shared one dev schema, Raj's run would replace the fct_orders Ana was halfway through checking, and neither could trust what they queried. Production has one schema and one writer. Dashboards and other teams read analytics, so a table there changes only when the agreed process builds it from reviewed code. Many teams keep the prod target only in the profile on the machine that runs the scheduled build, so no laptop holds credentials that can write to production.
- dev target
- Run by one engineer, many times a day
- Builds into a schema named for that person
- Read only by its owner while a change is checked
- Often limited to recent rows
- prod target
- Run by the scheduled job
- Builds into the shared schema
- Read by dashboards and other teams
- Always holds the full data
The target inside a model
Jinja renders before any SQL reaches the warehouse, and dbt hands it the active target as a variable named target. target.name holds the target's name from the profile and target.schema holds its schema. A model can test target.name in an if block and render differently on each target. The most common use is building dev on a slice of the data: a dev build runs many times a day, and only production needs the full history.
select
order_id,
amount,
order_date
from {{ source('raw', 'orders') }}
{% if target.name == 'dev' %}
where order_date >= date '2024-09-01'
{% endif %}
- dbt run
- dbt run
The first step runs on dev. target.name == 'dev' is true, so the if block renders to where order_date >= date '2024-09-01' and stg_orders holds orders 3 through 5, the 3 rows dated on or after September 1. The second step runs on prod. The condition is false, the block renders to nothing, and stg_orders holds all 5 orders. Nothing in the model file changed between the 2 runs.
Each answer depends on the order of events: Jinja sees the target before the warehouse sees any SQL, so each target gets its own compiled query from one file. After a dev run, and again after a prod run, read the compiled model in target/compiled/. The only differences between the 2 should be the ones your if blocks put there.
Practicing with targets
Add a second output named full to your profile, beside dev, with the schema dbt_ana_full. Build into each, once with no flag and once with --target full, and query both schemas to see the same tables in 2 places. Then add a dev-only where to one model and confirm that the row counts differ by exactly the rows the filter drops. Keep the full target afterwards: the rehearsal at the end of this lesson uses it as a stand-in for production.
- Do
- Give every engineer a dev schema named for them
- Make dev the default target, so a bare command never writes to prod
- Keep prod credentials on the machine that runs the schedule
- Avoid
- Commit a profiles.yml holding passwords to the repository
- Share one dev schema among several engineers
- Change business logic based on target.name
With targets in place, the remaining question is how much of the project one command builds. By default a command builds every model, and the next section narrows that.
About this lesson
This section is part of the Deploying dbt: Beginner lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.