Conversational AI Agent for Supabase PostgreSQL Database - n8n Workflow

Deploy a powerful AI Agent using this specialized n8n workflow. Chat with your Supabase database, generate dynamic SQL, and retrieve data conversationally using advanced n8n node configuration and OpenAI function calling.

Workflow Preview

Ready to automate?

Download this n8n workflow template and start using it instantly.

Who is this best for?


  • Database administrators and analysts who need fast, conversational access to data.

  • n8n users specializing in AI integration and function calling.

  • Developers using Supabase/PostgreSQL who want to democratize data retrieval.

  • Users looking for advanced n8n templates integrating Langchain components.

Overview

Accessing complex data often requires writing intricate SQL queries, which can be a bottleneck for non-technical users. This specialized n8n workflow solves this by utilizing an AI Agent capable of understanding natural language requests and dynamically generating precise SQL. This powerful n8n node setup connects the intelligence of OpenAI's models (via the OpenAI Chat Model n8n node) with the data execution capabilities of PostgreSQL tools. When a user initiates a conversation via the n8n trigger, the agent decides which database tool to use—whether to check the table schema, fetch column definitions, or execute a query. This n8n automation provides immediate, conversational data retrieval, transforming how teams interact with their Supabase data.

How it Works

This comprehensive n8n workflow begins with the When chat message received n8n trigger, which activates upon receiving a user's conversational input.


  1. Initiation (The n8n Trigger): The workflow is activated by the When chat message received n8n trigger, capturing the user's text input.

  2. AI Agent Orchestration: The user input is passed to the AI Agent n8n node, configured as an openAiFunctionsAgent. This agent uses a specific system message instructing it to act as a DB assistant, prioritizing running custom SQL queries.

  3. Tool Selection: The AI Agent intelligently decides which of its three PostgreSQL tools (acting as callable functions or n8n nodes) is necessary to fulfill the request:

If the AI needs to understand the available data structure, it calls the DB Schema tool.
If the AI needs details on columns, types, or relationships for a specific table, it calls the Get table definition tool, dynamically injecting the required table name.
* For final data retrieval or aggregation, it calls the Run SQL Query tool, dynamically injecting the generated SQL query.

  1. Execution and Response: The selected n8n node executes the required action against the Supabase database. The results are fed back to the AI Agent, which synthesizes the raw data into a clear, conversational response for the user.

Installation Guide

To utilize this advanced n8n workflow, follow these setup steps:


  1. Import the n8n Workflow: Copy the provided JSON and import it directly into your n8n instance.

  2. OpenAI Credentials: Locate the OpenAI Chat Model n8n node. You must set up or select your OpenAI API credentials. Ensure the API key has access to compatible models (like GPT-3.5 or GPT-4).

  3. Supabase/PostgreSQL Credentials: The workflow uses three Postgres Tool n8n nodes (Run SQL Query, DB Schema, Get table definition). Each of these nodes requires shared Postgres credentials.

Click on one of the Postgres nodes and create a new credential connection.
Input your Supabase connection details: Host, Database Name, User, and Password. (A sticky note reminds users to replace these crucial details.)

  1. Activate the Trigger: Ensure the When chat message received n8n trigger is active to start monitoring for chat input.

  2. Fine-tuning: Review the system message in the AI Agent n8n node parameters to fine-tune its persona and instructions for optimal performance against your specific dataset.

Node Details

When chat message received (n8n trigger):
Function: Serves as the starting point, listening for incoming chat messages to initiate the n8n workflow.
Key Configuration: This is the primary n8n trigger type for Langchain-based agents.
AI Agent (n8n node):
Function: Orchestrates the conversational flow, using OpenAI's function calling capability to decide which database tool to execute based on the user's text input.
Key Configuration: Uses the openAiFunctionsAgent type. Includes a crucial System Message prompt defining its role as a DB assistant.
OpenAI Chat Model (n8n node):
Function: Provides the language understanding and reasoning required by the AI Agent.
Key Configuration: Requires valid OpenAI API credentials.
Run SQL Query (Postgres Tool n8n node):
Function: Executes custom, dynamically generated SQL queries provided by the AI Agent against the Supabase database.
Key Configuration: Query uses an expression {{ $fromAI("query",...) }} to inject the SQL output generated by the AI.
DB Schema (Postgres Tool n8n node):
Function: Acts as a knowledge tool for the AI, fetching a list of all table names within the database's public schema.
Key Configuration: Contains a fixed SQL query to retrieve tableschema and tablename from informationschema.tables.
Get table definition (Postgres Tool n8n node):
Function: Provides detailed schema information (column name, data type, constraints) for a specific table, enabling the AI to construct accurate queries.
Key Configuration: Query uses an expression {{ $fromAI("table
name") }} to fetch details for the table requested by the AI.

Related n8n Workflows

Free

Nodes: 6 Nodes
Updated: December 26 2025
View all
Created by

I am a business analyst with a development background, dedicated to helping small businesses and entrepreneurs leverage cloud services for increased efficiency. My expertise lies in automating manual workflows, integrating data from multiple cloud service providers, creating insightful dashboards, and building custom CRM systems.

Featured*