n8n

How to Automate Gmail Email Archiving and Search?

Turn Gmail messages into a searchable knowledge base. Emails are saved to a Postgres database and also converted into vector embeddings for smart search. This helps teams find past conversations fast and review large inboxes without digging through threads.

Two paths keep the data complete. A manual run slices your mailbox history into weekly ranges, pulls each batch from Gmail, extracts clean fields like subject, sender, recipients, dates, and text, and writes them to a structured table. The text is split into readable chunks, embedded with the nomic embed text model in Ollama, and stored in a pgvector table for similarity search. A live Gmail trigger checks the inbox every minute and applies the same steps to new messages so your index stays fresh.

Setup needs a Gmail account, a PostgreSQL database with the pgvector extension, and an Ollama server running the embedding model. You add your Gmail account start date in the code node for the bulk import, test a small date range, then switch on the trigger. Expect one source of truth for email metadata and a vector store that supports fast related message lookup, customer thread research, and simple email analytics.

What are the key features?

  • Manual bulk import that slices history into weekly ranges for safe batch pulls
  • Gmail trigger polls the inbox every minute to capture new messages and attachments
  • Field extraction maps text, subject, from, to, date, message id, and thread id into structured rows
  • Postgres storage writes metadata into an emails_metadata table
  • Text splitter breaks long messages into 2000 character chunks with small overlap
  • Ollama embeddings use the nomic embed text model to create vector representations
  • Vector storage saves embeddings into a pgvector table with links back to message and thread ids
  • Table creation step ensures required tables exist before importing data

What are the benefits?

  • Reduce manual inbox searching from hours to minutes with semantic search across full history
  • Automate 100 percent of new email logging so your archive stays current
  • Improve data quality by storing clean fields like subject, sender, recipients, dates, and ids
  • Handle large mailboxes by processing history in weekly batches without timeouts
  • Connect Gmail, PostgreSQL, and Ollama in one reliable pipeline

How do you set it up?

  1. Import the template into n8n: Create a new workflow in n8n > Click the three dots menu > Select 'Import from File' > Choose the downloaded JSON file.
  2. You'll need accounts with Gmail, PostgreSQL and Ollama. See the Tools Required section above for links to create accounts with these services.
  3. In the n8n credentials manager, open the Gmail Trigger and Gmail nodes. In the Credential to connect with dropdown, click Create new credential. Choose Google OAuth2 if prompted and follow the on screen steps to sign in and allow access. Save and test the connection.
  4. Prepare PostgreSQL. Ensure the pgvector extension is installed and enabled by running CREATE EXTENSION IF NOT EXISTS vector; in your database. In n8n, create a new PostgreSQL credential with host, port, database, user, and password. Click Test to confirm it connects.
  5. Set up Ollama. Install and run Ollama on a reachable host and pull the model by running ollama pull nomic-embed-text. In n8n, open the Embeddings Ollama node, choose Create new credential, set the base URL of your Ollama server, and save. If unsure, follow the on screen instructions.
  6. Open the node named Explode interval into weeks. Edit the code to set your Gmail account creation date so the bulk import covers the correct start point. Save the node.
  7. Review the Create the table and Store structured nodes. Confirm schema and table names match your database. Adjust if needed and save.
  8. Click Test workflow to run a small backfill. Check your emails_metadata and emails_embeddings tables in PostgreSQL to confirm rows are written and embeddings exist.
  9. Open the Gmail Trigger node and confirm the INBOX label filter and the every minute polling option are set. Activate the workflow to keep new messages synced.
  10. Troubleshoot if needed. If embeddings are missing, verify the Ollama server URL and that the nomic-embed-text model is loaded. If Gmail rate limits, shorten the weekly ranges. If Postgres errors mention vector type, confirm the pgvector extension is enabled.

Tools Required

$24 / mo or $20 / mo billed annually to use n8n in the cloud. However, the local or self-hosted n8n Community Edition is free.

Gmail

Sign up

No cost: Personal Gmail (Gmail API has no usage-based pricing; quotas apply)

Ollama

Sign up

Free tier: $0 (self-hosted local API)

PostgreSQL

Sign up

Free: $0 (open-source PostgreSQL License; self-hosted)

Similar Templates

Join Futurise to access 1,200+ automation templates

Get instant access to ready-made automation workflows for n8n, Make.com, AI agents, and more. Download, customise, and deploy in minutes.