Automate YouTube Video Analysis & Database Storage with n8n

This n8n workflow automates fetching YouTube video stats from specific channels, filters top-performing recent videos, and stores them in a PostgreSQL database. It saves hours of manual data collection and ensures you track trending content effectively.
postgres
youTube
code
+7
Workflow Identifier: 1991
NODES in Use: splitInBatches, httpRequest, manualTrigger, executeWorkflowTrigger, postgres, youTube, code, if, set, stickyNote
Automate YouTube video analysis with n8n and PostgreSQL

Press CTRL+F5 if the workflow didn't load.

Learn how to Build this Workflow with AI:

What This Automation Does ⚙️

This workflow helps you pull new videos from many YouTube channels automatically.
It skips very short videos under 3.5 minutes, so you get only full-length content.
You compare new video views to the usual average views of each channel.
Videos published in the last two weeks with more views than normal get saved into a PostgreSQL database.
This makes finding important trending videos easy without manual work.


Beginner Step-by-Step: How to Use This Workflow in n8n

Step 1: Import Workflow

  1. Download the workflow using the Download button on this page.
  2. Go to the n8n editor where you want to run the workflow.
  3. Use the “Import from File” option to load the downloaded workflow.

Step 2: Configure Credentials and Variables

  1. Add your Google API credentials into the YouTube node.
  2. Add your PostgreSQL database credentials into the Postgres nodes.
  3. Update YouTube channel IDs, database table names, or any emails if needed.

Step 3: Test and Activate

  1. Run the workflow manually once to check if it works right.
  2. Look for any errors or missing data.
  3. Activate the workflow for automatic running when ready.

If you want more control, consider self-host n8n to run on your own server.


Inputs, Processing, and Outputs

Inputs

  • YouTube API with channel IDs and API key: For fetching recent video data.
  • PostgreSQL database: For storing video stats and checking last stored publish times.

Processing Steps

  • Use SplitInBatches node to handle each YouTube channel one by one.
  • Query PostgreSQL to find the latest video publish time for that channel.
  • Call the YouTube API to get videos published after that time or within last 3 months.
  • Request video details (duration and stats) via an HTTP Request node.
  • Use a Code node to skip videos shorter than 3.5 minutes (YouTube Shorts).
  • If videos remain, map and format their data with Set nodes.
  • Build a dynamic SQL Insert query to add new video stats to the PostgreSQL table.
  • Insert the data using a Postgres node.
  • Run a SQL query to find videos from last 14 days that have double the average views and good engagement.

Outputs

  • Stored video statistics include views, likes, comments, publish times, and channel IDs.
  • A list of top-performing recent videos ready for team review.

Why Build This Workflow

You save around 5 hours each week from manual searching and data copying.
It prevents mistakes that happen when copying data by hand.

The workflow keeps the database up to date with only new videos.
It points out videos that are doing much better than usual.

This quickens your response to trends and helps make better content plans.


Customization Ideas ✏️

  • Change the 14-day filter in the SQL query to any timeframe wanted.
  • Add more video details like tags or description by adjusting API calls and database.
  • Process multiple channels at the same time by changing the batch size but watch API limits.

Troubleshooting ????

  • Empty Video List from YouTube API
    Check the date formats in PostgreSQL and in YouTube API filter.
    Add buffer hours if needed to avoid missing videos due to timezone differences.
  • Duplicate Key Errors on Database Insert
    Video IDs may already exist.
    Use UPSERT (ON CONFLICT) logic or clear entries before insert if duplicates are not allowed.

Pre-Production Checklist ✅

  • Have a valid Google API Key and OAuth2 credentials with YouTube Data API permissions.
  • Make sure PostgreSQL connection works and table video_statistics is ready.
  • Run manual tests on small channel sets to verify workflow parts.
  • Plan API quota usage carefully to avoid hitting limits.

Deployment Guide

Turn the workflow on in n8n by activating it.
Set a schedule for how often it runs (daily, weekly, or manual).

Watch logs regularly for errors.
Keep backups of the database and archive old data to keep performance smooth.


Tools and Services Used

  • YouTube Data API v3: Fetches video metadata and statistics.
  • PostgreSQL Database: Stores video info and calculates averages.
  • n8n Automation platform: Runs and connects all parts.

Summary

✓ Automatically pulls new videos from many YouTube channels.
✓ Skips short videos to focus on meaningful content.
✓ Finds videos from last two weeks with higher views than normal.
✓ Saves detailed video statistics to PostgreSQL database.
✓ Saves you about 5 hours weekly and reduces errors.
✓ Makes it easier to spot trends and make fast decisions.


Automate YouTube video analysis with n8n and PostgreSQL

Visit through Desktop to Interact with the Workflow.

Author
Written By
Vikash Kumar
Building AI agents, n8n workflows and end-to-end automation for 30+ Brands across India, the US, Europe, Dubai & Australia. 7+ years of Experience saving founders real hours every week - no code required.
Author
Written By
Vikash Kumar
Building AI agents, n8n workflows and end-to-end automation for 30+ Brands across India, the US, Europe, Dubai & Australia. 7+ years of Experience saving founders real hours every week - no code required.

Frequently Asked Questions

The publishedAfter filter date may be wrong due to timezone or format. Check date values and add hours buffer to fix.
Use UPSERT logic with ON CONFLICT or clear existing video IDs before inserts to prevent duplicates.
Yes, but SQL queries and node configurations must be adapted for MySQL syntax and connection.
Modify the API request in HTTP node to include new fields, update the mapping node, and adjust SQL table and insert query.

Related Workflows

Automate Twist Channel Creation and Messaging with n8n

This workflow automates creating and updating a channel in Twist and sending a personalized message to specific users. It eliminates manual setup errors and saves time managing Twist communications.

Automate Ideogram Image Generation with Google Sheets & Gmail

This workflow automates graphic design image generation via Ideogram AI, storing image data in Google Sheets and Google Drive, with email alerts via Gmail. It saves designers hours by automating image creation, remixing, review, and record-keeping.

Automate IT Support with Slack and OpenAI in n8n

Streamline IT support by automating Slack message handling using n8n and OpenAI. This workflow handles Slack DMs, filters bots, queries a Confluence knowledge base, and delivers AI-generated responses, improving support efficiency and response time.

Automate Crypto Analysis with CoinMarketCap & n8n AI Agent

Discover how this unique n8n workflow leverages CoinMarketCap’s multi-agent AI to deliver precise, real-time cryptocurrency insights directly via Telegram. Manage crypto data analysis efficiently with automated multi-source API integration.

Automate Gumroad to Beehiiv Subscriber Sync with n8n

Learn how to automatically add new Gumroad sales customers as Beehiiv newsletter subscribers using n8n automation. This workflow saves time by syncing sales data to Google Sheets CRM and notifying your Telegram channel instantly.

Generate On-Brand Blog Articles Using n8n and OpenAI

This workflow automates the creation of on-brand blog articles by analyzing existing company content using n8n and OpenAI. It extracts article structures and brand voice to produce consistent draft articles, saving significant content creation time.
1:1 Free Strategy Session
Your competitors are already automating. Are you still paying for it manually?

Do you want to adopt AI Automation?

Every hour your team does repetitive work, you're burning real money.
While you wait, faster businesses are cutting costs and moving quicker.
AI and automations aren't the future anymore — they're the present.

Book a live 1-on-1 session where we show you exactly which of your daily tasks can be automated — and what it’s costing you not to.