Multi-AI Agent Chatbot for SQL Queries & QuickChart Visualization with n8n

Discover how this unique n8n workflow automates interactive AI-driven SQL database queries and dynamic chart generation with QuickChart. Eliminate manual database probing and chart creation, saving hours while enhancing data insights.
agent
toolWorkflow
postgresTool
+8
Workflow Identifier: 1318
NODES in Use: chatTrigger, postgresTool, toolWorkflow, executeWorkflowTrigger, agent, switch, httpRequest, outputParserStructured, lmChatOpenAi, memoryPostgresChat, set
Automate SQL queries with n8n and AI agents

Press CTRL+F5 if the workflow didn't load.

Learn how to Build this Workflow with AI:

What this workflow does

This workflow lets you chat in normal language to get data from a Postgres database and see charts right away.
It solves the problem of writing SQL by hand and making charts separately, saving time and errors.
You ask a question, and then get back data and a chart URL that shows the answer visually.

The system uses AI for understanding questions, writing SQL queries, running them, and making charts automatically.
It stores chat history so future talks have memory and context.


Who should use this workflow

This is good for people who work with Postgres database and need quick answers with charts.
Examples are analysts, managers, or anyone who wants easy data access without coding SQL or chart tools.

The workflow helps non-technical users by turning natural language questions into data and pictures they can use fast.


Tools and services used

  • Postgres Database: To store data and chat history.
  • OpenAI API (GPT-4o-mini): To understand questions and create SQL and chart code.
  • QuickChart API: To generate chart images from AI-made chart configuration.
  • n8n Automation Platform: To connect all steps and run the workflow.
  • LangChain: Used for AI prompt and agent management within n8n nodes.

Beginner step-by-step: How to build this in n8n

1. Download and Import Workflow

  1. Click the Download button on this page to get the workflow file.
  2. Open your n8n editor (on cloud or self-host n8n) and choose “Import from File”.
  3. Upload the workflow JSON file.

2. Configure Credentials

  1. Set up Postgres credentials in Settings → Credentials → Postgres. Make sure credentials allow SELECT queries.
  2. Enter your OpenAI API Key in Settings → Credentials → OpenAI.
  3. If needed, update table names or folder IDs inside the workflow nodes to match your setup.

3. Test the Workflow

  1. Trigger the webhook URL from Webhook node (named “When chat message received”).
  2. Send a test chat message like “Show me a bar chart of prices.”
  3. Check the output for data and a chart URL.

4. Activate for Production

  1. Make sure your webhook URL is secured and accessible to chat clients.
  2. Set the workflow active using the toggle in n8n editor.
  3. Monitor execution logs to track errors or usage.

Workflow inputs, processing, and outputs

Inputs

  • User’s chat message in plain text.
  • Postgres database with data and chat memory tables.

Processing

  • Primary AI Agent: Reads user question and picks either database query or chart tools.
  • Secondary Postgres Agent: Creates SQL query for data fetch based on AI output.
  • Execute SQL Node: Runs SQL and returns data.
  • Secondary QuickChart Agent: Uses data and user input to make Chart.js config JSON.
  • QuickChart GET URL Node: Builds URL using chart config.
  • Create QuickChart Node: Calls QuickChart API for chart image.
  • Postgres Chat Memory Node: Saves conversation history for future context.

Outputs

  • Structured data from SQL queries.
  • Dynamic QuickChart URL users can open to see visual chart.
  • Chat memory updated for next questions.

Customization ideas

  • Change AI model in OpenAI nodes to stronger versions like “gpt-4” for better understanding.
  • Adjust Chart.js JSON in QuickChart Object Schema node to change chart type or colors.
  • Add more tools for database operations beyond SELECT, like INSERT or UPDATE, via LangChain workflows.
  • Connect output to Slack or email nodes for notifications or reports.

What to do if something breaks (Troubleshooting)

SQL query errors or empty result

Check if database user has correct permissions to read tables.
Make sure table and column names in the query are spelled right.

Use a SQL client external to n8n to test the SQL first.

Charts don’t display right

JSON schema for the chart config might be wrong or missing fields.
Use the example JSON in the QuickChart Object Schema node as a guide.

Test chart JSON at QuickChart playground site to verify it works before running in workflow.


Beginner usage tips for n8n production

Import the full workflow to your n8n editor first.
Add your Postgres and OpenAI API keys under n8n Credentials.

Test the webhook by sending live chat messages.
After success, enable the workflow.

Check logs often to catch errors early.
Keep the chat memory table backed up to not lose past conversations.

If running self hosting n8n, visit self-host n8n for more ideas about stable setup.


Summary of benefits and results

✓ Users ask data questions in natural words.
✓ SQL queries and charts are made automatically by AI.
✓ Data and chart URLs return fast in chat replies.
✓ Chat memory stored in Postgres helps better responses later.
✓ No more manual SQL scripting or chart building needed.
✓ Saves time and avoids errors, making reports easier.


Automate SQL queries with n8n and AI agents

Visit through Desktop to Interact with the Workflow.

Frequently Asked Questions

Yes, but users must make sure the new database works with the Postgres query node or change queries to fit the new database.
Yes, because it calls GPT-4o-mini to make SQL and chart JSON. Users can reduce costs by optimizing prompts and usage.
Yes, chat history and data sit securely in the Postgres database owned by the user.
It works best with medium-size data for chat speed. Big queries may need extra steps like pagination or query tuning.

Promoted by BULDRR AI

Related Workflows

Automate Twist Channel Creation and Messaging with n8n

This workflow automates creating and updating a channel in Twist and sending a personalized message to specific users. It eliminates manual setup errors and saves time managing Twist communications.

Automate Ideogram Image Generation with Google Sheets & Gmail

This workflow automates graphic design image generation via Ideogram AI, storing image data in Google Sheets and Google Drive, with email alerts via Gmail. It saves designers hours by automating image creation, remixing, review, and record-keeping.

Automate IT Support with Slack and OpenAI in n8n

Streamline IT support by automating Slack message handling using n8n and OpenAI. This workflow handles Slack DMs, filters bots, queries a Confluence knowledge base, and delivers AI-generated responses, improving support efficiency and response time.

Automate Crypto Analysis with CoinMarketCap & n8n AI Agent

Discover how this unique n8n workflow leverages CoinMarketCap’s multi-agent AI to deliver precise, real-time cryptocurrency insights directly via Telegram. Manage crypto data analysis efficiently with automated multi-source API integration.

Automate Gumroad to Beehiiv Subscriber Sync with n8n

Learn how to automatically add new Gumroad sales customers as Beehiiv newsletter subscribers using n8n automation. This workflow saves time by syncing sales data to Google Sheets CRM and notifying your Telegram channel instantly.

Generate On-Brand Blog Articles Using n8n and OpenAI

This workflow automates the creation of on-brand blog articles by analyzing existing company content using n8n and OpenAI. It extracts article structures and brand voice to produce consistent draft articles, saving significant content creation time.
1:1 Free Strategy Session
Your competitors are already automating. Are you still paying for it manually?

Do you want to adopt AI Automation?

Every hour your team does repetitive work, you're burning real money.
While you wait, faster businesses are cutting costs and moving quicker.
AI and automations aren't the future anymore — they're the present.

Book a live 1-on-1 session where we show you exactly which of your daily tasks can be automated — and what it’s costing you not to.