Local AI Agentic RAG for Document Question Answering with Ollama and PGVector - n8n Workflow

Use this advanced n8n workflow to build a powerful local Agentic RAG system using Ollama, PGVector, and PostgreSQL. Process PDFs, CSVs, and Excel files automatically for intelligent question answering. Ideal for self-hosted n8n templates.

Workflow Preview

Ready to automate?

Download this n8n workflow template and start using it instantly.

Who is this best for?

Users running self-hosted n8n instances looking for local AI solutions.
Data scientists or developers needing to perform sophisticated RAG on proprietary documents without cloud services.
Anyone interested in advanced n8n templates for knowledge base creation.
Organizations requiring an n8n workflow that can intelligently handle mixed data types (text and tabular data).

Overview

This powerful n8n workflow solves the problem of reliable, local document question answering (Q&A) over diverse file types. Standard RAG solutions often struggle with numerical data or require complex, slow processing. This specific n8n workflow, leveraging the power of Ollama and PGVector, creates an 'Agentic RAG' system. This means the AI agent can dynamically choose the best tool for the job: performing a vector database lookup for text, or executing a dedicated SQL query for tabular data (CSVs/Excel files).

The complete automation is provided as a reusable set of n8n templates. The file ingestion process is fully automated via an n8n trigger, ensuring that any new document added to a local folder is immediately chunked, embedded using an Ollama embedding model, and stored in the PostgreSQL/PGVector database. This n8n node architecture provides maximum flexibility and power for local AI use cases.

How it Works

The n8n workflow operates in two main phases: Document Ingestion and Agentic Querying.

1. Document Ingestion (Local File Trigger)


  1. The Local File Trigger n8n trigger monitors a mounted local folder (/data/shared) for new or changed files.

  2. The file path is processed by the Set File ID n8n node to extract metadata (ID, type, title).

  3. Existing records for the file are deleted from the vector store and the table row store (cleanup for updates).

  4. Metadata is recorded in the documentmetadata PostgreSQL table.

  5. The Read/Write Files from Disk n8n node loads the content.

  6. A Switch n8n node directs processing based on file type:

Text/PDF files: The content is extracted, passed through the Recursive Character Text Splitter for chunking, and then embedded using the Embeddings Ollama n8n node before being stored in the Postgres PGVector Store.
Tabular files (CSV/Excel): The data is extracted, aggregated, and summarized. Crucially, the row data is inserted into the document
rows PostgreSQL table (as JSONB), and the schema is updated in the document_metadata table. This allows the RAG AI Agent to perform SQL queries later.

2. Agentic Querying (Chat Trigger)


  1. The When chat message received n8n trigger receives a user question.

  2. The RAG AI Agent n8n node takes over. This agent is configured with an Ollama (Change Base URL) language model and uses Postgres Chat Memory for context.

  3. The agent analyzes the user's input and selects the appropriate tool from its toolkit:

RAG Lookup (Postgres PGVector Store1): For general text questions.
Query Document Rows (PostgresTool): If the question requires aggregation or calculation on tabular data.
List Documents (PostgresTool): To check what files are available.
Get File Contents (PostgresTool): To fetch the full context of a document if RAG lookup is insufficient.

  1. The final, reasoned response from the RAG AI Agent is delivered back to the user via the Respond to Webhook n8n node. This entire n8n workflow structure provides robust, intelligent interaction.

Installation Guide

To deploy this advanced n8n workflow, follow these steps:


  1. Import the n8n Workflow: Copy the provided JSON and import it into your n8n instance via the 'New' menu > 'Import from JSON'.

  2. Configure Credentials:

PostgreSQL: Configure your PostgreSQL credentials for Supabase or your local database. Ensure the database supports the PGVector extension.
Ollama Connection: The Ollama (Change Base URL) node is an overridden OpenAI Chat n8n node. Create an OpenAI credential, but set the 'Base URL' to your local Ollama instance (e.g., http://ollama:11434/v1). The API key can be set to any placeholder value, as it is ignored for local models.

  1. Database Setup: Manually run the Create Document Metadata Table and Create Document Rows Table (for Tabular Data) n8n node steps once to initialize the required database structure. Wait for successful execution.

  2. Local File Setup: Ensure your n8n container has a volume mounted to /data/shared (this is where the Local File Trigger n8n trigger looks for documents).

  3. Activate: Enable the Local File Trigger and the When chat message received n8n trigger. The n8n automation is now live.

Node Details

Local File Trigger:
Function: This critical n8n trigger monitors the local shared folder (/data/shared) for document additions or changes, initiating the ingestion process.
Key Configuration: Monitors 'add' and 'change' events on the /data/shared path.
When chat message received (n8n trigger):
Function: The primary interface for users to submit questions to the RAG Agent.
Key Configuration: A public chat trigger endpoint.
RAG AI Agent:
Function: The core intelligence of the n8n workflow. It orchestrates the process, selects the best tool (RAG or SQL) to answer the query, and maintains conversation history.
Key Configuration: Uses the custom Ollama language model, Postgres chat memory, and references all RAG/Database tools.
Ollama (Change Base URL):
Function: Acts as the large language model for the RAG AI Agent, routing requests locally to Ollama.
Key Configuration: An OpenAI Chat n8n node where the Base URL is set to the Ollama API endpoint (http://ollama:11434/v1).
Postgres PGVector Store:
Function: Manages the storage of document chunks and their embeddings (via the Embeddings Ollama n8n node) in the documentspg table. Used for standard RAG lookups.
Key Configuration: Configured in 'insert' mode for the ingestion path and 'retrieve-as-tool' mode for the agent query path.
Embeddings Ollama:
Function: Generates vector embeddings for document chunks using a local Ollama embedding model (nomic-embed-text:latest). This is essential for the n8n workflow's RAG functionality.
Extract from CSV/Extract from Excel:
Function: Handles file parsing for tabular data before insertion into the documentrows PostgreSQL table.
Insert Table Rows & Query Document Rows (Postgres n8n node):
* Function: Stores and allows querying of tabular data rows. The agent can execute complex SQL against this n8n node to perform precise calculations.

Related n8n Workflows

Free

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

Hi 👋 I'm Jadai kongolo. As an AI Automation Expert, I’m passionate about simplifying tech and empowering small businesses and young coders through AI automation. With my AI agency, Oki, I create efficient, n8n-powered workflows that save time, streamline operations, and boost growth for SMBs.

Featured*