n8n

How to Sync YouTube to PostgreSQL Analytics?

Capture fresh YouTube video stats and store them in PostgreSQL for clean reporting. Ideal for marketing and content teams that want a simple way to track performance and find top videos from the last two weeks without manual work.

The flow can run by hand or be called from another workflow. For each channel, it reads the latest publish time from the database, then pulls up to 50 newer videos from the YouTube API. It fetches detailed metrics like views, likes, comments, and duration, removes Shorts by checking video length, maps fields into a standard format, and writes rows in bulk to a video_statistics table. A ranking query highlights best performing videos from the last two weeks so teams can plan what to promote next.

You need a YouTube account with API access and a PostgreSQL database. Set the Google API key in n8n and connect YouTube OAuth and PostgreSQL credentials. Expect faster reporting, fewer errors, and consistent data you can plug into dashboards. Use it to monitor channels, measure new uploads, and spot winning content on a steady two week cycle.

What are the key features?

  • Manual run or callable start using Manual Trigger and Execute Workflow Trigger
  • Incremental sync per channel by reading the latest publish time from PostgreSQL
  • YouTube API calls to fetch recent videos and detailed stats including views, likes, comments, and duration
  • Shorts filter that removes very short videos using the duration field
  • Data mapping and cleanup to standard fields like view_count, like_count, comment_count, and publish_time
  • Bulk insert builder that creates a parameterized SQL insert and writes many rows at once
  • Two week performance ranking using a PostgreSQL query to highlight top videos
  • Split in batches loop to process multiple channels without timeouts

What are the benefits?

  • Reduce manual work from weekly hours to a few minutes by pulling new videos automatically
  • Automate 90 percent of repetitive data collection for YouTube reporting
  • Improve data quality by removing copy paste errors and enforcing one clean schema
  • Handle 10 times more channels with batch loops and bulk inserts
  • Connect YouTube and PostgreSQL seamlessly for dashboard ready data
  • Surface winning videos from the last two weeks for quick promotion decisions

How do you set it up?

  1. Import the template into n8n: Create a new workflow in n8n > Click the three dots menu > Select 'Import from File' > Choose the downloaded JSON file.
  2. You'll need accounts with YouTube and PostgreSQL. See the Tools Required section above for links to create accounts with these services.
  3. In the n8n credentials manager, create a YouTube OAuth credential. Double click the YouTube node, choose Credential to connect with, click Create new credential, and follow the on screen steps to sign in.
  4. Create a Google API key for the YouTube Data API v3 in your Google Cloud console. In n8n, add this key as an environment variable named GOOGLE_API_KEY or paste it in the HTTP Request node key field.
  5. In the n8n credentials manager, create a PostgreSQL credential with host, port, database, user, and password. Test the connection to confirm access.
  6. Open the SQL node labeled create_table and run it once to create the video_statistics table if it does not exist. Confirm with the see table node to view the schema.
  7. Prepare the input list of channels. Each item should include a channel id and an optional url. If you call this workflow from another workflow, pass that list to the Execute Workflow Trigger input. For testing, you can paste sample items into the Manual Trigger input.
  8. Check the get_videos node options. It fetches up to 50 items, uses the channel id, and respects the publishedAfter time from the database so only new videos are synced.
  9. Verify the HTTP Request node includes the Google API key and pulls contentDetails and statistics. This is required for the Shorts filter and metrics.
  10. Click Test to run with one channel. After it finishes, open the see table node and run it to confirm new rows are present. If no rows appear, check the already_populated path which means no newer videos were found.
  11. If you see quota or permission errors, review the YouTube credential and API key. If you see database errors, confirm the table name and user rights in PostgreSQL.

Tools Required

$24 / mo or $20 / mo billed annually to use n8n in the cloud. However, the local or self-hosted n8n Community Edition is free.

PostgreSQL

Sign up

Free: $0 (open-source PostgreSQL License; self-hosted)

YouTube

Sign up

Free: $0, default quota 10,000 units/day

Similar Templates

Join Futurise to access 1,200+ automation templates

Get instant access to ready-made automation workflows for n8n, Make.com, AI agents, and more. Download, customise, and deploy in minutes.