Sync Databases with n8n Without Data Loss or Duplication


Learn how to use n8n to sync databases easily using this step-by-step beginner guide.


If you’ve ever struggled with keeping multiple databases in sync, this guide will show you exactly how to use n8n to sync databases effortlessly, without writing complex scripts.

Keeping two databases perfectly synced is one of the most painful tasks for developers, founders, agencies, and data teams.
Tables change. New rows come in. Someone edits an entry. Another app writes to a different DB. Before you know it – everything is out of sync.

If you’ve ever tried writing your own sync scripts… you already know:

  1. Too much code
  2. Too many edge cases
  3. Too much debugging
  4. Too much maintenance when schemas change

That’s where n8n makes your life dramatically easier.

This simple, beginner-friendly tutorial will walk you through how to sync ANY two databases using n8n, even if you’re not a coder.
You will learn how to build one-way sync, two-way sync, conflict handling, and incremental updates.

Whether you’re syncing:

  • MySQL → MySQL
  • MySQL → PostgreSQL
  • PostgreSQL → MySQL
  • PostgreSQL → PostgreSQL
  • MongoDB → MySQL
  • MySQL → Google Sheets

…this guide shows you exactly how to do it.

And the best part?

You can automate all of this without touching backend code.


What You’ll Learn

  • How to use n8n to sync databases
  • How to perform one-way sync
  • How to build two-way sync
  • How to detect new or updated rows
  • How to prevent duplicates
  • How to handle schema differences
  • How to use n8n’s MySQL, PostgreSQL, MongoDB, and Google Sheets nodes
  • Real use-cases of database sync
  • Final workflow structure you can replicate

Step-by-Step Guide: How to Use n8n to Sync Databases

Below is the complete step-by-step tutorial for beginners.
Everything is written in plain English.
Each step includes how-to instructions, pro tips, and code where required.

Step 1: Set Up Your n8n Environment

Before syncing anything, set up n8n.

How-To

  • Go to n8n.io
  • Create a free cloud account OR install self-hosted
  • Open your dashboard
  • Click “New Workflow”

Pro Tip

If you’re syncing production databases, always use a self-hosted instance for privacy.

Common Mistake

People often skip enabling SSL connections. Always enable SSL on production DBs.


Step 2: Add Your Database Credentials

In this tutorial, you’ll learn how to work with:

  • MySQL
  • PostgreSQL
  • MongoDB
  • Google Sheets

How-To (Credentials Setup)

MySQL Credentials

Host: your-host
Port: 3306
User: your-user
Password: your-password
Database: your-db
SSL: recommended

PostgreSQL Credentials

Host: your-host
Port: 5432
User: your-user
Password: your-password
Database: your-db
SSL: recommended

MongoDB Example URI

mongodb+srv://user:password@cluster.mongodb.net/?retryWrites=true&w=majority

Google Sheets Credentials

Export your Google service account JSON and paste it into n8n.
Another method of how to use n8n to connect APIs is through OAuth2 credentials.


Step 3: Fetch Data from the Source Database

The source database is where the data originates. Learn how to use n8n to sync databases effortlessly.

Choose a node:

  • MySQL Node
  • Postgres Node
  • MongoDB Node

How-To

  • Drag MySQL / Postgres / MongoDB node
  • Choose “Execute Query”
  • Add your query
Example SELECT query:
SELECT * FROM users WHERE updated_at > {{ $json.lastSyncTime }};

Why This Matters

By filtering using updated_at, you avoid re-syncing the entire database every time. Finish the n8n MongoDB Integration Guide and continue with the setup.


Step 4: Process the Data (Optional)

If the structure is different between databases, convert it.

Use the Function Node.

Example transform:

return items.map(item => {
  return {
    json: {
      id: item.json.id,
      name: item.json.full_name,
      email: item.json.email,
      updated_at: item.json.updated_at
    }
  }
});

Shortcut

Use Set Node if you only need small field remapping.


Step 5: Insert or Update Data in Target Database

This is where the sync happens.

Choose the correct node:

  • MySQL Node
  • PostgreSQL Node
  • MongoDB Node
  • Google Sheets Node

Sample UPSERT Query (MySQL → MySQL)

INSERT INTO users (id, name, email, updated_at)
VALUES ({{$json.id}}, {{$json.name}}, {{$json.email}}, {{$json.updated_at}})
ON DUPLICATE KEY UPDATE
name = VALUES(name),
email = VALUES(email),
updated_at = VALUES(updated_at);

Sample PostgreSQL UPSERT

INSERT INTO users (id, name, email, updated_at)
VALUES ($1, $2, $3, $4)
ON CONFLICT (id) DO UPDATE SET
name = EXCLUDED.name,
email = EXCLUDED.email,
updated_at = EXCLUDED.updated_at;

Rectify the errors of PostgreSQL and continue with the setup

MongoDB Insert/Update

Use Update Many or Upsert: true.

Google Sheets Sync

Use Append Row or Update Row.


Step 6: Build One-Way Sync (ALL COMBINATIONS)

Before diving in, it’s essential to understand how to use n8n to sync databases for one-way and two-way data flows.
Below is how one-way sync works for every pair:

