Automate Gmail Email Embeddings with PGVector & Ollama in n8n

Discover how this n8n workflow automates extracting Gmail emails, creating vector embeddings with Ollama, and storing data in PostgreSQL with PGVector. Save hours on email analysis with automated vector similarity search preparation.
gmailTrigger
embeddingsOllama
postgres
+13
Workflow Identifier: 1173
NODES in Use: gmailTrigger, set, splitOut, splitInBatches, set, code, gmail, set, postgress, embeddingsOllama, documentDefaultDataLoader, textSplitterRecursiveCharacterTextSplitter, vectorStorePGVector, if, noOp, manualTrigger
Automate Gmail emails with n8n and Ollama

Press CTRL+F5 if the workflow didn't load.

Learn how to Build this Workflow with AI:

What This Workflow Does

This workflow listens for new Gmail emails every minute.
It takes full email details like text, sender, receivers, date, subject, and attachments.
Then, it turns the email text into vector numbers using the Ollama embedding model.
It saves the email info and vectors into a PostgreSQL database with PGVector support.
You get a structured and searchable email database that helps find similar emails fast.

This helps save many hours manually sorting emails and avoids missing important info.
The setup also lets you import old emails in weekly batches easily.


Who Should Use This Workflow

If you get many Gmail emails and want to analyze or search them quickly, this is useful.
It fits teams and analysts who spend too much time organizing emails by hand.

The workflow works well for anyone wanting to build an AI-ready email search system.
You do not need deep coding skills but need basic n8n familiarity.


Tools and Services Used

  • Gmail API: Fetches emails with metadata and attachments.
  • Ollama API: Creates vector embeddings from email texts using the nomic-embed-text:latest model.
  • PostgreSQL Database: Stores email metadata in emails_metadata and vector embeddings in emails_embeddings tables.
  • PGVector Extension: Enables vector storage and similarity search in PostgreSQL.
  • n8n Automation Platform: Runs all nodes and logic connecting Gmail, embeddings, and database.

Beginner Step-by-Step: How to Use This Workflow in n8n

Step 1: Import Workflow

  1. Download the workflow file using the Download button on this page.
  2. Open the n8n editor where automation is created.
  3. Use the Import from File option to add the workflow to n8n.

Step 2: Configure Credentials and Settings

  1. Add your Gmail API credentials in the Gmail Trigger and Gmail nodes.
  2. Set PostgreSQL credentials in the nodes that connect to the database.
  3. Enter your Ollama API details in the Embeddings Ollama node.
  4. Update any IDs, email addresses, table names, or dates if needed for your account.

Step 3: Test the Workflow

  1. Run the workflow manually once to check connections and see if the database tables get created.
  2. Verify that emails import to the database and embeddings generate correctly.

Step 4: Activate for Production

  1. Turn on the Gmail Trigger node to start automatic processing every minute.
  2. Watch executions in the n8n dashboard and check database contents for data.
  3. Adjust batch sizes or polling intervals if needed to avoid hitting API limits.

For users self hosting n8n, you can visit self-host n8n for setup resources.


How the Workflow Works: Input → Process → Output

Input

  • New Gmail emails arriving in the INBOX label checked every minute.
  • Optionally, initial bulk import from historical email data by week intervals.

Processing Steps

  • Extracts full email metadata: sender, receivers, subject, date, text, attachments.
  • Generates semantic vector embeddings from the email text via Ollama API.
  • Upserts email metadata into emails_metadata PostgreSQL table.
  • Inserts vector embeddings linked by email_id into emails_embeddings table using PGVector.
  • Loops through batches when importing history, handling API rate limits carefully.

Output

  • A structured PostgreSQL database of emails indexed with vector embeddings.
  • Ready for fast similarity search and AI-driven analysis.
  • Less manual work organizing emails and fewer missed insights.

Edge Cases and Possible Failures

  • Gmail API may fail if the credentials expired or lack permissions.
  • Duplicate email ID conflicts can happen if email source sends repeated messages.
  • Embedding generation might fail with wrong model name or Ollama API downtime.
  • Database insert errors if PGVector extension is missing or table schema differs.
  • API limits require tuning batch sizes or polling frequency.

Customization Ideas

  • Change embedding model by editing Embeddings Ollama node’s model parameter.
  • Adjust the starting date in the weekly batch code node to import more or less email history.
  • Enable or disable attachment processing in Gmail Trigger and metadata extraction.
  • Tune batch sizes in SplitInBatches to balance speed and API limits.
  • Expand the PostgreSQL tables to add more email fields like flags or labels.

Summary

✓ Saves many manual hours by automating Gmail email import and indexing.
✓ Builds a searchable, vectorized email database with metadata and embeddings.
✓ Supports live email processing and batch historical import.
✓ Enables fast email similarity search for better analysis.
✓ Easy to configure and run inside n8n with simple steps.


Automate Gmail emails with n8n and Ollama

Visit through Desktop to Interact with the Workflow.

Frequently Asked Questions

Gmail node failures usually happen due to wrong credentials or missing API permissions. Check the Gmail credentials and enable mail read access in API scopes.
Database errors on email_id duplicates occur if emails are repeated or constraints are strict. Verify email_id uniqueness and adjust the upsert logic in the PostgreSQL node.
Embedding failures often come from typos in the model name or Ollama API connectivity problems. Confirm the model string and test the Ollama connection.
Yes, using batch fetching by weekly date intervals and rate control, the workflow scales to large Gmail inboxes. Adjust batch sizes as needed.

Promoted by BULDRR AI

Related Workflows

Automate Twist Channel Creation and Messaging with n8n

This workflow automates creating and updating a channel in Twist and sending a personalized message to specific users. It eliminates manual setup errors and saves time managing Twist communications.

Automate Ideogram Image Generation with Google Sheets & Gmail

This workflow automates graphic design image generation via Ideogram AI, storing image data in Google Sheets and Google Drive, with email alerts via Gmail. It saves designers hours by automating image creation, remixing, review, and record-keeping.

Automate IT Support with Slack and OpenAI in n8n

Streamline IT support by automating Slack message handling using n8n and OpenAI. This workflow handles Slack DMs, filters bots, queries a Confluence knowledge base, and delivers AI-generated responses, improving support efficiency and response time.

Automate Crypto Analysis with CoinMarketCap & n8n AI Agent

Discover how this unique n8n workflow leverages CoinMarketCap’s multi-agent AI to deliver precise, real-time cryptocurrency insights directly via Telegram. Manage crypto data analysis efficiently with automated multi-source API integration.

Automate Gumroad to Beehiiv Subscriber Sync with n8n

Learn how to automatically add new Gumroad sales customers as Beehiiv newsletter subscribers using n8n automation. This workflow saves time by syncing sales data to Google Sheets CRM and notifying your Telegram channel instantly.

Generate On-Brand Blog Articles Using n8n and OpenAI

This workflow automates the creation of on-brand blog articles by analyzing existing company content using n8n and OpenAI. It extracts article structures and brand voice to produce consistent draft articles, saving significant content creation time.
1:1 Free Strategy Session
Your competitors are already automating. Are you still paying for it manually?

Do you want to adopt AI Automation?

Every hour your team does repetitive work, you're burning real money.
While you wait, faster businesses are cutting costs and moving quicker.
AI and automations aren't the future anymore — they're the present.

Book a live 1-on-1 session where we show you exactly which of your daily tasks can be automated — and what it’s costing you not to.