The direct way to connect n8n to PostgreSQL is to create a PostgreSQL Credential inside n8n, then use the built‑in Postgres node in your workflow and select that credential. Once the credential is saved, every Postgres node you add can run queries (SELECT, INSERT, UPDATE, etc.) using that connection.
How to Connect n8n to PostgreSQL (the practical, production‑safe way)
Connecting n8n to PostgreSQL is mostly about giving n8n the correct connection details and making sure your database accepts the connection. Here’s what matters in real setups:
- You need a PostgreSQL server that n8n can reach (local container, remote server, cloud provider).
- You need a PostgreSQL user with the correct permissions for whatever queries you’ll run.
- In n8n, you create a Postgres Credential that stores the DB info securely.
- You add a Postgres node in your workflow to run queries using that credential.
Step‑by‑Step Setup
Below is the cleanest path for someone new to n8n but building production‑grade workflows.
- Open n8n → Credentials → New, then choose Postgres.
- Fill the fields:
- Host: the PostgreSQL hostname (for Docker, often the service name like “postgres”)
- Port: normally 5432
- Database: your DB name
- User and Password: credential with enough permissions
- SSL: set to “yes” only if required by your host (common with cloud DBs)
- Save the credential.
- In your workflow, drag in a Postgres node.
- In the node’s settings, set:
- Resource: usually “Query”
- Operation: “Execute query”
- Postgres credential: select the one you created
- Write your SQL query.
A simple working example query
SELECT id, email
FROM users
WHERE active = true; // This returns all active users
The Postgres node will output the rows as JSON so the next nodes can use them with expressions like {{$json.email}}.
Production Notes (things that matter when this runs 24/7)
- Network access: n8n must be able to reach the DB host. In Docker, that usually means both containers on the same network. In cloud setups, check firewall rules.
- Use a dedicated DB user: Don’t use the superuser. Give your n8n user only the tables it needs.
- Long queries: n8n nodes have execution timeouts depending on hosting. Heavy ETL work belongs in the DB or a script, not inside n8n.
- Parameterize when possible: You can use expressions for dynamic values:
- Example:
{{$json.customerId}} inside your SQL
- Error handling: wrap Postgres nodes with an Error Workflow or use Continue On Fail if failures are acceptable.
- Transactions: n8n’s Postgres node does not expose manual multi‑step transactions. If your use case requires it, keep that logic outside n8n.
Quick connection test example
SELECT NOW(); // A simple query to verify the connection works
That’s everything required to connect n8n to PostgreSQL in a way that works both in development and in real production environments.