Skip to main content

Sync Gmail emails to PostgreSQL with S3 attachment storage

Workflow preview

Workflow preview
100%
Sync Gmail emails to PostgreSQL with S3 attachment storage preview
Open on n8n.io

Important notice

This workflow is provided as-is. Please review and test before using in production.

1. Workflow Overview

Sync Gmail emails to PostgreSQL with S3 attachment storage Automated Gmail Email Processing System Who's it for Businesses and individuals who need to: Archive email communications in a searchable ...

Best for

  • Document Extraction automation workflows
  • advanced n8n builders looking for reusable templates

Tools used

n8n-nodes-base.stickynote, n8n-nodes-base.splitinbatches, n8n-nodes-base.code, n8n-nodes-base.set, n8n-nodes-base.merge, n8n-nodes-base.aggregate, n8n-nodes-base.postgres, n8n-nodes-base.filter

Source and attribution

This workflow is cataloged by N8N Workflows and links back to its original n8n.io source page by Jose Cuartas.

Original n8n.io source

1.1 Workflow description

Title
Sync Gmail emails to PostgreSQL with S3 attachment storage
Workflow name
Sync Gmail emails to PostgreSQL with S3 attachment storage

Sync Gmail emails to PostgreSQL with S3 attachment storage

Automated Gmail Email Processing System

Who's it for

Businesses and individuals who need to:

  • Archive email communications in a searchable database
  • Backup email attachments to cloud storage
  • Analyze email patterns and communication data
  • Comply with data retention policies
  • Integrate emails with other business systems

What it does

This workflow automatically captures, processes, and stores Gmail emails in a PostgreSQL database while uploading file attachments to S3/MinIO storage. It handles both individual emails (via Gmail Trigger) and bulk processing (via Schedule Trigger).

Key features:

  • Dual processing: real-time individual emails + scheduled bulk retrieval
  • Complete email metadata extraction (sender, recipients, labels, timestamps)
  • HTML to plain text conversion for searchable content
  • Binary attachment processing with metadata extraction
  • Organized S3/MinIO file storage structure
  • UPSERT database operations to prevent duplicates

How it works

  1. Email Capture: Gmail Trigger detects new emails, Schedule Trigger gets bulk emails from last hour
  2. Parallel Processing: Emails with attachments go through binary processing, others go directly to transformation
  3. Attachment Handling: Extract metadata, upload to S3/MinIO, create database references
  4. Data Transformation: Convert Gmail API format to PostgreSQL structure
  5. Storage: UPSERT emails to database with linked attachment information

Requirements

Credentials needed:

  • Gmail OAuth2 (gmail.readonly scope)
  • PostgreSQL database connection
  • S3/MinIO storage credentials

Database setup: Run the provided SQL schema to create the messages table with JSONB fields for flexible data storage.

How to set up

  1. Gmail OAuth2: Enable Gmail API in Google Cloud Console, create OAuth2 credentials
  2. PostgreSQL: Create database and run the SQL schema provided in setup sticky note
  3. S3/MinIO: Create bucket "gmail-attachments" with proper upload permissions
  4. Configure: Update authenticatedUserEmail in transform scripts to your email
  5. Test: Start with single email before enabling bulk processing

How to customize

  • Email filters: Modify Gmail queries (in:sent, in:inbox) to target specific emails
  • Storage structure: Change S3 file path format in Upload node
  • Processing schedule: Adjust trigger frequencies based on email volume
  • Database fields: Extend PostgreSQL schema for additional metadata
  • Attachment types: Add file type filtering in binary processing logic

Note: This workflow processes emails from the last hour to avoid overwhelming the system. Adjust timeframes based on your email volume and processing needs.

1.2 Logical Blocks

This catalog entry is organized from the workflow JSON. The node-level section below shows the executable blocks available for review before importing the template.

2. Block-by-Block Analysis