Source → Target Recommended Nodes Notes
MySQL → MySQL MySQL → MySQL Easiest sync
MySQL → PostgreSQL MySQL → Set → Postgres Map datatypes
PostgreSQL → MySQL Postgres → Set → MySQL Watch Boolean fields
PostgreSQL → PostgreSQL Postgres → Postgres Straightforward
MongoDB → MySQL MongoDB → Function → MySQL Convert JSON to SQL rows
MySQL → Google Sheets MySQL → Google Sheets Good for dashboards

Pro Tip

Always log the synced records using a “Write to File” or “Table Insert” node.


Step 7: Build Two-Way Sync (Advanced But Easy in n8n)

Learn how to use n8n to sync databases effortlessly. Step-by-step beginner guide to automate two-way database syncs without coding.
Two-way sync means:

  • Sync A → B
  • Sync B → A
  • Detect conflicts
  • Only overwrite newer rows

Basic Logic

IF updated_at_source > updated_at_target
   overwrite target
ELSE
   skip update

How-To

  1. Read from Database A
  2. Read from Database B
  3. Compare timestamps
  4. UPSERT into both databases
  5. Store last sync time

Example Conflict Resolution Function

if ($json.source.updated_at > $json.target.updated_at) {
  return [{
    json: {
      ...$json.source,
      action: "update_target"
    }
  }];
} else {
  return [{
    json: {
      ...$json.target,
      action: "update_source"
    }
  }];
}

Why Two-Way Sync Matters

If two apps modify different databases separately, two-way sync prevents data loss.


Step 8: Schedule the Sync Automatically

Use the Cron Node.

Recommended frequencies:

Database Sync Frequency Notes
MySQL Every 1–5 minutes Fast transactions
PostgreSQL Every 5 minutes Stable
MongoDB Every 10 minutes Heavy documents
Google Sheets Every 15 minutes Rate limit

Shortcut

Use “Execute Workflow Trigger” if you want manual syncing.
Many automation enthusiasts ask, ‘What’s the best approach to use n8n to sync databases across different platforms like MySQL and PostgreSQL?


Step 9: Test Your Full Workflow

Testing is crucial.

Test Checklist

  • Test with small dataset
  • Verify timestamps
  • Check for duplicates
  • Check schema mismatch
  • Test both directions (two-way sync)
  • Log everything

IMPORTANT TABLE: Node/Step Details

Step What It Does Notes
Source Query Node Reads data from DB A Use filters to reduce load
Function/Set Node Transform fields Fix datatype mismatches
Target DB Insert Node Writes data into DB B Use UPSERT
Reverse Sync Node Writes data into DB A Only for two‑way sync
Cron Automates sync Set frequency carefully


Real Use-Cases

1. Sync MySQL ERP to PostgreSQL Analytics

Problem: ERP uses MySQL but the BI dashboard uses PostgreSQL.
Solution: n8n syncs new + updated rows every 5 minutes. (Click here)

2. Sync PostgreSQL Production to MySQL Legacy System

Problem: Old system still runs MySQL but production has moved to PostgreSQL.
Solution: One-way sync keeps the old app alive without maintenance. (Click here)

3. Sync MongoDB Leads to MySQL CRM

Problem: Leads captured in MongoDB need to be added to CRM (MySQL).
Solution: n8n converts Mongo documents and inserts into SQL. (Click here)

4. Sync MySQL Sales Data to Google Sheets Dashboard

Problem: Team wants live spreadsheets without exporting CSV.
Solution: Automated sync every 10 minutes. (Click here)

5. Two-Way Sync Between Two PostgreSQL Databases

Problem: Remote teams use separate DBs but must stay aligned.
Solution: Two-way sync with conflict resolution. (Click here)


Workflow Snapshot (Simple Summary)

Your final n8n workflow will:

  • Step 1 → Trigger (Cron)
  • Step 2 → Fetch data from Database A
  • Step 3 → Fetch data from Database B
  • Step 4 → Transform fields
  • Step 5 → Compare timestamps
  • Step 6 → UPSERT into Database B
  • Step 7 → UPSERT into Database A (for two-way sync)
  • Step 8 → Log the results

Workflow Setup in n8n to Sync Databases


Conclusion

Syncing databases used to require hours of coding, custom scripts, and constant debugging.
But with n8n, you can build a complete one-way or two-way database sync without writing a backend or maintaining cron jobs manually. Once you know how to use n8n to sync databases, you can automate workflows, prevent duplicates, and handle schema changes seamlessly.

FAQ

Question: "Can I use n8n to sync databases in real-time?"
Answer: "Yes! Learning how to use n8n to sync databases allows you to schedule automated syncs that run every few minutes, ensuring your data stays up-to-date."

You now know how to:

  • Connect multiple databases
  • Sync MySQL, PostgreSQL, MongoDB, and Google Sheets
  • Build one-way + two-way sync
  • Fix datatype mismatches
  • Automate everything with Cron

Start small, test with sample data, and once you’re confident, deploy it to production.

Your data can stay in sync forever with zero maintenance.

Now… go build your first sync workflow.
Once you’ve done one, everything else feels incredibly simple.

 

Leave a Comment

Your email address will not be published. Required fields are marked *