Reputation: 205
I am relatively new to DBT and I have been reading about env_var
and I want to use this in a couple of situations and I am having difficultly and looking for some support.
firstly I am trying to use it in my profiles.yml file to replace the user and password so that this can be set when it is invoked. When trying to test this locally (before implementing this on our AWS side) I am failing to find the right syntax and not finding anything useful online. I have tried variations of:
dbt run --vars '{DBT_USER: my_username, DBT_PASSWORD=my_password}'
but it is not recognizing and giving nothing useful error wise. When running dbt run by itself it does ask for DBT_USER
so it is expecting it, but doesn't detail how
I would also like to use it in my dbt_project.yml
for the schema but I assume that this will be similar to the above, just a third variable at the end. Is that the case?
Thanks
Upvotes: 12
Views: 43720
Reputation: 5805
var
and env_var
are two separate features of dbt.
You can use var
to access a global variable you define in your dbt_project.yml
file. The --vars
command-line option lets you override the values of these vars at runtime. See the docs for var
.
You should use env_var
to access environment variables that you set outside of dbt for your system, user, or shell session. Typically you would use environment variables to store secrets like your profile's connection credentials.
To access environment variables in your profiles.yml
file, you replace the values for username and password with a call to the env_var
macro, as they do in the docs for env_var
:
profile:
target: prod
outputs:
prod:
type: postgres
host: 127.0.0.1
# IMPORTANT: Make sure to quote the entire Jinja string here
user: "{{ env_var('DBT_USER') }}"
password: "{{ env_var('DBT_PASSWORD') }}"
....
Then BEFORE you issue the dbt_run
command, you need to set the DBT_USER
and DBT_PASSWORD
environment variables for your system, user, or shell session. This will depend on your OS, but there are lots of good instructions on this. To set a var for your shell session (for Unix OSes), that could look like this:
$ export DBT_USER=my_username
$ export DBT_PASSWORD=abc123
$ dbt run
Note that storing passwords in environment variables isn't necessarily more secure than keeping them in your profiles.yml
file, since they're stored in plaintext and not protected from being dumped into logs, etc. (You shouldn't be checking profiles.yml
into source control). You should consider at least using an environment variable name prefixed by DBT_ENV_SECRET_
so that dbt keeps them out of logs. See the docs for more info
Upvotes: 35