IOLEBA

 IOLEBA N8N Textbook – Table of Contents

Chapter 7: Database Integrations
Chapter 7

Database Integrations

Introduction

Most business applications rely on databases to store and retrieve data. In this chapter, you'll learn to connect n8n to various database systems, perform CRUD operations (Create, Read, Update, Delete), and build data-driven workflows that interact seamlessly with your data stores.

Understanding Database Types

Databases fall into two main categories, each with different strengths:

SQL (Relational) Databases

Characteristics:
  • Structured data in tables with rows and columns
  • Relationships between tables
  • ACID compliance (reliable transactions)
  • Uses SQL query language
Popular SQL Databases:
  • MySQL: Most common, open-source
  • PostgreSQL: Advanced features, highly reliable
  • Microsoft SQL Server: Enterprise Windows environments
  • SQLite: Lightweight, file-based

NoSQL (Non-Relational) Databases

Characteristics:
  • Flexible schemas (no fixed structure)
  • Horizontal scalability
  • Fast read/write operations
  • Various data models (document, key-value, graph)
Popular NoSQL Databases:
  • MongoDB: Document-based (JSON-like)
  • Redis: In-memory key-value store
  • Firebase: Real-time cloud database
  • DynamoDB: AWS managed NoSQL

Connecting to MySQL/PostgreSQL

Let's start with SQL databases, the most common type for business applications.

Setting Up Database Credentials

  1. Go to Credentials in n8n sidebar
  2. Click "Add Credential"
  3. Select database type (MySQL or PostgreSQL)
  4. Configure connection details
Required Connection Information:
  • Host: Server address (e.g., localhost, db.example.com)
  • Database: Database name
  • User: Username with access permissions
  • Password: User password
  • Port: 3306 (MySQL) or 5432 (PostgreSQL)
⚠️ Security Best Practices:
  • Never use root database account
  • Create dedicated user with minimal required permissions
  • Use SSL/TLS connections when available
  • Restrict database access by IP address
  • Store credentials securely in n8n credential manager

CRUD Operations

CRUD represents the four basic database operations you'll use most often.

Create (INSERT)

Add new records to database tables.

MySQL Node Configuration:
  • Operation: Insert
  • Table: customers
  • Columns: name, email, created_at
Example SQL Generated:
INSERT INTO customers (name, email, created_at) 
VALUES ('John Smith', '[email protected]', NOW())

Read (SELECT)

Retrieve data from database tables.

Common SELECT Patterns:

Get all records:

SELECT * FROM customers

Get specific columns:

SELECT name, email FROM customers

Filter with WHERE:

SELECT * FROM customers 
WHERE created_at > '2025-01-01'

Order results:

SELECT * FROM customers 
ORDER BY created_at DESC 
LIMIT 10

Update (UPDATE)

Modify existing records.

Update Example:
UPDATE customers 
SET status = 'active', updated_at = NOW() 
WHERE email = '[email protected]'
⚠️ Critical Warning: Always use WHERE clause in UPDATE statements! Without it, you'll update ALL records in the table.

Delete (DELETE)

Remove records from tables.

⚠️ Deletion Warning:
DELETE FROM customers 
WHERE id = 123

NEVER run DELETE without WHERE clause! This would delete ALL records.

Best Practice: Consider "soft deletes" - mark records as deleted rather than actually removing them.

Building Data-Driven Workflows

Pattern 1: Database-Triggered Actions

Scenario: Daily Report Generation
  1. Schedule Trigger: Every day at 8 AM
  2. MySQL Node: Query yesterday's sales
  3. Aggregate Node: Calculate totals
  4. Set Node: Format report data
  5. Email Node: Send report to management

Pattern 2: Two-Way Data Sync

Scenario: CRM to Database Sync
  1. Webhook Trigger: CRM sends new contact
  2. MySQL Node: Check if contact exists
  3. IF Node: Exists or new?
  4. True: Update existing record
  5. False: Insert new record

Working with MongoDB

MongoDB is the most popular NoSQL database, storing data as flexible JSON-like documents.

MongoDB Operations

Operation SQL Equivalent Purpose
insertOne/insertMany INSERT Add documents
find SELECT Query documents
updateOne/updateMany UPDATE Modify documents
deleteOne/deleteMany DELETE Remove documents

