🔓 Unlock all 10,000+ workflows & prompts free Join Newsletter →
✅ Full access unlocked — explore all 10,000 AI workflow and prompt templates Browse Templates →
Home n8n Workflow
January 22, 2026

Apify to PostgreSQL, Instagram leads captured clean

Lisa Granqvist Partner Workflow Automation Expert

Instagram lead sourcing gets messy fast. You find a profile, you copy a URL, you hunt for an email in a bio, and then you paste it somewhere “temporary” that becomes permanent.

This is where Apify PostgreSQL leads automation pays off. Digital marketers feel it when campaign timelines get tight. Sales teams feel it when follow-ups slip. Agency operators see it when “just one more list” turns into a weekly fire drill.

This workflow turns Instagram prospecting into a repeatable system: generate smarter searches, scrape profiles, extract contact details with AI, and upsert clean lead records into PostgreSQL. You’ll see what it does, what you need, and how it behaves in the real world.

How This Automation Works

The full n8n workflow, from trigger to final output:

n8n Workflow Template: Apify to PostgreSQL, Instagram leads captured clean

The Problem: Instagram leads don’t stay clean

Manual Instagram prospecting usually starts with good intentions and ends with scattered data. You pull profiles from Google results, open each one, scan bios for contact details, then paste everything into a sheet that wasn’t designed to prevent duplicates. Later, someone tries to “clean it up” before outreach, which means re-checking profiles, fixing broken URLs, and guessing which entries are real. The worst part is the mental overhead. You stop trusting your own list, so you double-check everything, which steals time from actual outreach.

It adds up fast. Here’s where it breaks down in day-to-day work.

  • You end up with duplicates because two people scrape the same niche on different days.
  • Email addresses in bios are often formatted oddly, so manual copying misses them or grabs junk.
  • Google results include a lot of non-target pages, which means wasted clicks and bad leads.
  • Spreadsheets don’t enforce “one lead, one record,” so outreach gets inconsistent and hard to scale.

The Solution: Apify + AI scraping, stored safely in PostgreSQL

This n8n workflow automates the whole Instagram lead capture loop and stores the results where they stay reliable: your PostgreSQL database. You start by setting simple targeting inputs (platform, niche, country). An AI Agent then generates smart Google search queries for that niche, which helps you find profiles you wouldn’t think to search for manually. Apify scrapes Google results, the workflow expands and batches those URLs, then it cleans each profile link before scraping profile details with Apify’s Instagram Scraper. Finally, another AI step extracts contact details (especially emails) from bios, and n8n upserts the lead into PostgreSQL so existing records get updated instead of duplicated.

The workflow kicks off from a manual trigger while you test. After that, you can swap in a schedule to run daily. Data flows from “targeting inputs” to “query generation,” then through scraping and extraction, and lands in PostgreSQL as a consistent lead table you can actually build outreach on.

What You Get: Automation vs. Results

Example: What This Looks Like

Say you want 200 new Instagram prospects a week in “beauty & hair” in the USA. Manually, even a quick process takes about 2 minutes per profile to open, skim, copy, and paste, plus a little cleanup later, which is roughly 6 to 7 hours weekly. With this workflow, you spend about 10 minutes setting the targeting inputs, then let the scrape and extraction run in the background. You still review the database, but now it’s spot-checking, not rebuilding the list.

What You’ll Need

  • n8n instance (try n8n Cloud free)
  • Self-hosting option if you prefer (Hostinger works well)
  • Apify to run Google and Instagram scrapers
  • PostgreSQL to store and deduplicate lead records
  • OpenAI API key (get it from your OpenAI platform dashboard)

Skill level: Intermediate. You’ll connect accounts, paste API keys, and be comfortable checking a database table once.

Don’t want to set this up yourself? Talk to an automation expert (free 15-minute consultation).

How It Works

You set the targeting inputs. In the “Map Lead Inputs” step, you choose things like Instagram, a niche (for example beauty & hair), and a target country so the workflow doesn’t wander.

AI turns inputs into search queries. The AI Agent uses an OpenAI chat model to produce Google queries that are more specific than what most people type, which usually means cleaner search results.

Apify scrapes, then n8n batches the work. The workflow sends the query to Apify’s Google Search Scraper, expands the results into a list, then processes them in batches so you don’t overload external APIs.

Profiles get cleaned, scraped, and stored. Each URL is normalized, Apify’s Instagram Scraper pulls profile details, AI extracts contact info from the bio, and PostgreSQL upserts the record so you don’t get duplicates.

You can easily modify the niche, country, and quality filters to match your outreach goals. See the full implementation guide below for customization options.

Step-by-Step Implementation Guide

