n8n-tweet-workflow-guide

n8n Tweet Management Workflow with PostgreSQL - Setup Guide

Initial Form showing
Full n8n workflow canvas.

Table of Contents

  1. Introduction
  2. Purpose and Features
  3. Requirements
  4. Installation
  5. Backup Process
  6. Usage
  7. Screenshots
  8. Troubleshooting
  9. Support and Community

Introduction

This detailed guide provides everything you need to set up and use the n8n Tweet Management Workflow with PostgreSQL, a robust automation tool for managing tweets. Designed for social media managers, developers, and automation enthusiasts, this workflow streamlines tweet scheduling and storage with intuitive forms and PostgreSQL integration. The guide covers standalone and containerized setups, an optional backup system, usage instructions, and troubleshooting tips to ensure a smooth experience.

Purpose and Features

The n8n Tweet Management Workflow with PostgreSQL offers:

This workflow is ideal for automating repetitive tasks, maintaining data consistency, and creating a reliable tweet archive.

Requirements

Installation

This section provides multiple installation options to accommodate your environment.

Option 1: Standalone n8n Installation

Ideal for users running n8n directly on their machine without containers.

  1. Install Node.js:
    • Download and install Node.js (v16 or higher) from nodejs.org.
    • Verify in a terminal:
      node --version
      npm --version
      
  2. Install n8n:
    • Install globally:
      npm install n8n -g
      
    • Start n8n:
      n8n start
      
    • Access at http://localhost:5678.
  3. Set Up PostgreSQL:
    • Install PostgreSQL from postgresql.org/download.
    • Create a database (e.g., tweet_db):
      CREATE DATABASE tweet_db;
      
    • Create the tweets_table:
      CREATE TABLE tweets_table (
          id INTEGER PRIMARY KEY,
          tweet_date DATE,
          parent_tweet_id INTEGER,
          text TEXT
      );
      
    • Note your database credentials for later.

Option 2: Containerized n8n Installation (Docker)

Uses Docker for a portable, isolated environment, suitable for production or testing.

  1. Install Docker:
  2. Run n8n and PostgreSQL:
    • Create a docker-compose.yml file:
      version: '3'
      services:
        n8n:
          image: n8nio/n8n:latest
          ports:
            - '5678:5678'
          volumes:
            - n8n_data:/home/node/.n8n
          environment:
            - N8N_HOST=localhost
            - N8N_PORT=5678
        postgres:
          image: postgres:14
          environment:
            - POSTGRES_DB=tweet_db
            - POSTGRES_USER=your_user
            - POSTGRES_PASSWORD=your_secure_password
          volumes:
            - postgres_data:/var/lib/postgresql/data
          ports:
            - '5432:5432'
      volumes:
        n8n_data:
        postgres_data:
      
    • Run:
      docker-compose up -d
      
    • Access n8n at http://localhost:5678.
  3. Set Up PostgreSQL Table:
    • Connect to PostgreSQL:
      docker exec -it <postgres_container_name> psql -U your_user -d tweet_db
      
    • Create the tweets_table:
      CREATE TABLE tweets_table (
          id INTEGER PRIMARY KEY,
          tweet_date DATE,
          parent_tweet_id INTEGER,
          text TEXT
      );
      

Optional: Docker Compose with Backup

(Bonus)

This setup includes n8n, PostgreSQL, and a backup service for data protection. Replace placeholders with your values.

version: '3.8'
services:
  postgres:
    image: postgres:latest
    container_name: postgres
    restart: unless-stopped
    environment:
      POSTGRES_USER: your_user
      POSTGRES_PASSWORD: your_secure_password
      POSTGRES_DB: tweet_db
    ports:
      - '5432:5432'
    volumes:
      - postgres_data:/var/lib/postgresql/data
    networks:
      - n8n-network
  n8n:
    image: n8nio/n8n:latest
    container_name: n8n
    restart: unless-stopped
    environment:
      DB_TYPE: postgresdb
      DB_POSTGRESDB_HOST: postgres
      DB_POSTGRESDB_PORT: 5432
      DB_POSTGRESDB_DATABASE: tweet_db
      DB_POSTGRESDB_USER: your_user
      DB_POSTGRESDB_PASSWORD: your_secure_password
      N8N_HOST: localhost
      N8N_PORT: 5678
      WEBHOOK_URL: http://localhost:5678/
    ports:
      - '5678:5678'
    depends_on:
      - postgres
    volumes:
      - n8n_data:/home/node/.n8n
      - /path/to/your/desktop:/desktop
    networks:
      - n8n-network
  backup:
    image: ubuntu
    container_name: backup
    volumes:
      - postgres_data:/data/postgres
      - n8n_data:/data/n8n
      - /path/to/backup/folder:/backup
    entrypoint: /backup/backup.sh
    networks:
      - n8n-network
