Skip to main content

Query Google Sheets/CSV data through an AI agent using PostgreSQL

Workflow preview

Workflow preview
100%
Query Google Sheets/CSV data through an AI agent using PostgreSQL preview
Open on n8n.io

Important notice

This workflow is provided as-is. Please review and test before using in production.

1. Workflow Overview

Want to see it in action? Watch the full breakdown here: Video Link Template Description This n8n workflow empowers you to query structured financi...

Best for

  • Document Extraction automation workflows
  • AI RAG automation workflows
  • advanced n8n builders looking for reusable templates

Tools used

n8n-nodes-base.code, n8n-nodes-base.googledrivetrigger, @n8n/n8n-nodes-langchain.toolworkflow, @n8n/n8n-nodes-langchain.lmchatgooglegemini, n8n-nodes-base.set, n8n-nodes-base.if, n8n-nodes-base.postgres, n8n-nodes-base.googlesheets

Source and attribution

This workflow is cataloged by N8N Workflows and links back to its original n8n.io source page by Leonardo Grigorio.

Original n8n.io source

1.1 Workflow description

Title
Query Google Sheets/CSV data through an AI agent using PostgreSQL
Workflow name
Query Google Sheets/CSV data through an AI agent using PostgreSQL

Want to see it in action? Watch the full breakdown here: 📺 Video Link

Template Description

This n8n workflow empowers you to query structured financial data from Google Sheets or CSV files using AI-generated SQL. Unlike traditional vector database solutions that falter with numerical queries, this template leverages PostgreSQL for efficient data storage and an AI agent to dynamically create optimized SQL queries from natural language inputs.

What It Does
  • Retrieves data from Google Sheets or CSV files
  • Infers the data schema and builds a PostgreSQL table
  • Populates the table with your data
  • Uses an AI agent to translate natural language questions into SQL queries
  • Returns precise numerical results quickly and efficiently
Why Use This?
  • No SQL knowledge required—the AI generates queries for you
  • Bypasses the inefficiencies and costs of vector database approaches
  • Scales effortlessly without overwhelming the language model
  • Fully free and open-source

Setup Requirements

Pre-Conditions
  • PostgreSQL Database: A running PostgreSQL instance (no specific extensions required beyond standard installation).
  • Google Sheets Access: A publicly accessible or shared Google Sheet URL with structured data (e.g., financial records). Need a starting point? Use this Sample Google Sheet Template.
  • n8n Instance: A working n8n setup with access to the Google Drive and PostgreSQL nodes.
Step-by-Step Instructions
  1. Add Your Google Sheets URL

    • Open the "Google Drive Trigger" node.
    • Replace the placeholder URL with your Google Sheet’s link.
    • Verify the sheet name matches your data source.
  2. Configure PostgreSQL

    • Update the "PostgreSQL" nodes with your database credentials (host, database, user, password).
    • The workflow automatically creates and populates the table based on your data schema.
  3. Run the Workflow

    • Execute the workflow manually to set up the database.
    • Once initialized, use the AI agent by asking questions like:
      • "How much did I sell last week?"
      • "What were the total sales for Product X in February?"
  4. (Optional) Automate Updates

    • Add a "Schedule Trigger" node to sync your Google Sheets data with PostgreSQL on a regular basis.

How It Works

  • Schema Detection: The workflow analyzes your Google Sheets or CSV data to infer its structure and create an appropriate PostgreSQL table.
  • AI-Powered Queries: An optimized AI agent converts your natural language questions into precise SQL queries, ensuring accurate results.
  • Efficient Retrieval: By using PostgreSQL instead of vector-based methods, this template avoids common pitfalls like slow performance or inaccurate numerical outputs.

Tips for Success

  • Ensure your Google Sheet or CSV has consistent column headers for smooth schema detection.
  • Test with simple questions first to verify the AI agent’s query generation.
  • Check out the n8n Template Submission Guidelines for more best practices.

1.2 Logical Blocks

This catalog entry is organized from the workflow JSON. The node-level section below shows the executable blocks available for review before importing the template.

2. Block-by-Block Analysis

Block 1 - create table query

Type / Role
n8n-nodes-base.code - code
Config choices
Version 2

Block 2 - create insertion query

Type / Role
n8n-nodes-base.code - code
Config choices
Version 2

