⚒️ PostgreSQL conversational agent with Claude & DeepSeek (Multi-KPI, Secure)

1,002 views · ⚒️ Engineering

Description

🧠 Conversational PostgreSQL Agent

Enable AI-driven conversations with your PostgreSQL database using a secure and visual-free agent powered by n8n’s Model Context Protocol (MCP). This template allows users to ask multiple KPIs in a single message, returning consolidated insights — more efficient than the original Conversing with Data template.


🚀 Why This Template

Unlike the Conversing with Data workflow, which handles one KPI per message, this version:

💲 Estimated cost per full multi-request run: ~$0.01

This template is optimized for efficiency. Each message can return 2–4 KPIs (You can change the MaxIteration of the Agent to make it more, it is currently set up at 30 iterations) using a single Claude 3.5 Haiku session and DeepSeek-based SQL generation — balancing speed, reasoning, and affordability.


💬 Sample Use Case

User:
“Can you show product performance, revenue trends, and top 5 customers?”

Agent:

📊 Product Performance

  1. High-Waist Jeans — 10 units, $1,027 revenue
  2. Denim Jacket — 10 units, $783 revenue

📈 Sales Trends

🧍 Customer Insights

  1. Bob Brown — $1,520 spent
  2. Diana Wilson — $925 spent

All from one natural prompt.


🖼️ Real-World Interaction Screenshot

novisual_sql.png


🧰 What’s Inside

NodePurpose
MCP Server TriggerReceives user queries via /mcp/...
AI Agent + MemoryUnderstands and plans multi-step queries
Think ToolBreaks down the user’s question into structured goals
get_query_and_dataGenerates SQL securely from natural language
ListTables, GetSchemaAI tools to explore DB safely
Read/Insert/Update ToolsExecute structured operations (never raw SQL)
checkdatabase SubflowValidates SQL, formats response as clean text

🤖 Model Selection Recommendations

This template uses two types of models, selected for cost-performance balance and role alignment:

1. Claude 3.5 Haiku (Anthropic) – for the MCP Agent
The main conversational agent uses Claude 3.5 Haiku, ideal for MCP because it was built by Anthropic — the creators of the MCP standard. It’s fast, affordable, and performs excellently in tool-calling and reasoning tasks.

2. DeepSeek – for the SQL subworkflow
The subworkflow that turns natural language into SQL uses DeepSeek. It’s one of the most affordable and performant models available today for structured outputs like SQL, making it a perfect fit for utility logic.

✅ This setup provides top-tier reasoning + low-cost execution.


🔐 Security Benefits


🧪 Try a Prompt

> “Show me the top 5 products by units sold and revenue, total monthly sales trend, and top 5 customers by spending.”

In one message, the agent will:


🛠 How to Use

  1. 📥 Upload both workflow files into your n8n instance:
    • Build_your_own_PostgreSQL_MCP_server_No_visuals_.json
    • checkdatabase.json
  2. 🔐 Set up PostgreSQL credentials (e.g. “Postgres account 3”)
  3. 🧠 Confirm model setup:
    • Claude 3.5 Haiku for the main agent
    • DeepSeek for the subflow
  4. 🌐 Use the /mcp/... URL from the MCP Server Trigger to connect your frontend or chatbot
  5. 🗣 Ask questions naturally — the agent takes care of planning, querying, and formatting

🔄 Customization Ideas


📦 What’s Included

📝 These must be uploaded into your n8n workspace for the template to function.


📊 Comparison: Conversing with Data vs This Workflow

FeatureConversing with DataThis Workflow
Handles multi-KPI questions❌ No✅ Yes
Secure query execution✅ Yes✅ Yes
Structured response⚠️ JSON / raw✅ Clean natural language
Cost-efficiency⚠️ More calls✅ Optimized with fewer calls
Endpoint support❌ Manual interaction✅ MCP-ready (/mcp/...)

🔗 Prefer something more lightweight and cost-sensitive?
Try the original Conversing with Data template (single KPI + chart support):
Conversing with Data: Transforming Text into SQL Queries and Visual Curves

> I used this version for over 3 months and only spent $0.80 total, making it a great entry point if you’re just getting started or on a limited budget.


📚 More from the Same Creator

Looking for a different kind of AI reporting workflow?

Explore:
Customer Feedback Analysis with AI, QuickChart & HTML Report Generator
→ Automatically analyze customer input and generate full reports with insights and charts.
Customer Feedback Analysis with AI, QuickChart & HTML Report Generator

🔗 Nodes Used

Postgres, Execute Workflow Trigger, AI Agent, Anthropic Chat Model, Simple Memory, Call n8n Workflow Tool

📥 Import

Download workflow.json and import into n8n: Workflow menu → Import from File

📖 Importing guide · 🔑 Credential setup