Step 1: Configure the Manual Trigger

Start the workflow with the manual trigger so you can test and iterate on lead capture inputs.

  1. Add or confirm the Manual Run Trigger node is present as the first node.
  2. Keep default settings (no parameters required) for Manual Run Trigger.
  3. Connect Manual Run Trigger to Map Lead Inputs.

Step 2: Connect Lead Input Mapping

Define the search inputs used to generate targeted Google queries for Instagram leads.

  1. Open Map Lead Inputs and add assignments for lead targeting fields.
  2. Set site to instagram.
  3. Set field_of_interest to beauty & hair.
  4. Set target_country to USA.
  5. Connect Map Lead Inputs to Query Builder Agent.

Step 3: Set Up the Query Builder AI

Generate a Google search query string tailored to your niche and country.

  1. Open Query Builder Agent and set Text to =field of interest: {{ $json.field_of_interest }} target country: {{ $json.target_country }}.
  2. Keep Prompt Type set to define.
  3. Ensure OpenAI Chat Assistant is connected as the language model for Query Builder Agent.
  4. Credential Required: Connect your openAiApi credentials in OpenAI Chat Assistant.
  5. Connect Query Builder Agent to Escape Query Script.

⚠️ Common Pitfall: The query generator uses your inputs exactly—if field_of_interest or target_country are empty in Map Lead Inputs, the query will be incomplete.

Step 4: Configure Scraping and URL Cleanup

Scrape Google results, expand the list, and normalize profile URLs for downstream scraping.

  1. In Escape Query Script, keep the provided JavaScript that escapes quotes in the query string.
  2. In Search Scraper Request, set URL to https://api.apify.com/v2/acts/apify~google-search-scraper/run-sync-get-dataset-items?token=[CONFIGURE_YOUR_TOKEN].
  3. Set JSON Body to ={ "focusOnPaidAds": false, "forceExactMatch": false, "includeIcons": false, "includeUnfilteredResults": false, "maxPagesPerQuery": 20, "mobileResults": false, "queries": "{{ $json.escapedQuery }}", "resultsPerPage": 100, "saveHtml": false, "saveHtmlToKeyValueStore": true }.
  4. Enable Send Body and set Specify Body to json.
  5. In Expand Results List, set Field to Split Out to organicResults.
  6. Connect Search Scraper RequestExpand Results ListIterate Result BatchesClean Profile URL.
  7. In Clean Profile URL, keep the JavaScript that removes a trailing slash from the url field.

⚠️ Common Pitfall: Replace [CONFIGURE_YOUR_TOKEN] in Search Scraper Request and Profile Scrape Request with your Apify token before running.

Step 5: Scrape Profiles and Extract Contact Details

Scrape Instagram profiles and extract email addresses from biographies using an AI extractor.

  1. In Profile Scrape Request, set URL to https://api.apify.com/v2/acts/apify~instagram-scraper/run-sync-get-dataset-items?token=[CONFIGURE_YOUR_TOKEN].
  2. Set JSON Body to ={ "addParentData": false, "directUrls": [ "{{ $json.cleanedUrl }}" ], "enhanceUserSearchWithFacebookPage": false, "isUserReelFeedURL": false, "isUserTaggedFeedURL": false, "resultsLimit": 2, "resultsType": "details", "searchLimit": 1, "searchType": "hashtag" }.
  3. Open Extract Contact Details and set Text to ={{ $json.biography }}.
  4. Keep Input Schema as provided to extract Email in JSON.
  5. Ensure OpenAI Extractor Model is connected as the language model for Extract Contact Details.
  6. Credential Required: Connect your openAiApi credentials in OpenAI Extractor Model.
  7. Connect Clean Profile URLProfile Scrape RequestExtract Contact Details.

Step 6: Configure Database Upsert

Save extracted leads into your PostgreSQL table with an upsert operation.

  1. Open Upsert Lead Records and set Operation to upsert.
  2. Set Schema to public and Table to Instagram_Leads_Beauty&Hair.
  3. Map columns.value.email to ={{ $('Filter').item.json.output[0].Email }}.
  4. Map columns.value.user_name to ={{ $('Profile Scrape Request').item.json.username }}.
  5. Map columns.value.account_link to ={{ $('Profile Scrape Request').item.json.inputUrl }}.
  6. Map columns.value.follower_count to ={{ $('Profile Scrape Request').item.json.followsCount }}.
  7. Map columns.value.target_country to ={{ $('Map Lead Inputs').item.json.target_country }} and columns.value.field_of_interest to ={{ $('Map Lead Inputs').item.json.field_of_interest }}.
  8. Credential Required: Connect your postgres credentials in Upsert Lead Records.
  9. Connect Extract Contact DetailsUpsert Lead RecordsIterate Result Batches.

