<@ULVA73B9P> hi, how can i schedule dbt run jobs w...
# ask-marvin
j
@Marvin hi, how can i schedule dbt run jobs with prefect ?
m
thought for 185.3 seconds
Short answer: wrap your dbt run in a Prefect flow, deploy it with a schedule, and point a worker at the work pool. Slightly longer answer below (Prefect 3.x assumed). Option A — dbt Core (CLI) 1) Install
Copy code
uv pip install "prefect-dbt[cli]" dbt-core dbt-postgres
…replace
dbt-postgres
with your adapter. 2) Flow that runs dbt
Copy code
from prefect import flow
from prefect_dbt.cli.commands import trigger_dbt_cli_command

@flow(name="dbt-build")
def dbt_build():
    # Keep the CLI flags in the command string so there’s no ambiguity
    return trigger_dbt_cli_command(
        "dbt build -s marts.* --profiles-dir ~/.dbt --target prod",
        cwd="/path/to/your/dbt/project",
    )
3) Deploy with a schedule (code-first)
Copy code
from prefect.schedules import Cron

dbt_build.from_source(
    source="<https://github.com/your-org/your-repo>",
    entrypoint="flows/dbt_build.py:dbt_build",
).deploy(
    name="dbt-nightly",
    work_pool_name="prod-pool",
    schedule=Cron("0 2 * * *", timezone="UTC"),
)
4) Start a worker on your infra (Process, Docker, K8s, etc.)
Copy code
prefect work-pool create prod-pool
prefect worker start -p prod-pool
Notes - Prefer a container image that already has dbt + your adapter installed (and your
profiles.yml
or env vars). - If you don’t want to manage
profiles.yml
, you can use the
DbtCliProfile
block from
prefect-dbt
to generate one dynamically. Option B — dbt Cloud
Copy code
from prefect import flow
from prefect_dbt.cloud.credentials import DbtCloudCredentials
from <http://prefect_dbt.cloud.jobs|prefect_dbt.cloud.jobs> import run_dbt_cloud_job

@flow(name="dbt-cloud-job")
def run_cloud_job():
    creds = DbtCloudCredentials(account_id=12345, api_key="***")  # or load from a Prefect block
    return run_dbt_cloud_job(job_id=67890, credentials=creds, wait_for_completion=True)
Deploy and schedule the flow just like above. Docs and examples - prefect-dbt collection - schedules in Prefect 3 - dbt + Prefect overview Want me to tailor this to your setup? Are you using dbt Core or dbt Cloud, and what worker type (process, Docker, Kubernetes) are you running?