What are dbt Packages and how to use them

If you’re all about saving time and reusing pre-made code, then dbt Packages are your best friends. These are open-source collections of macros (basically reusable SQL snippets) that you can plug straight into your project — kind of like using libraries in Python or macros in Alteryx.

Using packages in dbt usually comes down to three simple steps:

    • Find the right package
    • Install it in your project
    • Use it in your models or macros

Let’s go through each of these steps.


Types of Packages

There are loads of packages out there, covering everything from testing to SQL generation and helper macros. Here are a few examples:

Generic tests (help you check data quality)

    • not_empty_string
    • not_accepted_values
    • equal_rowcount

SQL generators (help you write cleaner SQL)

    • group_by
    • union_relations
    • generate_surrogate_key

...and many more across categories like introspective macros, cross-database utilities, Jinja helpers, and even custom materializations.


Where to Find dbt Packages

All official and community-made packages live on the dbt Hub: https://hub.getdbt.com/

Quick fact check: Not all packages on dbt Hub are made by dbt Labs. Many are community-built but published there for easy access. However, dbt Labs does maintain some of the most popular ones, like dbt_utils.

Personally, I’ve found dbt_utils to be a great starting point — it includes a bunch of really handy macros for everyday tasks.

In this example, I’ll use one of my favourites from that package: generate_surrogate_key, which creates a unique ID based on one or more columns. This is especially useful if your dataset doesn’t already have an ID field.


How to Install a Package

To install a package, create (or update) a file called packages.yml in your project’s root folder — or wherever your team’s best practice suggests.

Then, copy in the code snippet from the dbt_utils page. For example, to install dbt_utils, add this:

Once that’s saved, run dbt deps in your terminal (the same place you’d usually run dbt run or dbt build)


Step 3: Use the Package in Your Code

Every package comes with its own documentation that shows how to call its macros. For example, the docs for generate_surrogate_key can be found in this link.

The syntax looks like this:

So if you wanted to create an ID using the year and name columns, you could write:

{{ dbt_utils.generate_surrogate_key(['year', 'name']) }} AS athlete_stats_id

Here is an example of me using it in a project:

That’s all there is to it. You’ve just used a pre-built dbt macro to create a unique key — no need to reinvent the wheel.


Wrap-Up

dbt Packages are a massive time-saver. They help you write cleaner, reusable SQL and follow best practices without starting from scratch each time. Once you get the hang of using them, you’ll start spotting opportunities everywhere — whether it’s testing, transformations, or automation.

If you haven’t already, go explore https://hub.getdbt.com/ and try out a few packages in your next project. You’ll be amazed at how much they simplify your workflow.

Author:
Rosh Khan
Powered by The Information Lab
1st Floor, 25 Watling Street, London, EC4M 9BR
Subscribe
to our Newsletter
Get the lastest news about The Data School and application tips
Subscribe now
© 2025 The Information Lab