Skip to main content
Hooks in dbt allow you to execute SQL statements or custom logic at specific points in your dbt workflow. They’re powerful tools for automating operational tasks, managing permissions, and enhancing your data pipeline with custom functionality.

What Are Hooks?

Hooks are snippets of SQL that run at predefined moments during the dbt execution process. They help automate operational tasks like:
  • Granting permissions on objects
  • Setting table properties or attributes
  • Adding comments or metadata
  • Creating indexes or optimizing data structures
  • Executing database-specific operations
There are four main types of hooks in dbt:

Using Model-Level Hooks

Pre-hooks and Post-hooks You can define pre-hooks and post-hooks directly in your model SQL files using the config() function:
In this example:
  • The pre-hook deletes invalid records before the model builds
  • The post-hook grants select permissions after the model is created
Multiple Hooks You can specify multiple hooks as a list:

Project and Folder-Level Hooks

You can configure hooks that apply to groups of models in your dbt_project.yml file:
On-Run-Start and On-Run-End Hooks These hooks run once at the beginning or end of your dbt run:
Hook Context VariablesIn hooks, you can access several useful context variables:
  • {{ this }} - The relation being built (table/view)
  • {{ target }} - Information about the current target database
  • {{ run_started_at }} - Timestamp when the run started
  • {{ invocation_id }} - Unique ID for the current dbt run

Using Macros in Hooks

You can make your hooks more reusable by calling macros:
With a corresponding macro defined:
This approach allows you to centralize and reuse your hook logic across multiple models. You can also configure hooks with macros in your YAML files:

Common Hook Use Cases

Permission Management A common use for hooks is automating permission grants:
Database-Specific Operations Perform operations specific to your data warehouse:
Environment-Specific Actions Apply different hooks based on your deployment environment:

Operations with run-operation

Operations are a way to execute standalone macros using the run-operation command. This is useful for administrative tasks that you want to run on demand, rather than as part of a model build. Creating an Operation Macro To create an operation, define a macro that performs the desired actions:
Note two important points:
  1. The SQL is defined within a {% set sql %} block
  2. The run_query() function is used to actually execute the SQL
Running an Operation To run this operation from the command line:
This would grant select privileges on all tables in your schema to the ‘reporter’ role.
Key Difference Between Hooks and Operations
  • Hooks are automatically executed at specific times during dbt runs
  • Operations are explicitly run on-demand using the run-operation command
  • With operations, you must use run_query() or a statement block to execute the SQL

Operation Examples

Refreshing a Snowflake Pipe
Creating Multiple Objects
Passing Complex Arguments You can pass complex arguments to operations:
With a corresponding macro:

Best Practices

Hooks

Operations

When to Use Hooks vs. OperationsUse hooks when you need to:
  • Execute SQL automatically at specific points in your dbt workflow
  • Apply consistent actions across multiple models
  • Implement pre/post processing that’s tightly coupled to models
Use operations when you need to:
  • Run administrative tasks on-demand
  • Perform one-off database maintenance
  • Execute complex logic that doesn’t fit into the model build process
  • Create setup/teardown scripts for your environment
By mastering hooks and operations, you can significantly extend dbt’s capabilities and automate many aspects of database administration and data pipeline management.