Configure dbt to talk to your PostgreSQL database by installing dbt-postgres, creating a profiles.yml entry, and validating with dbt debug.
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.
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.
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.
dbt looks for ~/.dbt/profiles.yml
. Each profile stores one or more outputs (targets). Use environment variables for secrets to avoid commits.
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
Define DBT_USER
and DBT_PASSWORD
in your shell or CI system. dbt resolves them at runtime, keeping plaintext secrets out of git.
Execute dbt debug --target dev --profile ecommerce_profile
. A green [32m“Connection ok!”[0m confirms credentials, network, and permissions are correct.
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
.
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
.
Wrong profile name or target causes [31m“target not found”[0m errors. Missing grants for CREATE or USAGE blocks model materialization. Fix by aligning names and running GRANT statements.
No. A role with USAGE on the source schemas and CREATE on the target schema is enough.
Yes. Set host: 127.0.0.1
and start an SSH tunnel locally, or use the ssh_tunnel:
config in dbt 1.6+.
Create separate outputs (dev, prod) in the same profile and pass --target prod
when running dbt.