How to Connect Postgres to dbt in PostgreSQL

Galaxy Glossary

How do I connect PostgreSQL to dbt using profiles.yml?

Configure dbt to talk to your PostgreSQL database by installing dbt-postgres, creating a profiles.yml entry, and validating with dbt debug.

Sign up for the latest in SQL knowledge from the Galaxy Team!
Welcome to the Galaxy, Guardian!
Oops! Something went wrong while submitting the form.

Description

What does connecting Postgres to dbt enable?

Connecting gives dbt permission to read from source tables like Customers or Orders and materialize models into target schemas such as analytics. You gain version-controlled transformations, incremental refreshes, and tests without leaving SQL.

Which prerequisites must be met?

Ensure PostgreSQL ≥9.6 is reachable, Python ≥3.8 is installed, and you have write access to the target schema. A git-initialized dbt project must exist in your workspace.

How do you install the dbt-postgres adapter?

Run pip install dbt-postgres --upgrade. This adds the Postgres adapter and the dbt CLI. Verify with dbt --version; the output should list “postgres” as an installed adapter.

How do you create profiles.yml?

dbt looks for ~/.dbt/profiles.yml. Each profile stores one or more outputs (targets). Use environment variables for secrets to avoid commits.

Sample profiles.yml for an ecommerce database

ecommerce_profile: target: dev outputs: dev: type: postgres host: localhost port: 5432 user: {{ env_var('DBT_USER') }} password: {{ env_var('DBT_PASSWORD') }} dbname: ecommerce schema: analytics threads: 4 keepalives_idle: 0 search_path: public

Where should environment variables be defined?

Define DBT_USER and DBT_PASSWORD in your shell or CI system. dbt resolves them at runtime, keeping plaintext secrets out of git.

How do you test the connection?

Execute dbt debug --target dev --profile ecommerce_profile. A green “Connection ok!” confirms credentials, network, and permissions are correct.

How do you run models once connected?

Create SQL models under models/. Then run dbt run --select customer_first_order. dbt compiles, connects to Postgres, and writes results into analytics.customer_first_order.

Best practices for secure, fast connections

Use a dedicated Postgres role with least-privilege grants. Pin adapter versions in requirements.txt. Limit threads to available CPU cores. Monitor connections with pg_stat_activity.

What are common mistakes?

Wrong profile name or target causes “target not found” errors. Missing grants for CREATE or USAGE blocks model materialization. Fix by aligning names and running GRANT statements.

Why How to Connect Postgres to dbt in PostgreSQL is important

How to Connect Postgres to dbt in PostgreSQL Example Usage


# Bash
export DBT_USER=developer
export DBT_PASSWORD=secret

dbt debug --profile ecommerce_profile --target dev

dbt run --select customer_first_order

-- models/customer_first_order.sql
SELECT
  c.id             AS customer_id,
  MIN(o.order_date) AS first_order_date
FROM  c
JOIN    o ON o.customer_id = c.id
GROUP BY c.id;

How to Connect Postgres to dbt in PostgreSQL Syntax


pip install dbt-postgres

~/.dbt/profiles.yml
--------------------
ecommerce_profile:
  target: dev
  outputs:
    dev:
      type: postgres            # Adapter
      host: localhost           # DB host
      port: 5432                # DB port
      user: 
      password: 
      dbname: ecommerce         # Database name
      schema: analytics         # Target schema for models
      threads: 4                # Parallelism
      keepalives_idle: 0        # Connection keepalive
      search_path: public       # Optional search path

Common Mistakes

Frequently Asked Questions (FAQs)

Do I need superuser privileges?

No. A role with USAGE on the source schemas and CREATE on the target schema is enough.

Can I connect through an SSH tunnel?

Yes. Set host: 127.0.0.1 and start an SSH tunnel locally, or use the ssh_tunnel: config in dbt 1.6+.

How do I switch between dev and prod databases?

Create separate outputs (dev, prod) in the same profile and pass --target prod when running dbt.

Want to learn about other SQL terms?

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie
BauHealth Logo
Truvideo Logo
Welcome to the Galaxy, Guardian!
Oops! Something went wrong while submitting the form.