0
0
dbtdata~5 mins

Creating your own dbt package

Choose your learning style9 modes available
Introduction

Creating your own dbt package helps you share and reuse data transformation code easily across projects.

You want to reuse common SQL models in multiple dbt projects.
You want to share your data transformation logic with your team.
You want to organize your dbt code into smaller, manageable parts.
You want to publish your dbt models as a package for others to use.
You want to maintain consistent data transformations across different environments.
Syntax
dbt
1. Create a new directory for your package.
2. Add a dbt_project.yml file with package details.
3. Add your models, macros, and tests inside the package folder.
4. In your main project, add the package to packages.yml.
5. Run `dbt deps` to install the package.
6. Use the package models and macros in your project.

The dbt_project.yml file defines your package name and version.

The packages.yml file in your main project lists all external packages to use.

Examples
This file sets the package name and default model materialization.
dbt
# dbt_project.yml in your package folder
name: my_dbt_package
version: 0.1.0
config-version: 2

models:
  my_dbt_package:
    +materialized: view
This tells your main project to use the local package you created.
dbt
# packages.yml in your main project
packages:
  - local: ../my_dbt_package
This shows how to reference a model from your package.
dbt
# Using a model from the package in your main project
select * from {{ ref('my_dbt_package.example_model') }}
Sample Program

This example shows how to create a simple dbt package with one model and use it in another project.

dbt
# Step 1: Create package folder 'my_dbt_package'
# Step 2: Add dbt_project.yml inside 'my_dbt_package'
# Content:
# name: my_dbt_package
# version: 0.1.0
# config-version: 2
# models:
#   my_dbt_package:
#     +materialized: view

# Step 3: Add a model file 'models/example_model.sql' inside 'my_dbt_package'
# Content:
# select 1 as id, 'hello' as greeting

# Step 4: In your main dbt project, add packages.yml:
# packages:
#   - local: ../my_dbt_package

# Step 5: Run dbt deps to install the package
# $ dbt deps

# Step 6: Use the package model in your main project model:
# select * from {{ ref('my_dbt_package.example_model') }}

# For demonstration, simulate the output of running the package model:
print('id | greeting')
print('1  | hello')
OutputSuccess
Important Notes

Always keep your package version updated in dbt_project.yml for clarity.

Use dbt deps to install or update packages after changing packages.yml.

You can publish your package to a git repository for sharing with others.

Summary

Creating a dbt package helps reuse and share data models easily.

Define your package with dbt_project.yml and add it to your main project via packages.yml.

Use dbt deps to install packages and ref() to use package models.