n8n RAG Tutorial: Build an AI Knowledge Base with Supabase Vector Store

A general chatbot is useful, but it doesn’t know your price list, your SOPs or last month’s client handbook. Ask it about those and it will guess. Retrieval-Augmented Generation (RAG) fixes that: you store your own documents as embeddings, and before the AI answers, it searches those documents and uses the matching chunks as context.

n8n has everything you need for RAG built in, and Supabase gives you a Postgres database with the pgvector extension to store the vectors. In this tutorial you’ll build a working knowledge-base chatbot step by step.

What you’ll build

  • An ingestion workflow that takes new files from a Google Drive folder, splits them into chunks, creates embeddings and stores them in Supabase.
  • A RAG chat agent that answers questions using only your documents, with conversation memory.
  • A Telegram knowledge bot so your team can ask the same agent from their phones.
  • A re-index workflow that removes old chunks when a file is updated, so answers don’t come from outdated versions.

How RAG works in n8n

Two sides share one vector table:

INGEST:  File → Default Data Loader → Text Splitter → Embeddings → Supabase (documents)

QUERY:   Question → AI Agent → Supabase Vector Store tool → top matching chunks
                         ↓
                    Answer grounded in your files

The important rule: use the same embeddings model on both sides. Vectors from different models can’t be compared.

Step 1: Connect Supabase to n8n

1.1 Create the vector table and match function

The Supabase Vector Store node expects a table and a SQL matching function, following Supabase’s LangChain quickstart. In your Supabase project, open the SQL Editor and run:

create extension if not exists vector;

create table documents (
  id bigserial primary key,
  content text,
  metadata jsonb,
  embedding vector(1536)
);

create function match_documents (
  query_embedding vector(1536),
  match_count int default null,
  filter jsonb default '{}'
) returns table (
  id bigint,
  content text,
  metadata jsonb,
  similarity float
)
language plpgsql
as $$
#variable_conflict use_column
begin
  return query
  select id, content, metadata,
    1 - (documents.embedding <=> query_embedding) as similarity
  from documents
  where metadata @> filter
  order by documents.embedding <=> query_embedding
  limit match_count;
end;
$$;

1536 matches OpenAI’s text-embedding-3-small. If you pick a different embeddings model, change the dimension in both places to match it.

