jusstol
08/18/2025, 9:55 AMMarvin
08/18/2025, 9:55 AMMarvin
08/18/2025, 9:58 AMuv pip install "prefect-dbt[cli]" dbt-core dbt-postgres
…replace dbt-postgres
with your adapter.
2) Flow that runs dbt
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)
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.)
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
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?