AI Agent to chat with Supabase PostgreSQL DB n8n Workflow Template

n8n workflow template · 6 nodes · OpenAI

AI Agent to chat with Supabase PostgreSQL DB is a free n8n workflow template that starts when a chat message is received and works with OpenAI and Postgres. It has 6 nodes, 3 of them AI nodes (Chat Trigger, OpenAI Chat Model and AI Agent). The notes inside the workflow say: “Accessing and analyzing database data often requires SQL expertise or dedicated reports, which can be time-consuming. This workflow empowers users to interact with a database conversationally through an AI-powered agent.” To run it, you add your own credentials for OpenAI API and Postgres.

At a glance

Trigger
Chat Trigger (When chat message received)
Nodes
6
Apps
OpenAI and Postgres
AI nodes
3
Credentials
2 types
Category
Spreadsheets, databases and docs

Apps and integrations

  • OpenAI
  • Postgres

Node types used

  • Chat Trigger
  • OpenAI Chat Model
  • Postgres Tool
  • AI Agent

How it works

The text below comes from the sticky notes inside the workflow. We removed links, code samples and anything that looks like a key or token.

AI Agent to chat with Supabase/PostgreSQL DB

Made by Mark Shcherbakov from community 5minAI

Accessing and analyzing database data often requires SQL expertise or dedicated reports, which can be time-consuming. This workflow empowers users to interact with a database conversationally through an AI-powered agent. It dynamically generates SQL queries based on user requests, streamlining data retrieval and analysis.

This workflow integrates OpenAI with a Supabase database, enabling users to interact with their data via an AI agent. The agent can

  • Retrieve records from the database.
  • Extract and analyze JSON data stored in tables.
  • Provide summaries, aggregations, or specific data points based on user queries.

Finetune the prompt of assistant

Setup notes

The text below comes from the sticky notes inside the workflow. We removed links, code samples and anything that looks like a key or token.

Set up steps

Preparation

  1. Create Accounts
  • N8N: For workflow automation.
  • Supabase: For database hosting and management.
  • OpenAI: For building the conversational AI agent.
  1. Configure Database Connection
  • Set up a PostgreSQL database in Supabase.
  • Use appropriate credentials (username, password, host, and database name) in your workflow.

N8N Workflow

AI agent with tools

  1. Code Tool
  • Execute SQL queries based on user input.
  1. Database Schema Tool
  • Retrieve a list of all tables in the database.
  • Use a predefined SQL query to fetch table definitions, including column names, types, and references.
  1. Table Definition
  • Retrieve a list of columns with types for one table.

... or watch set up video [20 min]

Replace password and username for Supabase

Nodes in this workflow

The 6 nodes in file order (sticky notes excluded). Branches and connections determine execution order in n8n:

  1. When chat message received (Chat Trigger)
  2. OpenAI Chat Model (OpenAI Chat Model)
  3. DB Schema (Postgres Tool)
  4. Get table definition (Postgres Tool)
  5. AI Agent (AI Agent)
  6. Run SQL Query (Postgres Tool)

Credentials you need

  • OpenAI API – used by OpenAI Chat Model
  • Postgres – used by DB Schema, Get table definition and Run SQL Query

Configure your own credentials after importing. Create each credential in n8n, then select it in the node.

How to import into n8n

  1. Download the JSON file with the button above.
  2. In n8n, open a new workflow, select the ... menu and choose Import from File. You can also paste the JSON straight into the editor canvas.
  3. Open each node that shows a warning and select or create its credential.
  4. Read the notes on the canvas, test the workflow with sample data, then activate it.

Do you need somewhere to run it? Read our guide to self-hosted n8n.

Need this customised?

We can adapt this workflow to your apps, data and process, then deploy and support it.

Contact Us