MongoDB Query Examples

Find all active users:
{ "status": "active" }
Find users created this year:
{ "createdAt": { "$gte": "2025-01-01" } }
Find by multiple conditions:
{ 
  "status": "active",
  "subscription": "premium",
  "credits": { "$gt": 0 }
}

Database Performance Optimization

Query Optimization

Best Practices:
  • Use indexes: Speed up frequently queried fields
  • Limit results: Use LIMIT/TOP to reduce data transfer
  • Select specific columns: Don't use SELECT * in production
  • Filter at database: Use WHERE instead of filtering in workflow
  • Batch operations: Insert/update multiple records at once

Connection Management

Connection Best Practices:
  • Reuse connections across nodes
  • Set appropriate timeout values
  • Handle connection errors gracefully
  • Close connections when workflow ends
  • Monitor connection pool usage

Common Database Workflow Patterns

Pattern 1: Upsert (Insert or Update)

Problem: Don't know if record exists

Solution:

  1. Try to find record by unique identifier
  2. IF found: Update existing record
  3. IF not found: Insert new record

Pattern 2: Bulk Data Import

Importing CSV to Database:
  1. Read Binary File: Load CSV
  2. Split In Batches: Process 100 rows at a time
  3. MySQL Node: Insert batch
  4. Wait: Brief pause between batches
  5. Loop: Continue until all data imported

Pattern 3: Scheduled Database Cleanup

Maintain Database Health:
  1. Schedule Trigger: Weekly
  2. MySQL Node: Delete old records (e.g., logs older than 90 days)
  3. MySQL Node: Optimize tables
  4. Slack Node: Report cleanup results

Error Handling for Database Operations

Common Database Errors:
  • Connection timeout: Database unreachable
  • Duplicate key: Trying to insert existing unique value
  • Constraint violation: Data doesn't meet requirements
  • Permission denied: User lacks necessary privileges
  • Table doesn't exist: Wrong database or table name
Error Handling Strategy:
  1. Enable "Continue On Fail" for database nodes
  2. Use IF node to check for errors
  3. Log errors to external system
  4. Send notifications for critical failures
  5. Implement retry logic for temporary issues

Practice Exercises

Exercise 1: Customer Database Manager

Build a workflow that:

  • Receives customer data via webhook
  • Validates email format
  • Checks if customer exists in database
  • Updates if exists, inserts if new
  • Logs all operations to audit table
Exercise 2: Daily Sales Report

Create automated reporting:

  1. Schedule trigger (daily at 8 AM)
  2. Query database for yesterday's sales
  3. Calculate: total revenue, number of orders, average order value
  4. Identify top 5 selling products
  5. Format as HTML email
  6. Send to management team
Exercise 3: Data Sync Between Systems

Synchronize data across platforms:

  • Schedule trigger (every hour)
  • Get new records from Source Database A
  • Transform data to match Target Database B schema
  • Insert into Target Database B
  • Update sync timestamp in Source Database A

Advanced Database Techniques

Transactions

Ensure multiple operations complete successfully together or none at all.

Use Case: Transferring money between accounts
  • Must deduct from Account A AND add to Account B
  • If either fails, both should be rolled back
  • Prevents data inconsistency

Stored Procedures

Call pre-written database procedures for complex operations.

Benefits:
  • Faster execution (pre-compiled)
  • Reduced network traffic
  • Centralized business logic
  • Enhanced security

Key Takeaways

  • Databases store structured (SQL) or flexible (NoSQL) data
  • CRUD operations are the foundation: Create, Read, Update, Delete
  • Always use WHERE clauses in UPDATE and DELETE statements
  • Optimize queries with indexes, limits, and specific column selection
  • Handle database errors gracefully with retry logic
  • Batch operations improve performance for large datasets
  • Security best practices protect your data

Looking Forward

In Chapter 8, we'll explore API authentication methods. You'll learn OAuth, API keys, JWT tokens, and how to securely connect n8n to protected APIs, enabling integration with virtually any modern web service.

📥 Download This Chapter

Your browser's print dialog will open - select "Save as PDF" as the destination

n8n Textbook | Chapter 7: Database Integrations

© 2025 IOLEBA | Dr. Marcus Lee

Originally Published: November 2025