Skip to main content

Natural language database queries with dual-agent AI & PostgreSQL integration

Workflow preview

Workflow preview
100%
Natural language database queries with dual-agent AI & PostgreSQL integration preview
Open on n8n.io

Important notice

This workflow is provided as-is. Please review and test before using in production.

1. Workflow Overview

AI Database Assistant with Smart Query's & PostgreSQL Integration Description: Transform Your Database into an Intelligent AI Assistant This workflow creates a smart database assistant that safe...

Best for

  • Engineering automation workflows
  • AI RAG automation workflows
  • advanced n8n builders looking for reusable templates

Tools used

n8n-nodes-base.executeworkflowtrigger, @n8n/n8n-nodes-langchain.agent, @n8n/n8n-nodes-langchain.toolthink, @n8n/n8n-nodes-langchain.lmchatopenrouter, @n8n/n8n-nodes-langchain.memorypostgreschat, n8n-nodes-base.telegram, @n8n/n8n-nodes-langchain.openai, n8n-nodes-base.telegramtrigger

Source and attribution

This workflow is cataloged by N8N Workflows and links back to its original n8n.io source page by Paul.

Original n8n.io source

1.1 Workflow description

Title
Natural language database queries with dual-agent AI & PostgreSQL integration
Workflow name
Natural language database queries with dual-agent AI & PostgreSQL integration

AI Database Assistant with Smart Query's & PostgreSQL Integration

Description:

πŸš€ Transform Your Database into an Intelligent AI Assistant

This workflow creates a smart database assistant that safely handles natural language queries without crashing your system. Features dual-agent architecture with built-in query limits and PostgreSQL optimization – perfect for commercial applications!

βœ… Ideal for:

  • SaaS developers building database search features πŸ”
  • Database administrators providing safe AI access πŸ›‘οΈ
  • Business teams needing user-friendly data queries πŸ“Š
  • Anyone wanting ChatGPT-like database interaction πŸ€–

πŸ”§ How It Works

1️⃣ User asks a question – "Show me top 10 popular products" 2️⃣ Main AI Agent – Interprets the request and ensures safety limits 3️⃣ SQL Sub-Agent – Generates precise PostgreSQL queries 4️⃣ Database executes – Returns formatted, limited results safely

⚑ Setup Instructions

1️⃣ Prepare Your Database

  • Ensure PostgreSQL is accessible from n8n
  • Note your table structure and column names
  • Set up database connection credentials

2️⃣ Customize the Templates

  • Replace [YOUR_TABLE_NAME] with your actual table name
  • Update [YOUR_FIELDS] with your column names
  • Modify examples to match your use case
  • Important: Keep all LIMIT clauses intact!

3️⃣ Configure the Agents

  • Copy Main Agent system message to your primary AI node
  • Copy Sub-Agent system message to your SQL generator node
  • Connect the sub-workflow between both agents

4️⃣ Test & Deploy

  • Test with sample queries like "Show me 5 recent items"
  • Verify query limits work (max 50 results)
  • Deploy and monitor performance

🎯 Why Use This Workflow?

βœ”οΈ System Protection – Built-in limits prevent crashes from large queries βœ”οΈ Natural Language – Users ask questions in plain English βœ”οΈ Commercial Ready – Generic templates work with any database βœ”οΈ Dual-Agent Safety – Smart interpretation + precise SQL generation βœ”οΈ PostgreSQL Optimized – Handles complex schemas and data types

🚨 Critical Features

  • Query Limits: Default 10, maximum 50 results (can be modified)
  • Error Prevention: No unlimited data retrieval
  • Smart Routing: Natural language β†’ Safe SQL β†’ Formatted results
  • Customizable: Works with any PostgreSQL database schema

πŸ”— Start building your AI database assistant today – safe, smart, and scalable!

1.2 Logical Blocks

This catalog entry is organized from the workflow JSON. The node-level section below shows the executable blocks available for review before importing the template.

2. Block-by-Block Analysis

Block 1 - When Executed by Another Workflow

Type / Role
n8n-nodes-base.executeWorkflowTrigger - executeWorkflowTrigger
Config choices
Version 1.1

Block 2 - Query agent

