The ibm-dbt-db2 adapter allows dbt to work with IBM Db2 databases. This adapter uses the ibm_db Python driver to connect to Db2 databases.
- ✅ Full dbt support for IBM Db2
- ✅ Table and view materializations
- ✅ Incremental models (merge and delete+insert strategies)
- ✅ Seeds
- ✅ Snapshots
- ✅ Tests and documentation
- ✅ Grants management
- Python 3.10 - 3.12 (Python 3.13+ not yet tested; Python 3.9 not supported due to dbt-core 1.11+ requirements)
- dbt-core ~= 1.11.0
- ibm_db == 3.2.8
- IBM Db2 database (LUW, z/OS, or iSeries)
git clone <repository-url>
cd db2-dbt
pip install -e .pip install ibm-dbt-db2Configure your Db2 connection in ~/.dbt/profiles.yml:
my_db2_project:
outputs:
dev:
type: db2
host: your-db2-host
port: 50000 # Default Db2 port
database: your_database
schema: your_schema
username: your_username
password: your_password
threads: 4
target: devFor a complete example with all available options including SSL/TLS configuration, see profiles.yml.example.
| Parameter | Required | Default | Description |
|---|---|---|---|
type |
Yes | - | Must be db2 |
host |
Yes* | - | Db2 server hostname |
port |
No | 50000 | Db2 server port |
database |
Yes | - | Database name |
schema |
Yes | - | Schema name |
username |
Yes | - | Db2 username |
password |
Yes | - | Db2 password |
threads |
No | 1 | Number of threads for parallel execution |
*Not required if using dsn
| Parameter | Required | Default | Description |
|---|---|---|---|
security |
No | - | Security protocol (use SSL to enable SSL/TLS) |
ssl_server_certificate |
No | - | Path to server CA certificate file |
ssl_client_keystore |
No | - | Path to client keystore database (.kdb file) |
ssl_client_keystash |
No | - | Path to client keystash file (.sth file) |
ssl_client_hostname_validation |
No | - | Enable hostname verification (true/false) |
retries |
No | 1 | Number of connection retry attempts |
my_db2_project:
outputs:
prod:
type: db2
host: secure-db2.example.com
port: 50001
database: PRODDB
schema: ANALYTICS
username: prod_user
password: prod_password
threads: 8
# SSL/TLS settings
security: SSL
ssl_server_certificate: /path/to/server-ca.crt
ssl_client_hostname_validation: true
retries: 3
target: prodAlternatively, you can use a DSN (Data Source Name):
my_db2_project:
outputs:
dev:
type: db2
dsn: MY_Db2_DSN
username: your_username
password: your_password
schema: your_schema
threads: 4
target: devDb2 uppercases unquoted identifiers by default. The adapter handles this automatically, but be aware:
- Unquoted table/column names will be uppercased
- Use quotes in your SQL to preserve case:
"MyTable"vsMYTABLE
The adapter maps dbt data types to Db2 types:
| dbt Type | Db2 Type |
|---|---|
| string | VARCHAR |
| text | VARCHAR(max_length) |
| integer | INTEGER |
| bigint | BIGINT |
| float | FLOAT |
| numeric | DECIMAL |
| boolean | BOOLEAN |
| timestamp | TIMESTAMP |
| date | DATE |
| time | TIME |
Supported incremental strategies:
- merge (default) - Uses MERGE statement
- delete+insert - Deletes matching records then inserts
Example:
{{
config(
materialized='incremental',
unique_key='id',
incremental_strategy='merge'
)
}}
SELECT * FROM source_table
{% if is_incremental() %}
WHERE updated_at > (SELECT MAX(updated_at) FROM {{ this }})
{% endif %}dbt init my_db2_projectEdit ~/.dbt/profiles.yml with your Db2 connection details.
cd my_db2_project
dbt debugCreate SQL files in the models/ directory:
-- models/my_model.sql
SELECT
customer_id,
customer_name,
order_date
FROM {{ source('raw', 'orders') }}
WHERE order_date >= CURRENT_DATE - 30 DAYSdbt rundbt test# Run all models
dbt run
# Run specific model
dbt run --select my_model
# Run models and downstream dependencies
dbt run --select my_model+
# Test all models
dbt test
# Generate documentation
dbt docs generate
# Serve documentation
dbt docs serve
# Create snapshots
dbt snapshot
# Load seed data
dbt seed| Feature | Supported |
|---|---|
| Table materialization | ✅ Yes |
| View materialization | ✅ Yes |
| Incremental materialization | ✅ Yes |
| Ephemeral materialization | ✅ Yes |
| Seeds | ✅ Yes |
| Snapshots | ✅ Yes |
| Tests | ✅ Yes |
| Documentation | ✅ Yes |
| Sources | ✅ Yes |
| Custom schemas | ✅ Yes |
| Grants | ✅ Yes |
| Constraints |
-
Verify Db2 is accessible:
db2 connect to your_database user your_username
-
Check firewall/network: Ensure port 50000 (or your custom port) is open
-
Verify credentials: Ensure username/password are correct
This adapter requires Python 3.10 or higher due to dbt-core 1.11+ dependencies requiring dbt-common~=1.37 and dbt-adapters~=1.15, which both require Python 3.10+.
Supported versions: Python 3.10, 3.11, 3.12 Not supported: Python 3.9 (use older dbt-core versions), Python 3.13+ (not yet tested)
If ibm_db installation fails:
# On macOS
brew install gcc
# On Linux
sudo apt-get install python3-dev gcc
# Then reinstall
pip install ibm_db==3.2.8- Python Version:
- Requires Python 3.10+ (dbt-core 1.11+ dependency requirement)
- Python 3.13+ not yet tested
- Constraints: CHECK, UNIQUE, PRIMARY KEY, and FOREIGN KEY constraints are defined but not enforced by Db2 in dbt context
- LISTAGG limit_num: Db2's LISTAGG function does not support limiting the number of aggregated values
To build the wheel file for development:
# Install build dependencies
pip install build
# Build the wheel
python -m build
# The wheel file will be in dist/The project uses pytest for testing with support for multiple Python versions and platforms:
# Install test dependencies
pip install -e ".[dev]"
# Run unit tests
pytest tests/unit -v
# Run functional tests (requires Db2 connection)
pytest tests/functional -v
# Run all tests
pytest tests/ -vThe project uses flake8 for linting:
# Run linting
flake8 dbt/ tests/
# Auto-fix some issues with pre-commit
pre-commit run --all-filesThe project uses GitHub Actions for CI/CD:
- Triggers: Push to main, pull requests
- Testing Matrix:
- OS: Ubuntu, macOS
- Python: 3.10, 3.11, 3.12
- Jobs: Lint (flake8) and test across all combinations
- Note: Windows is excluded due to ibm_db DLL dependency issues in CI environments
- Triggers: Manual workflow dispatch or GitHub release
- Process:
- Build: Creates wheel and validates with
twine check - Test Install: Verifies installation on Ubuntu and macOS with Python 3.10 and 3.12
- Publish: Deploys to Test PyPI or PyPI based on selection
- Build: Creates wheel and validates with
When you create and push a version tag, the workflow automatically triggers and waits for approval:
-
Create and push a version tag:
git tag v1.0.16 git push origin v1.0.16
-
Workflow automatically triggers:
- Builds the package
- Runs tests on multiple platforms
- Pauses and waits for deployment approval
-
Choose deployment target:
- Go to Actions tab → Select the running workflow
- Click "Review deployments"
- Select environment:
- ✅ test-pypi - For testing (https://test.pypi.org/p/ibm-dbt-db2)
- ✅ pypi - For production (https://pypi.org/p/ibm-dbt-db2)
- Click "Approve and deploy"
-
Only authorized users can approve:
shubhamkapoor992amitkumar293
For manual releases with full control:
- Go to Actions tab in GitHub
- Select "Build and Publish Release" workflow
- Click "Run workflow"
- Configure the release:
- Use workflow from: Select branch (usually
main) - Where to publish? Choose:
test-pypi- For testingpypi- For production
- Git ref (tag/branch) to release from: (Optional)
- Leave empty to use current branch
- Or enter a tag:
v1.0.16 - Or enter a branch:
main
- Use workflow from: Select branch (usually
- Click "Run workflow"
- Workflow will pause for approval (only authorized users can approve)
Examples:
- Release from current main branch: Leave "Git ref" empty
- Release from specific tag: Enter
v1.0.16in "Git ref" - Release from feature branch: Enter
feature-branchin "Git ref"
To test the package before production release:
pip install --index-url https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple/ ibm-dbt-db2Required: Configure GitHub environments for deployment protection:
-
Go to Repository Settings → Environments
-
Create
test-pypienvironment:- Click "New environment"
- Name:
test-pypi - Deployment protection rules:
- ✅ Enable "Required reviewers"
- Add reviewers:
shubhamkapoor992,amitkumar293
- Deployment branches and tags:
- Select "Selected branches and tags"
- Add rule:
main(restricts releases to main branch) - Add rule:
v*(allows version tags like v1.0.16)
-
Create
pypienvironment:- Same steps as above
- This protects production releases
What this does:
- ✅ Workflow pauses before publishing and requires approval
- ✅ Only authorized reviewers can approve deployments
- ✅ Provides audit trail of who approved each release
- ✅ Prevents unauthorized or accidental releases
Contributions are welcome! Please:
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests (unit and/or functional)
- Ensure all tests pass and code passes linting
- Submit a pull request
All pull requests will automatically run:
- Linting checks (flake8)
- Unit tests across multiple Python versions and platforms
- Code quality validation
[Add your license here]
For issues and questions:
- GitHub Issues: https://github.com/IBM/db2-dbt/issues
- dbt Community: https://community.getdbt.com/
- Migrated to modern
pyproject.tomlpackaging (PEP 517/518/621) - Updated to dbt-core ~1.11.0
- Updated ibm_db to 3.2.8
- Breaking: Dropped Python 3.9 support (requires Python 3.10+)
- Tested on Python 3.10, 3.11, 3.12
- Fixed 27 Flake8 linting errors
- Improved unit test coverage (37 passing, 8 skipped)
- Updated to strict code quality standards
- Modernized CI/CD workflows
- Security updates (PyYAML 6.0.3+, flake8 7.3)