DBT : How to get a new connection based on your dbt_project.yml and profiles.yml [Postgres or Redshift]

getDbt

The below statement is referring to the process of establishing a database connection in a support script or Jupyter notebook that is using DBT. The snippet provided is meant to connect to a Postgres or Redshift database and is based on the information provided in the dbt_project.yml and profiles.yml files.

The dbt_project.yml file contains information about your DBT project, including the database type, connection details, and other project-specific settings. The profiles.yml file contains information about the database profiles you have created, including the connection string and authentication details.

By using the snippet, you can extract the necessary information from these files and use it to establish a connection to the database. However, it is important to note that this snippet only works with Postgres or Redshift databases and does not take advantage of the adapter functionality provided by DBT. This means that you may need to modify the code if you are using a different type of database.

It is recommended that you understand the DBT project and profile configuration files before using this snippet to ensure that you are able to establish a reliable and secure connection to your database.

import psycopg2
import dbt.project

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

Get more useful articles on dbt

  1. ,
Author: user

Leave a Reply