How to Connect BigQuery to dbt

Galaxy Glossary

How do I connect dbt to BigQuery in minutes?

Configure dbt’s profiles.yml so your models run in Google BigQuery with secure, reusable credentials.

Sign up for the latest in SQL knowledge from the Galaxy Team!
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Description

Table of Contents

Why connect BigQuery and dbt?

Running dbt on BigQuery lets teams version-control SQL, manage data tests, and deploy cost-efficient ELT pipelines without maintaining servers.

What credentials does dbt need?

dbt authenticates with a Google Cloud service-account JSON key that has at least BigQuery Data Editor and BigQuery Job User roles on the target project.

How do I create a service-account key?

In Google Cloud Console, create a new service account → grant BigQuery roles → generate a key → download the JSON file. Store it in a secure path or inject via an environment variable.

Where do I place the key for local runs?

Save the key file outside your repository, e.g., ~/.gcp/galaxy-bq-key.json. Set GOOGLE_APPLICATION_CREDENTIALS to that path so dbt and other tools find it automatically.

What does a BigQuery profile look like?

Add a bigquery target in ~/.dbt/profiles.yml. Specify project, dataset, location, and threads. Use method: service-account plus the key file path or rely on the env var.

Sample profiles.yml

galaxy_bq:
target: dev
outputs:
dev:
type: bigquery
method: service-account # or oauth
keyfile: ~/.gcp/galaxy-bq-key.json
project: galaxy-data-dev
dataset: analytics
threads: 4
location: US
timeout_seconds: 300

How do I test the connection?

Run dbt debug --target dev. A successful connection shows green checks for credentials, project access, and dataset reachability.

How do I model e-commerce tables?

Create a model like models/customer_ltv.sql that joins Customers, Orders, and OrderItems. Run dbt run to materialize it in BigQuery.

Example model

with o as (
select customer_id, sum(total_amount) as lifetime_value
from {{ ref('Orders') }}
group by customer_id
)
select c.id, c.name, c.email, o.lifetime_value
from {{ ref('Customers') }} c
left join o on c.id = o.customer_id;

Best practices for BigQuery + dbt

Set batch_size to control INSERT limits, use cost controls in GCP budgets, and leverage dbt incremental models to minimize slot usage.

Why How to Connect BigQuery to dbt is important

How to Connect BigQuery to dbt Example Usage


/* customers_by_region.sql (dbt model) */
select
  c.id,
  c.name,
  c.email,
  r.region,
  count(o.id) as orders,
  sum(o.total_amount) as revenue
from  c
left join  o on c.id = o.customer_id
left join `.public.regions` r on c.region_id = r.id
group by 1,2,3,4;

How to Connect BigQuery to dbt Syntax


profiles.yml BigQuery target:

galaxy_bq:
  target: dev
  outputs:
    &dev_output
    dev:
      type: bigquery              # adapter name
      method: service-account     # oauth | service-account
      keyfile: ~/.gcp/key.json    # omit if using env var
      project: my-gcp-project     # GCP project ID
      dataset: analytics          # default dataset
      threads: 4                  # parallel threads
      location: US                # multi-region or region
      timeout_seconds: 300        # job timeout
      priority: interactive       # interactive | batch
      retries: 1                  # job retries

Use variables in <code>profiles.yml</code> or environment variables, e.g., <code></code>, for CI/CD flexibility.

Common Mistakes

Frequently Asked Questions (FAQs)

Can I use OAuth instead of a key file?

Yes, set method: oauth and omit keyfile. Use for local development; switch to service accounts in automation.

How do I separate dev and prod targets?

Create multiple outputs (dev, prod) in profiles.yml, each pointing to its own project and dataset. Set dbt run --target prod in CI.

Does dbt support BigQuery incremental models?

Absolutely. Use {{ config(materialized='incremental') }} and define a unique key or timestamp. dbt will append or merge new rows efficiently.

Want to learn about other SQL terms?

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.