Block 1 - SYSTEM OVERVIEW

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 2 - SETUP REQUIREMENTS

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 3 - SYSTEM TRIGGERS

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 4 - EMAIL RETRIEVAL

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 5 - ATTACHMENT PROCESSING

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 6 - DATA MAPPING

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 7 - DATA TRANSFORMATION

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 8 - DATABASE STORAGE

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 9 - SCRIPT: Extract Binary

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 10 - SCRIPT: Email Transform

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 11 - Process Items Loop

Type / Role
n8n-nodes-base.splitInBatches - splitInBatches
Config choices
Version 3

Block 12 - Extract Binary Metadata

Type / Role
n8n-nodes-base.code - code
Config choices
Version 2

Block 13 - Link Email Attachments

Type / Role
n8n-nodes-base.code - code
Config choices
Version 2

Block 14 - Structure Fields

Type / Role
n8n-nodes-base.set - set
Config choices
Version 3.4

Block 15 - Final Merge

Type / Role
n8n-nodes-base.merge - merge
Config choices
Version 3.2

Block 16 - Aggregate Attachments

Type / Role
n8n-nodes-base.aggregate - aggregate
Config choices
Version 1

Block 17 - Insert or Update Database

Type / Role
n8n-nodes-base.postgres - postgres
Config choices
Version 2.6

Block 18 - Binary Data Filter

Type / Role
n8n-nodes-base.filter - filter
Config choices
Version 2.2

Block 19 - Upload to Storage

Type / Role
n8n-nodes-base.s3 - s3
Config choices
Version 1

Block 20 - Flow Convergence

Type / Role
n8n-nodes-base.noOp - noOp
Config choices
Version 1

Block 21 - End Loop

Type / Role
n8n-nodes-base.noOp - noOp
Config choices
Version 1

Block 22 - Get Individual Message

Type / Role
n8n-nodes-base.gmail - gmail
Config choices
Version 2.1

Block 23 - Get Many Sent

Type / Role
n8n-nodes-base.gmail - gmail
Config choices
Version 2.1

Block 24 - Get Many Inbox

Type / Role
n8n-nodes-base.gmail - gmail
Config choices
Version 2.1

Showing the first 24 of 30 workflow blocks. Download the JSON for the full node graph.

3. Summary Table

Workflow Sync Gmail emails to PostgreSQL with S3 attachment storage
Complexity advanced
Nodes 30
Categories Document Extraction
Author Jose Cuartas
Published 23 Sept 2025

4. Reproducing the Workflow from Scratch

  1. 1. Download the workflow JSON

    Use the JSON export at /data/workflows/8871/8871.json as the source template for this automation.

  2. 2. Import the template into n8n

    Open n8n, import the downloaded JSON, and review each node before activating the workflow.

  3. 3. Configure credentials and variables

    Replace placeholder credentials, API keys, webhook URLs, account IDs, and environment-specific values with your own settings.

  4. 4. Test with sample data

    Run the workflow manually or in a staging workspace, inspect node output, and confirm downstream systems receive the expected data.

  5. 5. Activate and monitor

    Enable the workflow only after testing, then monitor executions, errors, and rate limits during the first production runs.

5. General Notes & Resources

Review imported nodes carefully before activation. This catalog entry is intended to help you inspect the workflow structure, understand required services, and find related templates faster.

Node names, credentials, schedules, webhook paths, and external service limits may need adjustment for your workspace.

Frequently asked questions

What does Sync Gmail emails to PostgreSQL with S3 attachment storage do?

Sync Gmail emails to PostgreSQL with S3 attachment storage Automated Gmail Email Processing System Who's it for Businesses and individuals who need to: Archive email communications in a searchable ...

What do I need before importing this workflow?

Review the workflow JSON, configure any required credentials in n8n, and test the automation in a safe workspace before using it in production.

Can I customize this workflow?

Yes. Use the block-by-block analysis and the downloadable JSON to inspect each node, then adjust credentials, prompts, schedules, filters, or destinations for your Document Extraction use case.