How to Integrate PostgreSQL with dbt

Galaxy Glossary

How do I connect dbt to PostgreSQL and run models?

Connect dbt to PostgreSQL so you can version-control, test, and document SQL transformations in a repeatable workflow.

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

Why integrate PostgreSQL with dbt?

Pairing dbt with PostgreSQL turns raw tables into governed, version-controlled models. You keep SQL in Git, schedule repeatable builds, and surface trusted data for dashboards without moving data out of Postgres.

What prerequisites should I check?

Verify PostgreSQL is reachable, install dbt-postgres (pip install dbt-postgres), and create a service account with CREATE and USAGE on your target schema.

How do I configure profiles.yml for Postgres?

Create ~/.dbt/profiles.yml with a Postgres target. The profile name must match profile: in dbt_project.yml.

profiles.yml template

ecommerce:
target: dev
outputs:
dev:
type: postgres
host: localhost
user: analytics
password: {{ env_var('PG_PASSWORD') }}
port: 5432
dbname: warehouse
schema: analytics
threads: 4
keepalives_idle: 0

How do I run my first dbt model against Postgres?

Place a model file such as models/orders_enriched.sql in your dbt project, then run dbt run. dbt compiles the model and creates the target table or view in analytics.orders_enriched.

Sample model

-- models/orders_enriched.sql
select o.id as order_id,
c.name as customer_name,
o.order_date,
o.total_amount,
date_part('dow', o.order_date) as weekday
from {{ ref('orders') }} o
join {{ ref('customers') }} c on c.id = o.customer_id;

How can I test and document my Postgres models?

Add YAML tests and descriptions. Run dbt test to validate uniqueness or foreign keys inside Postgres, and generate docs with dbt docs generate.

Best practices for Postgres + dbt

Use incremental models for large tables, apply grants: in dbt_project.yml for least privilege, and pin Postgres extensions in a pre-hook so deployments stay idempotent.

Common errors and quick fixes

psycopg2.OperationalError: confirm the host, port, and SSL settings. permission denied for schema: grant USAGE and CREATE on the target schema to the dbt role.

Full workflow example

1) Seed reference data with dbt seed. 2) Build models with dbt run --select orders_enriched+. 3) Run tests. 4) Schedule nightly runs through Cron or Airflow for fresh Postgres insights.

Why How to Integrate PostgreSQL with dbt is important

How to Integrate PostgreSQL with dbt Example Usage


-- dbt model: models/top_products.sql
select p.id,
       p.name,
       sum(oi.quantity) as total_units_sold,
       sum(oi.quantity * p.price) as total_revenue
from  p
join  oi on oi.product_id = p.id
group by p.id, p.name
order by total_revenue desc
limit 20;

How to Integrate PostgreSQL with dbt Syntax


# profiles.yml syntax for PostgreSQL
ecommerce:
  target: dev
  outputs:
    dev:
      type: postgres           # required
      host: <host>             # e.g. localhost or prod-db.company.com
      user: <username>
      password: <password>     # or env_var()
      port: 5432               # default Postgres port
      dbname: <database>
      schema: <target_schema>  # where dbt creates models
      threads: 1-8             # parallelism
      search_path: public,analytics  # optional
      keepalives_idle: 0          # optional

# CLI commands in a Postgres context
$ dbt debug                       # validate connection
$ dbt run                         # build all models
$ dbt run --models customers+     # build graph starting at customers
$ dbt test                        # execute data tests
$ dbt seed                        # load CSVs into Postgres
$ dbt docs generate && dbt docs serve

Common Mistakes

Frequently Asked Questions (FAQs)

Does dbt support SSL when connecting to PostgreSQL?

Yes. Add sslmode: require and related parameters under your Postgres output. dbt passes them to psycopg2.

Can I run dbt incrementally on Postgres?

Absolutely. Add materialized='incremental' to your model, define a unique_key, and dbt will merge only new or changed rows.

How do I deploy dbt-Postgres to production?

Store profiles.yml variables in a CI secret store, run dbt run in a container, and point the profile to your production Postgres host.

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.