Skip to content
Silver 4 min read · Jinja, Environments 2 practice problems ↓

var(), target and environments

The same project must build differently in development and production: other schemas, smaller samples, different dates. Variables and the target let one codebase do that. This lesson shows both.

Pipelines in production · dbt · Silver · Jinja and reuse, lesson 2 of 4

Read first: Jinja in dbt · A dbt project, file by file (skip if you know them)

In plain words first

A recipe that says "serves N" works for 2 or for 200; you choose N when you cook. And a kitchen at home and a restaurant kitchen follow the same recipe with different equipment.

var() is the N you pass in. The target is which kitchen you are cooking in: dev or prod.

var(): values passed in

A var is a named value the modelmodel: A SELECT statement in a .sql file. dbt runs it and saves the result in the warehouse as a table or view named after the file. Learn more → reads with {{ var('name') }}. Defaults go in dbt_project.yml; a run can override them with --vars.

YAML · dbt_project.yml
vars:
  start_date: '2024-01-01'
  jaffle_shop:                 # scoped to this project's models
    reporting_days: 30
dbt · models/marts/fct_signups_by_week.sql
select
    date_trunc('week', signed_up_on)  as signup_week,
    count(*)                         as signups
from {{ ref('stg_accounts') }}
where signed_up_on >= '{{ var("start_date") }}'
  -- second argument of var(): the default
  and signed_up_on <  '{{ var("end_date", "2999-12-31") }}'
group by 1
Shell
dbt run --select fct_signups_by_week \
  --vars '{start_date: "2025-03-01", end_date: "2025-04-01"}'

A var with no default in the project and no default argument raises a compile error naming it, which is better than silently building the wrong range. Note the quotes in the SQL: var() returns the value, and JinjaJinja: The template language dbt runs before sending SQL: {{ }} inserts a value, {% %} runs logic such as loops and ifs. Learn more → prints it as-is, so a date needs quotes around it to be a string literal.

target: where this run is going

target describes the connection in use: target.name (dev, prod, ci), target.schema, target.database, target.type (snowflake, bigquery...), target.threads. The common use is to make development builds cheap:

dbt · models/staging/stg_plays.sql · Smaller builds outside production
select play_id, account_id, track_id, played_at
from {{ source('app', 'plays') }}
{% if target.name != 'prod' %}
-- 3 days outside prod, full history in prod
where played_at >= dateadd('day', -3, current_date)
{% endif %}

Two things to get right in that if:

  • Compare by target name, not by schema or user name. Names like dev, ci and prod are stable; schemas and users change from person to person.
  • Decide what a new target gets. {% if target.name != 'prod' %} limits everything except prod: dev, ci, and any target added later. {% if target.name == 'dev' %} limits only dev, so CI builds full history. Limiting everything but prod is the usual default, because it keeps CI cheap.
Watch out: Never let target-based logic change the meaning of a model in production, only its size. If prod and dev compute different business logic, tests in dev prove nothing about prod.

env_var(): values from the environment

{{ env_var('DBT_PASSWORD') }} reads an environment variable, with an optional default as the second argument. Use it for secrets and for anything the orchestrator decides (a run date, a warehousewarehouse: A database built for analytics on large tables, like Snowflake, BigQuery, Databricks or Redshift. dbt runs its SQL there. Learn more → size). Variables whose names start with DBT_ENV_SECRET_ are scrubbed from logs.

Which one when

NeedUse
A business parameter: reporting window, a cut-off date, a list of countriesvar(), with a default
Different behaviour per environment: sample in dev, more threads in prodtarget
Secrets and values the scheduler injectsenv_var()

Common mistakes

  • Printing a date var without quotes — where d >= {{ var("start") }} compiles to 2024 - 01 - 01, an arithmetic expression.
  • Checking target.name == 'dev' for sampling — CI and any new target build the full table. Prefer != 'prod' for cost limits.
  • Putting secrets in vars — Vars are in dbt_project.yml and logs. Secrets go through env_var().

Key takeaways

  • var() reads a value from dbt_project.yml or --vars; give it a default.
  • target tells a model which environment it is building in.
  • Use target to change data volume in dev, never business logic.
  • env_var() is for secrets and scheduler-provided values.

Interview prep

THE QUESTION

"How do you make dbt development builds faster and cheaper without changing production logic?"

Use target: in staging models over large sources, add {% if target.name != 'prod' %} where played_at >= dateadd(day, -3, current_date) {% endif %}, so every non-production target builds a sample and prod builds everything. Only the amount of data changes, never the logic. Combine with --defer to read unchanged upstream models from production instead of rebuilding them, and with smaller warehouses per target in profiles.yml.

Avoid: "we have a separate copy of the model for dev". The same code should run everywhere, with target and vars changing only size and parameters.

Follow-ups · answer out loud, then open

Follow-up"How do you pass a reporting date into a dbt run from Airflow?"
With --vars '{report_date: "2025-03-01"}' read through var('report_date'), or with an environment variable read by env_var(). Give the var a sensible default so manual runs work.
Scenario"Dev builds of a staging model over a 5 TB plays table cost too much. What do you do?"
Limit data outside production with an {% if target.name != 'prod' %} block that adds a recent-days filter, and use --defer so unchanged upstream models are read from production instead of rebuilt. The logic stays identical; only the volume changes.
Trap"Use var('env') == 'prod' to detect production."
A var can be forgotten or overridden by mistake. target.name comes from the connection actually used, so it is the reliable signal.

Check yourself

3 questions

How do you give var('end_date') a fallback inside the model?

Show the answer

var('end_date', '2999-12-31'). The second argument of var() is its default.

Which condition limits data in dev and CI but not in prod?

Show the answer

target.name != 'prod'. Anything other than prod is limited, including targets added later.

Where should a warehouse password come from?

Show the answer

env_var(). env_var reads it from the environment; DBT_ENV_SECRET_ variables are scrubbed from logs.

Practise this

Problems that use this, graded on hidden tests

Keep going

Up next · Jinja and reuse · lesson 3 of 4 · 5 min read
Macros and custom generic tests
Reusable SQL with arguments and defaults, and your own generic tests built the same way.

Related lessons

Previous: Jinja in dbt

Primary sources: Project variables · target · env_var