dbt-snowflake

1.11.4 · active · verified Thu Apr 09

dbt-snowflake is the official adapter plugin for dbt (data build tool), enabling users to define, manage, and run data transformations against a Snowflake data warehouse. It extends dbt-core with Snowflake-specific SQL dialect, connection management, and materialization strategies. Current version is 1.11.4, and it is typically released in lockstep with dbt-core major and minor versions, meaning a quarterly or bi-annual cadence.

Warnings

Install

Imports

Quickstart

This Python script generates a minimal `profiles.yml` file in your `~/.dbt/` directory, configured for Snowflake using environment variables for credentials. After running this script, follow the printed instructions to initialize a dbt project, link it to the generated profile, and run `dbt debug` to verify your Snowflake connection. Remember to set the required Snowflake environment variables (e.g., `SNOWFLAKE_ACCOUNT`, `SNOWFLAKE_USER`, `SNOWFLAKE_PASSWORD`) or replace placeholders.

import os
from pathlib import Path

# Define minimal profiles.yml content using environment variables.
# Make sure to set SNOWFLAKE_ACCOUNT, SNOWFLAKE_USER, SNOWFLAKE_PASSWORD,
# SNOWFLAKE_ROLE, SNOWFLAKE_WAREHOUSE, SNOWFLAKE_DATABASE, SNOWFLAKE_SCHEMA
# in your environment before running this, or replace the placeholders directly.
profiles_yml_content = f"""
dbt_snowflake_example:
  target: dev
  outputs:
    dev:
      type: snowflake
      account: {os.environ.get('SNOWFLAKE_ACCOUNT', 'your_account.snowflakecomputing.com')}
      user: {os.environ.get('SNOWFLAKE_USER', 'your_snowflake_user')}
      password: {os.environ.get('SNOWFLAKE_PASSWORD', 'your_snowflake_password')}
      role: {os.environ.get('SNOWFLAKE_ROLE', 'SYSADMIN')}
      warehouse: {os.environ.get('SNOWFLAKE_WAREHOUSE', 'COMPUTE_WH')}
      database: {os.environ.get('SNOWFLAKE_DATABASE', 'DBT_DEV_DB')}
      schema: {os.environ.get('SNOWFLAKE_SCHEMA', 'PUBLIC')}
      threads: 1
      client_session_keep_alive: false
"""

# Ensure ~/.dbt directory exists and write profiles.yml
dbt_dir = Path.home() / ".dbt"
dbt_dir.mkdir(parents=True, exist_ok=True)
profiles_yml_path = dbt_dir / "profiles.yml"
with open(profiles_yml_path, "w") as f:
    f.write(profiles_yml_content)
print(f"Generated a sample profiles.yml at: {profiles_yml_path}")

print("\nNext steps:")
print("1. Create a dbt project: `dbt init my_snowflake_project`")
print("2. Update `my_snowflake_project/dbt_project.yml` to use `profile: dbt_snowflake_example`")
print("3. Navigate into your project directory: `cd my_snowflake_project`")
print("4. Verify your connection: `dbt debug`")
print("   Ensure you have configured environment variables or replaced placeholders.")

# Uncomment the following to directly run dbt debug if you have dbt-core installed
# import subprocess
# try:
#     print("\nAttempting to run 'dbt debug' to verify connection...")
#     # This assumes you have created 'my_snowflake_project' and updated its profile
#     # For a full quickstart, this requires more setup than a single snippet can provide.
#     # subprocess.run(["dbt", "debug", "--profile", "dbt_snowflake_example"], check=True)
#     # print("dbt debug completed successfully.")
# except FileNotFoundError:
#     print("Error: 'dbt' command not found. Install dbt-core via `pip install dbt-core`.")
# except subprocess.CalledProcessError as e:
#     print(f"Error during 'dbt debug': {e}")
#     print("Check your profiles.yml and Snowflake credentials.")

view raw JSON →