Creating your own dbt package helps you share and reuse data transformation code easily across projects.
Creating your own dbt package
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.
# dbt_project.yml in your package folder name: my_dbt_package version: 0.1.0 config-version: 2 models: my_dbt_package: +materialized: view
# packages.yml in your main project
packages:
- local: ../my_dbt_package# Using a model from the package in your main project select * from {{ ref('my_dbt_package.example_model') }}
This example shows how to create a simple dbt package with one model and use it in another project.
# 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')
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.
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.
Practice
Solution
Step 1: Understand the role of dbt packages
dbt packages are designed to bundle data models and logic for reuse.Step 2: Identify the main benefit
Sharing and reusing models saves time and keeps projects consistent.Final Answer:
To reuse and share data models easily across projects -> Option AQuick Check:
dbt package purpose = reuse/share models [OK]
- Confusing packages with dashboards
- Thinking packages store raw data
- Believing packages speed up SQL writing only
Solution
Step 1: Recall where packages are declared
dbt usespackages.ymlto list external or local packages.Step 2: Differentiate from other config files
dbt_project.ymlconfigures the project itself, not packages.Final Answer:
packages.yml -> Option DQuick Check:
Package list file = packages.yml [OK]
- Adding packages in dbt_project.yml
- Confusing profiles.yml with packages.yml
- Trying to add packages in SQL files
packages.yml snippet:packages: - local: ../my_dbt_package
What command installs this package into your project?
Solution
Step 1: Identify the command for package installation
dbt usesdbt depsto download and install packages listed inpackages.yml.Step 2: Differentiate from other commands
dbt runruns models,dbt installis not a valid command.Final Answer:
dbt deps -> Option CQuick Check:
Install packages with dbt deps [OK]
- Using dbt install (not a valid command)
- Confusing dbt run with package install
- Trying dbt build instead of dbt deps
dbt deps, you get an error: "Could not find package at path '../my_package'". What is the likely cause?Solution
Step 1: Understand the error message
"Could not find package at path" means dbt cannot locate the folder specified.Step 2: Check the path in packages.yml
Relative paths must be correct and folder must exist for local packages.Final Answer:
The relative path in packages.yml is incorrect or folder missing -> Option AQuick Check:
Path error = wrong folder path [OK]
- Running dbt run before dbt deps
- Editing dbt_project.yml instead of packages.yml
- Deleting unrelated config files
customer_summary from your custom package my_package. Which is the correct way to reference it in your SQL model?Solution
Step 1: Recall how to reference models from packages
Useref('package_name.model_name')to refer to models in packages.Step 2: Check each option's syntax
select * from {{ ref('my_package.customer_summary') }} uses the correct single-argumentrefsyntax with dot notation for packages.Final Answer:
select * from {{ ref('my_package.customer_summary') }} -> Option BQuick Check:
Package model ref = ref('package.model') [OK]
- Using ref('package', 'model') syntax (that's for sources)
- Forgetting the package name in ref
- Using double underscore instead of dot
Ready to prove it?
Pass the test and this pattern moves unknown