Type / Role
@n8n/n8n-nodes-langchain.agent - agent
Config choices
Version 2

Block 3 - Think

Type / Role
@n8n/n8n-nodes-langchain.toolThink - toolThink
Config choices
Version 1

Block 4 - OpenRouter Chat Model

Type / Role
@n8n/n8n-nodes-langchain.lmChatOpenRouter - lmChatOpenRouter
Config choices
Version 1

Block 5 - Postgres Chat Memory

Type / Role
@n8n/n8n-nodes-langchain.memoryPostgresChat - memoryPostgresChat
Config choices
Version 1.3

Block 6 - OpenRouter Chat Model1

Type / Role
@n8n/n8n-nodes-langchain.lmChatOpenRouter - lmChatOpenRouter
Config choices
Version 1

Block 7 - Think1

Type / Role
@n8n/n8n-nodes-langchain.toolThink - toolThink
Config choices
Version 1

Block 8 - Download File1

Type / Role
n8n-nodes-base.telegram - telegram
Config choices
Version 1.2

Block 9 - Transcribe1

Type / Role
@n8n/n8n-nodes-langchain.openAi - openAi
Config choices
Version 1.6

Block 10 - Telegram Trigger1

Type / Role
n8n-nodes-base.telegramTrigger - telegramTrigger
Config choices
Version 1.1

Block 11 - Voice or Text

Type / Role
n8n-nodes-base.switch - switch
Config choices
Version 3.2

Block 12 - Text

Type / Role
n8n-nodes-base.set - set
Config choices
Version 3.4

Block 13 - Merge

Type / Role
n8n-nodes-base.merge - merge
Config choices
Version 3.1

Block 14 - Main agent

Type / Role
@n8n/n8n-nodes-langchain.agent - agent
Config choices
Version 2

Block 15 - SEND MESSAGE

Type / Role
n8n-nodes-base.telegram - telegram
Config choices
Version 1.2

Block 16 - CALL QUERY AGENT

Type / Role
@n8n/n8n-nodes-langchain.toolWorkflow - toolWorkflow
Config choices
Version 2.2

Block 17 - ACCES DATABASE WITH DYNAMIC QUERYS

Type / Role
n8n-nodes-base.postgres - postgres
Config choices
Version 2.6

Block 18 - Sticky Note

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 19 - Sticky Note1

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 20 - Sticky Note2

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 21 - Sticky Note3

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 22 - Sticky Note4

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

3. Summary Table

Workflow Natural language database queries with dual-agent AI & PostgreSQL integration
Complexity advanced
Nodes 22
Categories Engineering, AI RAG
Author Paul
Published 17 Jun 2025

4. Reproducing the Workflow from Scratch

  1. 1. Download the workflow JSON

    Use the JSON export at /data/workflows/5012/5012.json as the source template for this automation.

  2. 2. Import the template into n8n

    Open n8n, import the downloaded JSON, and review each node before activating the workflow.

  3. 3. Configure credentials and variables

    Replace placeholder credentials, API keys, webhook URLs, account IDs, and environment-specific values with your own settings.

  4. 4. Test with sample data

    Run the workflow manually or in a staging workspace, inspect node output, and confirm downstream systems receive the expected data.

  5. 5. Activate and monitor

    Enable the workflow only after testing, then monitor executions, errors, and rate limits during the first production runs.

5. General Notes & Resources

Review imported nodes carefully before activation. This catalog entry is intended to help you inspect the workflow structure, understand required services, and find related templates faster.

Node names, credentials, schedules, webhook paths, and external service limits may need adjustment for your workspace.

Frequently asked questions

What does Natural language database queries with dual-agent AI & PostgreSQL integration do?

AI Database Assistant with Smart Query's & PostgreSQL Integration Description: Transform Your Database into an Intelligent AI Assistant This workflow creates a smart database assistant that safe...

What do I need before importing this workflow?

Review the workflow JSON, configure any required credentials in n8n, and test the automation in a safe workspace before using it in production.

Can I customize this workflow?

Yes. Use the block-by-block analysis and the downloadable JSON to inspect each node, then adjust credentials, prompts, schedules, filters, or destinations for your Engineering, AI RAG use case.