volumes:
  postgres_data:
  n8n_data:
networks:
  n8n-network:

Steps:

  1. Save as docker-compose.yml.
  2. Replace /path/to/your/desktop with a local folder for file access in the n8n container.
  3. Replace /path/to/backup/folder with a local directory for backups.
  4. Create a backup.sh script in the backup folder: (Bonus)
    #!/bin/bash
    BACKUP_DIR="/backup"
    TIMESTAMP=$(date +%Y%m%d_%H%M%S)
    N8N_BACKUP_FILE="n8n_backup_$TIMESTAMP.tar"
    tar -cvf "$BACKUP_DIR/$N8N_BACKUP_FILE" /data/n8n
    POSTGRES_BACKUP_FILE="postgres_backup_$TIMESTAMP.tar"
    tar -cvf "$BACKUP_DIR/$POSTGRES_BACKUP_FILE" /data/postgres
    cd "$BACKUP_DIR"
    ls -1 n8n_backup_*.tar | sort -r | tail -n +3 | xargs -r rm -f
    ls -1 postgres_backup_*.tar | sort -r | tail -n +3 | xargs -r rm -f
    
  5. Make executable:
    chmod +x /path/to/backup/folder/backup.sh
    
  6. Run:
    docker-compose up -d
    
  7. Check the backup folder for .tar files.

Docker Compose Setup
Screenshot: Docker Compose terminal showing n8n, PostgreSQL, and backup services running.

Importing the Workflow

  1. Open n8n at http://localhost:5678.
  2. Go to Workflows > Import from File.
  3. Select workflow.json from your purchase package.
  4. Save the workflow.

n8n Import/Export Menu
Screenshot: n8n import/export menu with workflow.json selected for import.

Configuring PostgreSQL Credentials

  1. In n8n, go to Credentials > Add Credential > PostgreSQL.
  2. Enter:
    • Host: postgres (Docker) or localhost (standalone)
    • Database: tweet_db
    • User: your_user
    • Password: your_secure_password
    • Port: 5432
  3. Save and assign to Execute a SQL query and Insert_row_to_Tweet_table nodes.

n8n add PostgreSQL credentials
Screenshot: PostgreSQL credentials configured in n8n with correct host, database, user, password, and port.

Backup Process

The backup system ensures your data is safe and recoverable.

Why Backups Are Important

Backups protect against:

How the Backup Service Works

The backup service uses an Ubuntu container to:

Backups are stored in the folder mapped to /path/to/backup/folder. Monitor storage to avoid disk issues.

Restoring from Backups

To restore:

  1. Stop Services:
    docker-compose down
    
  2. Restore PostgreSQL:
    • Extract the backup:
      tar -xvf /path/to/backup/folder/postgres_backup_20250708_152700.tar -C /tmp/postgres_restore
      
    • Copy to the PostgreSQL volume:
      docker cp /tmp/postgres_restore/. <postgres_container_name>:/var/lib/postgresql/data
      
  3. Restore n8n Data:
    • Extract:
      tar -xvf /path/to/backup/folder/n8n_backup_20250708_152700.tar -C /tmp/n8n_restore
      
    • Copy:
      docker cp /tmp/n8n_restore/. <n8n_container_name>:/home/node/.n8n
      
  4. Restart Services:
    docker-compose up -d
    
  5. Verify data in n8n and PostgreSQL.

backup folder
This screenshot shows the backup folder with .tar files, confirming successful creation of n8n and PostgreSQL backups.

Usage

Form Inputs Explained

Running the Workflow

  1. Open the workflow in n8n and activate it.
  2. Copy the first_input node’s webhook URL.
  3. Paste in a browser to access the form.
  4. Submit valid data; verify in tweets_table:
    SELECT * FROM tweets_table ORDER BY id DESC LIMIT 1;
    

Initial Form showing
This screenshot shows the first_input form in a browser, confirming the fields for entering date and parent tweet index.


Tweet Form in browser
Screenshot of the Tweet Form in a browser. Submitting saves the tweet to the database.

Screenshots

Troubleshooting

Support and Community