Gmail Email Vectorization using Ollama and PGVector - n8n Workflow

Use this powerful n8n workflow to automatically turn incoming Gmail messages into vector embeddings using Ollama, storing data in PGVector for similarity search and building a RAG knowledge base.

Workflow Preview

Ready to automate?

Download this n8n workflow template and start using it instantly.

Who is this best for?

• Users building RAG systems based on personal email history.
• Data scientists needing to analyze large volumes of proprietary communication data.
• Developers looking for advanced n8n templates integrating Gmail, Local AI (Ollama), and PostgreSQL.
• Anyone needing a robust n8n workflow for creating a searchable email knowledge base.

Overview

This sophisticated n8n workflow provides a complete, self-hosted solution for transforming your Gmail inbox into a powerful, searchable knowledge base. It uses local AI processing via the Ollama platform (specifically the nomic-embed-text model) to generate high-quality vector embeddings from your email content. The n8n workflow is dual-purpose, featuring an n8n trigger for real-time processing of new emails, and a robust manual bulk import function designed to vectorize your entire historical archive by processing data in weekly batches. All structured email metadata and the resulting vector data are safely stored in separate PostgreSQL tables (emailsmetadata and emailsembeddings), leveraging PGVector for efficient vector similarity searches. This specialized n8n node setup ensures data integrity and scalability, making it one of the most effective n8n templates available for email knowledge base generation.

How it Works

This n8n workflow operates in two distinct modes:


  1. Real-Time Mode (New Emails):

The Gmail Trigger n8n node continuously monitors your INBOX for new messages every minute. Upon detection, the email data flows directly to the Extract email fields n8n node, where metadata is prepared. The data is then stored structurally in PostgreSQL via the Store structured n8n node. Simultaneously, the email text is vectorized using the Embeddings Ollama n8n node and saved to PGVector via the Store vectorized n8n node.


  1. Historical Bulk Import Mode (Past Emails):

When the Manual Trigger n8n node is executed, the n8n workflow initializes by ensuring the PostgreSQL tables are created (Create the table). The custom Code n8n node, Explode interval into weeks, calculates all the weekly periods between your account creation date (which must be edited in the n8n node) and today. This creates multiple items, which are processed sequentially by the Loop Over Items n8n node.

Inside the loop, the Set before and after dates n8n node specifies the weekly range. The Get a batch of messages Gmail n8n node retrieves the emails for that specific week. Each message is processed—extracted, structured, vectorized, and stored—identical to the real-time path. Finally, an If n8n node checks if the process was manual, and if so, ensures the loop continues to the next batch until the entire email history is processed. This efficient batching prevents timeout issues common with large data imports in any n8n workflow.

Installation Guide


  1. Import the n8n Workflow: Import the provided JSON into your n8n instance using the 'Import from JSON' function.

  2. Credential Configuration: Set up necessary credentials for:

PostgreSQL/PGVector: Pointing to your database instance capable of handling vector storage.
Gmail: Ensure the n8n node has permissions to read and monitor your emails.
* Ollama: Ensure your Ollama server is running and accessible to the n8n installation, and that the nomic-embed-text model is available (use ollama pull nomic-embed-text).

  1. Customize the Code n8n node: Edit the Explode interval into weeks n8n node and update the variable whenDidICreateMyGmailAccount to your actual Gmail account creation date. This is crucial for the bulk import function of this n8n template.

  2. Activation: Activate the n8n workflow to enable the continuous Gmail Trigger monitoring. To perform the initial bulk import, simply execute the Manual Trigger n8n node.

Node Details

Manual Trigger (n8n trigger): Used exclusively to initiate the one-time bulk ingestion of historical email data.
Gmail Trigger (n8n trigger): The primary n8n node for production use, monitoring the INBOX label every minute for new emails.
Explode interval into weeks (Code n8n node): A custom n8n node that calculates discrete weekly intervals based on a user-defined start date, preparing the data structure needed for the historical batch loop.
Get a batch of messages (Gmail n8n node): Retrieves emails based on date filters set dynamically by the looping structure.
Extract email fields (Set n8n node): Standardizes and extracts crucial email components (emailtext, emailid, threadid, etc.) into variables accessible by downstream n8n nodes.
Store structured (PostgreSQL n8n node): Inserts or updates the structured metadata into the emails
metadata table using an UPSERT operation based on emailid. Uses error handling (continueErrorOutput) specific to this n8n workflow.
Embeddings Ollama (n8n node): Connects to the local Ollama service to generate vector embeddings using the nomic-embed-text:latest model, a key component for this AI-driven n8n workflow.
Recursive Character Text Splitter (n8n node): Ensures large emails are broken into manageable, context-rich chunks (chunk size 2000, overlap 50) before vectorization, optimizing RAG performance.


  • Store vectorized (PGVector n8n node): Inserts the vector embeddings and associated document metadata into the emailsembeddings table, completing the knowledge base creation process for this powerful n8n workflow.

Related n8n Workflows

Free

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

Software Engineering | Die-hard maker

Featured*