Block 3 - Google Drive Trigger

Type / Role
n8n-nodes-base.googleDriveTrigger - googleDriveTrigger
Config choices
Version 1

Block 4 - execute_query_tool

Type / Role
@n8n/n8n-nodes-langchain.toolWorkflow - toolWorkflow
Config choices
Version 1.2

Block 5 - Google Gemini Chat Model

Type / Role
@n8n/n8n-nodes-langchain.lmChatGoogleGemini - lmChatGoogleGemini
Config choices
Version 1

Block 6 - get_postgres_schema

Type / Role
@n8n/n8n-nodes-langchain.toolWorkflow - toolWorkflow
Config choices
Version 2

Block 7 - change_this

Type / Role
n8n-nodes-base.set - set
Config choices
Version 3.4

Block 8 - is not in database

Type / Role
n8n-nodes-base.if - if
Config choices
Version 2.2

Block 9 - table exists?

Type / Role
n8n-nodes-base.postgres - postgres
Config choices
Version 2.5

Block 10 - fetch sheet data

Type / Role
n8n-nodes-base.googleSheets - googleSheets
Config choices
Version 4.5

Block 11 - remove table

Type / Role
n8n-nodes-base.postgres - postgres
Config choices
Version 2.5

Block 12 - create table

Type / Role
n8n-nodes-base.postgres - postgres
Config choices
Version 2.5

Block 13 - perform insertion

Type / Role
n8n-nodes-base.postgres - postgres
Config choices
Version 2.5

Block 14 - Execute Workflow Trigger

Type / Role
n8n-nodes-base.executeWorkflowTrigger - executeWorkflowTrigger
Config choices
Version 1

Block 15 - Sticky Note

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 16 - Sticky Note1

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 17 - When chat message received

Type / Role
@n8n/n8n-nodes-langchain.manualChatTrigger - manualChatTrigger
Config choices
Version 1.1

Block 18 - response output

Type / Role
n8n-nodes-base.set - set
Config choices
Version 3.4

Block 19 - sql query executor

Type / Role
n8n-nodes-base.postgres - postgres
Config choices
Version 2.5

Block 20 - schema finder

Type / Role
n8n-nodes-base.postgres - postgres
Config choices
Version 2.5

Block 21 - schema to string

Type / Role
n8n-nodes-base.code - code
Config choices
Version 2

Block 22 - AI Agent With SQL Query Prompt

Type / Role
@n8n/n8n-nodes-langchain.agent - agent
Config choices
Version 1.7

Block 23 - Sticky Note2

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

3. Summary Table

Workflow Query Google Sheets/CSV data through an AI agent using PostgreSQL
Complexity advanced
Nodes 23
Categories Document Extraction, AI RAG
Author Leonardo Grigorio
Published 04 Mar 2025

4. Reproducing the Workflow from Scratch

  1. 1. Download the workflow JSON

    Use the JSON export at /data/workflows/3079/3079.json as the source template for this automation.

  2. 2. Import the template into n8n

    Open n8n, import the downloaded JSON, and review each node before activating the workflow.

  3. 3. Configure credentials and variables

    Replace placeholder credentials, API keys, webhook URLs, account IDs, and environment-specific values with your own settings.

  4. 4. Test with sample data

    Run the workflow manually or in a staging workspace, inspect node output, and confirm downstream systems receive the expected data.

  5. 5. Activate and monitor

    Enable the workflow only after testing, then monitor executions, errors, and rate limits during the first production runs.

5. General Notes & Resources

Review imported nodes carefully before activation. This catalog entry is intended to help you inspect the workflow structure, understand required services, and find related templates faster.

Node names, credentials, schedules, webhook paths, and external service limits may need adjustment for your workspace.

Frequently asked questions

What does Query Google Sheets/CSV data through an AI agent using PostgreSQL do?

Want to see it in action? Watch the full breakdown here: Video Link Template Description This n8n workflow empowers you to query structured financi...

What do I need before importing this workflow?

Review the workflow JSON, configure any required credentials in n8n, and test the automation in a safe workspace before using it in production.

Can I customize this workflow?

Yes. Use the block-by-block analysis and the downloadable JSON to inspect each node, then adjust credentials, prompts, schedules, filters, or destinations for your Document Extraction, AI RAG use case.