DEV Community

Pirate Prentice
Pirate Prentice

Posted on

n8n Postgres Node: Query, Insert, Update and Delete Records (Free Workflow JSON)

The n8n Postgres node lets you run SQL queries, insert rows, update records, and delete data from any PostgreSQL database — all inside your automation workflows, no backend code required.

This guide covers every operation, credential setup, common gotchas, and a free copy-paste workflow JSON you can import directly into n8n.


Prerequisites

  • n8n (self-hosted or cloud)
  • A PostgreSQL database (local, Supabase, Railway, Render, Neon, or any hosted Postgres)
  • Your Postgres host, port, database name, username, and password

Setting Up Postgres Credentials in n8n

  1. In n8n, go to Credentials → New → Postgres
  2. Fill in:
    • Host — your Postgres hostname (e.g. db.yourproject.supabase.co)
    • Port — default 5432
    • Database — your database name
    • User — your Postgres username
    • Password — your Postgres password
    • SSL — enable if your host requires it (Supabase, Neon, Railway all require SSL)
  3. Click Test — you should see a green checkmark

Common gotcha: Supabase and Neon require SSL mode set to Require. If you get a connection refused error on port 5432, check your host's firewall rules — many cloud Postgres providers only allow connections from specific IP ranges.


Postgres Node Operations

Operation What it does
Execute Query Run any raw SQL statement
Insert Insert one or more rows into a table
Update Update rows matching a condition
Delete Delete rows matching a condition
Select Fetch rows from a table

Operation 1: Execute Query (Raw SQL)

Use this for complex queries, JOINs, aggregations, or anything beyond simple CRUD.

Node settings:

  • Operation: Execute Query
  • Query: your SQL (use $1, $2 for parameterized queries)

Example — count orders by status:

SELECT status, COUNT(*) as total
FROM orders
GROUP BY status
ORDER BY total DESC
Enter fullscreen mode Exit fullscreen mode

Example — parameterized query with n8n expressions:

SELECT * FROM customers WHERE email = $1 AND created_at > $2
Enter fullscreen mode Exit fullscreen mode

In the Query Parameters field, add: ={{ $json.email }}, {{ $json.since_date }}

Why parameterized queries? They prevent SQL injection. Never concatenate user input directly into your SQL string.


Operation 2: Insert

Insert one or more rows. n8n maps workflow item fields to table columns automatically.

Node settings:

  • Operation: Insert
  • Schema: public (default)
  • Table: your table name
  • Columns: select which columns to fill
  • On conflict: choose Ignore (skip duplicates) or Update (upsert behavior)

Example use case: A webhook fires when a new form is submitted → Insert node writes the submission to your leads table.


Operation 3: Update

Update existing rows that match a condition.

Node settings:

  • Operation: Update
  • Table: your table name
  • Update Key: the column used to identify which row(s) to update (usually id)

Example: After a Stripe webhook fires for a successful payment, update orders set status = 'paid' where id matches.


Operation 4: Delete

Delete rows matching a key value.

Node settings:

  • Operation: Delete
  • Table: your table name
  • Delete Key: column to match for deletion

Caution: For complex WHERE conditions, use Execute Query with a parameterized DELETE statement instead.


Operation 5: Select

Fetch rows from a table with optional filtering.

Node settings:

  • Operation: Select
  • Table: your table name
  • Limit: how many rows to return
  • Sort: optional column + direction

For complex filtering (JOINs, multi-condition WHERE), use Execute Query instead.


Common Gotchas

Problem Cause Fix
SSL connection error Cloud Postgres requires SSL Set SSL to Require in credentials
Column not found Case mismatch Postgres is case-sensitive for quoted identifiers; use lowercase
Timeout on large queries Default timeout too short Increase Query Timeout (ms) in credentials
Too many connections n8n opens a new connection per execution Use a connection pooler (PgBouncer, Supabase pooler)
Insert fails on duplicate No conflict handling Set On Conflict to Ignore or Update

Free Workflow JSON: Webhook → Postgres Lead Capture

This workflow receives a form submission via webhook and inserts it into a leads table.

Assumed table:

CREATE TABLE leads (
  id SERIAL PRIMARY KEY,
  name TEXT,
  email TEXT,
  source TEXT,
  created_at TIMESTAMPTZ DEFAULT NOW()
);
Enter fullscreen mode Exit fullscreen mode
{
  "name": "Webhook to Postgres Lead Capture",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "new-lead",
        "responseMode": "responseNode"
      },
      "name": "Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1,
      "position": [250, 300]
    },
    {
      "parameters": {
        "operation": "insert",
        "schema": "public",
        "table": "leads",
        "columns": "name,email,source",
        "additionalFields": { "onConflict": "ignore" }
      },
      "name": "Postgres",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2,
      "position": [500, 300]
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "{\"status\": \"ok\"}"
      },
      "name": "Respond",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1,
      "position": [750, 300]
    }
  ],
  "connections": {
    "Webhook": { "main": [[{"node": "Postgres", "type": "main", "index": 0}]] },
    "Postgres": { "main": [[{"node": "Respond", "type": "main", "index": 0}]] }
  }
}
Enter fullscreen mode Exit fullscreen mode

Import this JSON into n8n (New Workflow → Import), swap in your credential ID, and activate.


Real-World Use Cases

  • CRM sync: Webhook → insert new contacts into Postgres
  • Order processing: Stripe webhook → update order status in your database
  • Daily reporting: Schedule trigger → SELECT aggregated stats → send email
  • Data cleanup: Cron job → DELETE rows older than 90 days
  • Audit logging: Any workflow → INSERT an audit row after every significant action

Want More n8n Workflows?

The n8n Workflow Starter Pack includes 10 pre-built, documented workflow JSONs covering the most common automation patterns — ready to import and customize.

Get the n8n Workflow Starter Pack → ($29, instant download)


What Postgres use case are you building in n8n? Drop it in the comments — I read every one.

Top comments (1)

Collapse
 
pirateprentice profile image
Pirate Prentice

Quick question for Postgres users: What operation do you run most often through n8n — SELECT queries for dashboards, INSERT for lead capture, or UPDATE for syncing records back after processing?

For me the webhook → INSERT → respond pattern (workflow 1 in this article) covers about 70% of automation use cases. The SSL gotcha (sslmode=require vs disable) is the one that trips people up most on cloud databases like Supabase or Railway.

If you want the full workflow JSON with error handling and a retry branch, grab the Workflow Starter Pack at the link in the article — Postgres workflow included.