Skip to content


With prefect-dbt you can trigger and observe dbt Cloud jobs, execute dbt Core CLI commands, and incorporate other tools, such as Snowflake, into your dbt runs. Prefect provides a global view of the state of your workflows and allows you to take action based on state changes.

Getting started


Install prefect-dbt

Install prefect-dbt:

pip install -U prefect-dbt

If necessary, see additional installation options for dbt Core with BigQuery, Snowflake, and Postgres.

To install with all additional functionality, use the following command:

pip install -U "prefect-dbt[all_extras]"

Register newly installed blocks types

Register the block types in the prefect-dbt module to make them available for use.

prefect block register -m prefect_dbt

Explore the examples below to use Prefect with dbt.

Integrate dbt Cloud jobs with Prefect flows

If you have an existing dbt Cloud job, use the pre-built flow run_dbt_cloud_job to trigger a job run and wait until the job run is finished.

If some nodes fail, run_dbt_cloud_job efficiently retries the unsuccessful nodes.

Prior to running this flow, save your dbt Cloud credentials to a DbtCloudCredentials block:

from prefect import flow

from import DbtCloudJob
from import run_dbt_cloud_job

def run_dbt_job_flow():
    result = run_dbt_cloud_job(
    return result


Integrate dbt Core CLI commands with Prefect flows

Prefect-dbt supports execution of dbt Core CLI commands. If you don't have a DbtCoreOperation block saved, create one and set the commands that you want to run.

Optionally, specify the project_dir. If profiles_dir is not set, the DBT_PROFILES_DIR environment variable will be used. If DBT_PROFILES_DIR is not set, the default directory will be used $HOME/.dbt/.

Use an existing profile

If you have an existing dbt profile, specify the profiles_dir where profiles.yml is located:

from prefect import flow
from prefect_dbt.cli.commands import DbtCoreOperation

def trigger_dbt_flow() -> str:
    result = DbtCoreOperation(
        commands=["pwd", "dbt debug", "dbt run"],
    return result

if __name__ == "__main__":

Set up a new profile

To setup a new profile, first save and load a DbtCliProfile block and use it in DbtCoreOperation.

Then, specifyprofiles_dir where profiles.yml will be written. Here's example code with placeholders:

from prefect import flow
from prefect_dbt.cli import DbtCliProfile, DbtCoreOperation

def trigger_dbt_flow():
    dbt_cli_profile = DbtCliProfile.load("DBT-CORE-OPERATION-BLOCK-NAME-PLACEHOLDER")
    with DbtCoreOperation(
        commands=["dbt debug", "dbt run"],
    ) as dbt_operation:
        dbt_process = dbt_operation.trigger()
        # do other things before waiting for completion
        result = dbt_process.fetch_result()
    return result

if __name__ == "__main__":

Save credentials to a block

Blocks can be created through code or through the UI.

dbt Cloud

To create a dbt Cloud Credentials block do the following:

  1. Go to your dbt Cloud profile.
  2. Log in to your dbt Cloud account.
  3. Scroll to API or click API Access on the sidebar.
  4. Copy the API Key.
  5. Click Projects on the sidebar.
  6. Copy the account ID from the URL:<ACCOUNT_ID>.
  7. Create and run the following script, replacing the placeholders.
from import DbtCloudCredentials


Then, to create a dbt Cloud job block do the following:

  1. Navigate to your dbt home page.
  2. On the top nav bar, click on Deploy -> Jobs.
  3. Select a job.
  4. Copy the job ID from the URL:<ACCOUNT_ID>/projects/<PROJECT_ID>/jobs/<JOB_ID>
  5. Create and run the following script, replacing the placeholders.
from import DbtCloudCredentials, DbtCloudJob

dbt_cloud_credentials = DbtCloudCredentials.load("CREDENTIALS-BLOCK-NAME-PLACEHOLDER")
dbt_cloud_job = DbtCloudJob(

Load the saved block, which can access your credentials:

from import DbtCloudJob


dbt Core CLI

Available TargetConfigs blocks

Visit the API Reference to see other built-in TargetConfigs blocks.

If the desired service profile is not available, check out the Examples Catalog to see how you can build one from the generic TargetConfigs class.

To create dbt Core target config and profile blocks for BigQuery:

  1. Save and load a GcpCredentials block.
  2. Determine the schema / dataset you want to use in BigQuery.
  3. Create a short script, replacing the placeholders.
from prefect_gcp.credentials import GcpCredentials
from prefect_dbt.cli import BigQueryTargetConfigs, DbtCliProfile

credentials = GcpCredentials.load("CREDENTIALS-BLOCK-NAME-PLACEHOLDER")
target_configs = BigQueryTargetConfigs(
    schema="SCHEMA-NAME-PLACEHOLDER",  # also known as dataset

dbt_cli_profile = DbtCliProfile(

To create a dbt Core operation block:

  1. Determine the dbt commands you want to run.
  2. Create a short script, replacing the placeholders.
from prefect_dbt.cli import DbtCliProfile, DbtCoreOperation

dbt_cli_profile = DbtCliProfile.load("DBT-CLI-PROFILE-BLOCK-NAME-PLACEHOLDER")
dbt_core_operation = DbtCoreOperation(

Load the saved block that holds your credentials:

from import DbtCoreOperation



For assistance using dbt, consult the dbt documentation.

Refer to the prefect-dbt API documentation linked in the sidebar to explore all the capabilities of the prefect-dbt library.

Additional installation options

Additional installation options for dbt Core with BigQuery, Snowflake, and Postgres are shown below.

Additional functionality for dbt Core and Snowflake profiles

pip install -U "prefect-dbt[snowflake]"

Additional functionality for dbt Core and BigQuery profiles

pip install -U "prefect-dbt[bigquery]"

Additional functionality for dbt Core and Postgres profiles

pip install -U "prefect-dbt[postgres]"

Some dbt Core profiles require additional installation

According to dbt's Databricks setup page, users must first install the adapter:

pip install dbt-databricks

Check out the desired profile setup page on the sidebar for others.