Conversational Text-to-SQL Data Analysis and Plot Generation - n8n Workflow

This advanced n8n workflow converts natural language requests into executable SQL queries against PostgreSQL. It uses multiple n8n node steps, including AI agents and memory, to deliver structured data for visualization. A powerful n8n template.

Workflow Preview

Ready to automate?

Download this n8n workflow template and start using it instantly.

Who is this best for?


  • Data Analysts and Business Intelligence users who want to query databases without writing SQL manually.

  • Developers building conversational data interaction tools.

  • n8n specialists looking for advanced examples of chained AI Agents and data processing within an n8n workflow.

  • Anyone seeking a robust n8n templates solution for Text-to-SQL automation.

Overview

The 'Conversational Text-to-SQL' n8n workflow solves the complexity of bridging natural language interaction with structured database queries. By integrating large language models (LLMs) with PostgreSQL capabilities, this n8n workflow allows users to simply ask a question about their data. First, the workflow intelligently extracts the database schema metadata upon setup. When a user sends a query via the dedicated n8n trigger, the primary AI agent converts the text into a precise SQL query. This query is executed against the database, and the results are then passed to a second AI agent. This second agent processes the query results and generates structured output suitable for visualization (like curves or plots). This dual-agent approach ensures both accurate data retrieval and subsequent meaningful visualization instructions, making this n8n node configuration exceptionally powerful.

How it Works

This powerful automation operates in two distinct phases: Setup and Execution.

Setup Phase (Manual Trigger)


  1. The n8n workflow begins with the 'When clicking "Test workflow"' n8n trigger, initiating the schema extraction process.

  2. The List all tables in a database and Schema Extractor n8n node steps connect to PostgreSQL to fetch metadata.

  3. This metadata is formatted, converted to JSON, and saved locally via the Save file locally n8n node, creating the necessary context for the AI agent.

Execution Phase (Chat Trigger)


  1. The Chat Trigger n8n trigger node listens for user input (the natural language query).

  2. The pre-saved database schema is loaded and combined with the chat input using the Combine schema data and chat input n8n node.

  3. The combined input and schema, along with conversation history provided by the Window Buffer Memory n8n node, are fed into the AI Agent (powered by Deepseek-chat).

  4. The AI Agent processes the request and outputs a structured response containing the generated SQL query.

  5. The Extract SQL query n8n node isolates the SQL string.

  6. An If n8n node (Check if query exists) verifies the SQL output. If valid, the Final SQL result Postgres n8n node executes the query.

  7. The raw data results are formatted and passed to the secondary plot agent n8n node.

  8. The plot agent uses the deepseek-chat LLM and a Structured Output Parser to convert the tabular data into structured plotting instructions (e.g., JSON data points or curve definitions).

  9. Finally, the results are merged and prepared by the Prepare final output n8n node, providing the user with both the query result information and the visualization data.

Installation Guide

To deploy this comprehensive n8n workflow, follow these steps:


  1. Import the n8n Workflow: Copy the provided JSON data and import it directly into your n8n instance via the Workflows tab, using the 'New' button and selecting 'Import from JSON'.

  2. Credential Setup:

PostgreSQL: You must set up a PostgreSQL credential for the List all tables, Schema Extractor, and Final SQL result n8n node instances. Ensure the credentials have read access to the relevant database.
AI/LLM: Configure the credentials for the Deepseek-chat n8n node instances (or replace them with an OpenAI/compatible LLM connection, ensuring the API key is valid).

  1. Initial Schema Setup: Before using the Chat Trigger, run the upstream flow once using the When clicking "Test workflow" n8n trigger. This executes the schema extraction n8n node sequence and saves the necessary schema file locally for the AI agent to access.

  2. Activate the Workflow: Ensure the primary Chat Trigger is activated (it should show a webhook URL).

  3. Testing: Use the chat interface linked to the Chat Trigger to test conversational queries against your database.

Node Details

Chat Trigger (n8n trigger): The primary entry point for conversational input. This n8n trigger initiates the entire process based on external chat interactions.
Deepseek-chat (LLM): The core LLM engine used by the main AI Agent to perform the complex Text-to-SQL translation.
AI Agent (LangChain n8n node): Orchestrates the decision-making process, utilizing the Deepseek LLM, the loaded database schema, and Window Buffer Memory to formulate precise SQL queries.
Postgres (n8n node): Used in three key places:


  1. To initially extract all table and column schemas.


  1. To execute the AI-generated SQL query (Final SQL result).

Check if query exists (If n8n node): Crucial flow control step. Ensures that subsequent database execution only occurs if the primary AI Agent successfully generated a valid query string.
plot agent (LangChain n8n node): A specialized secondary agent responsible for taking the resulting data from the executed SQL and transforming it into structured JSON suitable for visual representation. It uses a Structured Output Parser n8n node for guaranteed output formatting.

  • Window Buffer Memory (LangChain n8n node): Maintains the context of the conversation, allowing the AI agent to understand follow-up questions within the current session of this n8n workflow.

Related n8n Workflows

Paid

Nodes: 15 Nodes
Updated: December 26 2025
View all
Created by
hippolyte-hu
hippolyte-hu

Featured*