1.2 Add the Supabase credential

  1. In Supabase, go to Integrations → Data API and copy the Project URL (for example https://your-project.supabase.co, without /rest/v1). Make sure the Data API is enabled.
  2. Go to Project Settings → API Keys and create or reveal a secret key.
  3. In n8n, create a Supabase API credential and paste the URL as Host and the key as Secret Key.

Supabase is phasing out the legacy service_role key, so use the new secret key format for new credentials. Never put this key in front-end code.

1.3 Add an OpenAI credential

Create an OpenAI credential with your API key. You’ll use it for both embeddings and the chat model. New to Supabase in n8n? The Supabase automation tutorial covers the basics of the regular Supabase node.

Workflow 1: Ingest Google Drive files into Supabase

Google Drive Trigger (File Created)
        ↓
Google Drive (Download file)
        ↓
Supabase Vector Store (Insert Documents)
   ↑ Embeddings OpenAI
   ↑ Default Data Loader (Binary)
  1. Add a Google Drive Trigger, set the event to File Created and point it at a folder like Knowledge Base.
  2. Add a Google Drive node with operation Download and File ID {{ $json.id }}.
  3. Add a Supabase Vector Store node, Operation Mode Insert Documents, Table Name documents.
  4. On its Embeddings connector, attach Embeddings OpenAI with model text-embedding-3-small.
  5. On its Document connector, attach a Default Data Loader: Type of Data Binary, Data Format Automatically Detect by MIME Type, Text Splitting Simple (chunks of 1,000 characters with 200 overlap).
  6. Under Options → Metadata, add file_id = {{ $('Google Drive Trigger').item.json.id }} and file_name = {{ $('Google Drive Trigger').item.json.name }}.

Drop a PDF into the folder, run the workflow, then check the documents table. You should see several rows per file, each with content, metadata and an embedding.

Workflow 2: RAG chat agent that answers from your docs

When chat message received
        ↓
AI Agent
   ↑ OpenAI Chat Model
   ↑ Simple Memory
   ↑ Supabase Vector Store (Retrieve Documents as Tool)
          ↑ Embeddings OpenAI
  1. Add a Chat Trigger (When chat message received).
  2. Add an AI Agent node. Attach an OpenAI Chat Model and a Simple Memory sub-node so follow-up questions keep their context.
  3. On the agent’s Tool connector, add a Supabase Vector Store node with Operation Mode Retrieve Documents (As Tool for AI Agent).
  4. Set Name to company_knowledge_base, Table Name documents, Limit 4, and write a clear Description: Search internal company documents: policies, pricing, SOPs and product guides. Use this for any question about the company.
  5. Attach Embeddings OpenAI with the same model as ingestion.
  6. In the agent’s System Message option, add rules like these:
You answer questions using the company_knowledge_base tool.
Always search the tool before answering.
If the documents don't contain the answer, say you don't know.
Mention the source file name when possible.

The agent decides when to call the tool based on its name and description, so a specific description matters more than you’d expect. If you want a refresher on the agent node itself, see how to create an AI agent in n8n.

Open the chat panel and ask something only your documents know. If the answer quotes your file, RAG is working.

Workflow 3: Telegram knowledge bot

Telegram Trigger (message)
        ↓
AI Agent (same tool, memory and model)
        ↓
Telegram (Send Message)
  1. Add a Telegram Trigger listening for messages.
  2. Copy the AI Agent setup from Workflow 2. Set Source for Prompt to Define below with Prompt {{ $json.message.text }}.
  3. In Simple Memory, set the session ID to Define below with key {{ $('Telegram Trigger').item.json.message.chat.id }} so each chat keeps its own history.
  4. Add a Telegram node, operation Send Message, Chat ID {{ $('Telegram Trigger').item.json.message.chat.id }}, Text {{ $json.output }}.

Now staff can ask “What’s our refund policy?” from Telegram and get an answer pulled from the actual policy file.

Workflow 4: Re-index updated files (no stale answers)

If you upload a new version of a file, the old chunks stay in the table and the agent may quote outdated info. Delete them before inserting again:

Google Drive Trigger (File Updated)
        ↓
Postgres (Execute Query: delete old chunks)
        ↓
Google Drive (Download) → Supabase Vector Store (Insert)
  1. Add a Google Drive Trigger with event File Updated on the same folder.
  2. Add a Postgres node connected to your Supabase database (use the connection details from Supabase’s Connect panel) with operation Execute Query, and pass the file ID as a query parameter {{ $('Google Drive Trigger').item.json.id }}:
delete from documents
where metadata->>'file_id' = $1;
  1. Reuse the Download and Insert steps from Workflow 1 with the same metadata fields.

Because every chunk carries file_id in its metadata, you can also use the vector store’s Metadata Filter option in Get Many mode to search only one file.

Troubleshooting

  • “Could not find the function public.match_documents”: the SQL function wasn’t created, or its name differs from the node’s Query Name option. Re-run the SQL and check the name.
  • “different vector dimensions”: your embeddings model doesn’t match vector(1536). Change the table and function dimensions, or switch models, then re-ingest.
  • Agent answers without searching: make the tool description more specific and tell the agent in the system message to always use the tool first.
  • Irrelevant chunks: lower the chunk size, raise the Limit slightly, or enable Rerank Results with a reranker node such as Reranker Cohere.
  • Answers from old file versions: run the re-index workflow, or clear rows for that file_id.
  • Credential errors: the Host must not include /rest/v1, and the Data API must be enabled in Supabase.

Wrapping up

With one Supabase table, one SQL function and a few n8n nodes, your AI agent now answers from your own documents instead of guessing. Start with a small folder of high-value files like FAQs and SOPs, test the answers, then add more sources such as Notion pages or website content using the same insert pattern.

Want to see builds like this step by step? Watch the tutorials on Node & Volt on YouTube.

Leave a Comment