Search code examples
dbt

Env var required but not provided - dbt CLI


We have an environment variable set in dbtCloud called DBT_SNOWFLAKE_ENV that selects the right database depending on which environment is used.

At the moment, I'm trying to set up dbt CLI with VSCode. I created a profiles.yml file that looks like this:

default:
  target: development
  outputs:
    development:
      type: snowflake
      account: skxxxx.eu-central-1

      user: <name>
      password: <pass>

      role: sysadmin
      warehouse: transformations_dw
      database: " {{ env_var('DBT_SNOWFLAKE_ENV', 'analytics_dev') }} "
      schema: transformations
      threads: 4

I added the env_var line after some suggestions but I realise that the environment variable still doesn't exist yet. The problem I see is that if I hardcode analytics_dev in that place (which makes sense), the error still persists.

I wouldn't want anybody who's going to use dbt to have to change the environment variable if they want to run something on production.

What are my options here?


Solution

  • You can set up a source file for the variables on dbt cli - for example you would create a bash script called set_env_var.sh and then source set_env_var.sh in your terminal.

    An example of the bash script would be:

    export SNOWFLAKE_ACCOUNT=xxxxx
    export SNOWFLAKE_USER=xxxxx
    export SNOWFLAKE_ROLE=xxxx
    export SNOWFLAKE_SCHEMA=xxxx
    export SNOWFLAKE_WAREHOUSE=xxxxx
    

    and in your profiles.yml you can add all the variables you want, for example..

    warehouse: "{{ env_var('SNOWFLAKE_WAREHOUSE') }}"
    database: "{{ env_var('SNOWFLAKE_DATABASE') }}"
    

    Hope this helps.