Using dbt connections in supporting Python scripts

If you need a connection in a support script or Jupyter notebook, you can use this snippet to get a new connection based on your dbt_project.yml and profiles.yml. Note that this is for Postgres or Redshift and doesn’t take advantage of adapters; I welcome improvements!

import psycopg2
import dbt.project

def new_conn():
    project = dbt.project.read_project('dbt_project.yml')
    profile = project.profiles[project['profile']]
    conn_info = profile['outputs'][profile['target']]
    return psycopg2.connect(host=conn_info['host'],
                            port=conn_info['port'],
                            user=conn_info['user'],
                            password=conn_info['pass'],
                            database=conn_info['dbname'])
3 Likes

This is amazing. We’d love the dbt library to be more useful in the context of jupyter notebooks and other random python scripts and this is a great start. Would be curious if folks have other ideas of things they’d like to be able to do after import dbt.