⚠️ Common Pitfall: The mapping uses $('Filter') for email, but no node named Filter exists in the workflow. Update the email mapping to the correct source from Extract Contact Details before running.

Step 7: Test and Activate Your Workflow

Validate each step with manual execution, then activate for production.

  1. Click Execute Workflow on Manual Run Trigger to run the workflow end-to-end.
  2. Confirm Search Scraper Request returns results and Expand Results List outputs individual items.
  3. Verify Profile Scrape Request returns profile details and Extract Contact Details outputs an Email field (or N/A).
  4. Check that Upsert Lead Records inserts or updates records in Instagram_Leads_Beauty&Hair.
  5. When satisfied, toggle the workflow to Active for production use.
🔒

Unlock Full Step-by-Step Guide

Get the complete implementation guide + downloadable template

Common Gotchas

  • Apify credentials can expire or need specific permissions. If things break, check your Apify token status in the Apify Console first.
  • If you’re using Wait nodes or external rendering, processing times vary. Bump up the wait duration if downstream nodes fail on empty responses.
  • Default prompts in AI nodes are generic. Add your brand voice early or you’ll be editing outputs forever.

Frequently Asked Questions

How long does it take to set up this Apify PostgreSQL leads automation?

About 30–60 minutes if your Apify, OpenAI, and Postgres accounts are ready.

Do I need coding skills to automate Apify PostgreSQL leads?

No. You will mainly connect accounts and paste API keys. The included code steps are already built; you’re not expected to write new code.

Is n8n free to use for this Apify PostgreSQL leads workflow?

Yes. n8n has a free self-hosted option and a free trial on n8n Cloud. Cloud plans start at $20/month for higher volume. You’ll also need to factor in OpenAI API costs (often a few cents per run, depending on volume) and Apify usage.

Where can I host n8n to run this automation?

Two options: n8n Cloud (managed, easiest setup) or self-hosting on a VPS. For self-hosting, Hostinger VPS is affordable and handles n8n well. Self-hosting gives you unlimited executions but requires basic server management.

Can I customize this Apify PostgreSQL leads workflow for a different niche and country?

Yes, and it’s straightforward. Update the targeting values in the “Map Lead Inputs” (Edit Fields/Set) step, then the AI Agent will generate new search queries automatically. You can also add a follower minimum by inserting an If/Switch filter after the profile scrape. Common tweaks include collecting extra fields (like category keywords), limiting results per run, and writing leads into a CRM after the Postgres upsert.

Why is my Apify connection failing in this workflow?

Usually it’s an invalid or expired Apify token, so regenerate it and update the credential in n8n. It can also be a wrong actor/task configuration for the scraper you’re calling, or Apify rate limits when you push too many requests quickly. If failures happen mid-run, reduce batch size in the loop and try again. Also check that your Apify plan still has usage available, because “no remaining credits” looks like a random error when you’re in a hurry.

How many leads can this Apify PostgreSQL leads automation handle?

A lot. On n8n Cloud, the practical limit is your monthly executions and how you batch results; on self-hosted n8n there’s no fixed execution cap, it mostly depends on your server and your Apify/OpenAI usage. In real usage, teams often run a few hundred profiles per niche per day, then scale up once they’re happy with lead quality. If you want higher volume, smaller batches and more frequent runs tend to be more stable than one giant scrape.

Is this Apify PostgreSQL leads automation better than using Zapier or Make?

Often, yes, because this workflow leans on batching, conditional logic, and multi-step processing that gets awkward (and expensive) in many Zapier-style setups. n8n is also easier to self-host, which matters when you’re doing lead gen at scale. That said, if you only want a tiny two-step flow like “new row in a sheet → send an email,” Zapier or Make can feel faster to set up. The real decider is volume and complexity. Talk to an automation expert if you want a quick recommendation based on your outreach target.

Once this is in place, your Instagram lead list stops being a fragile spreadsheet and starts acting like an actual database. Honestly, that alone makes outreach feel calmer.

Need Help Setting This Up?

Our automation experts can build and customize this workflow for your specific needs. Free 15-minute consultation—no commitment required.

Lisa Granqvist

Workflow Automation Expert

Expert in workflow automation and no-code tools.

×

Use template

Get instant access to this n8n workflow Json file

💬
Get a free quote today!
Get a free quote today!

Tell us what you need and we'll get back to you within one working day.

Get a free quote today!
Get a free quote today!

Tell us what you need and we'll get back to you within one working day.

Launch login modal Launch register modal