{
  "name": "Query Google Sheets/CSV data through an AI agent using PostgreSQL",
  "nodes": [
    {
      "id": "a073154f-53ad-45e2-9937-d0a4196c7838",
      "name": "create table query",
      "type": "n8n-nodes-base.code",
      "position": [
        1280,
        2360
      ]
    },
    {
      "id": "2beb72c4-dab4-4058-b587-545a8ce8b86d",
      "name": "create insertion query",
      "type": "n8n-nodes-base.code",
      "position": [
        1660,
        2360
      ]
    },
    {
      "id": "ba19c350-ffb7-4fe1-9568-2a619c914434",
      "name": "Google Drive Trigger",
      "type": "n8n-nodes-base.googleDriveTrigger",
      "position": [
        600,
        2060
      ]
    },
    {
      "id": "dd2108fe-0cfe-453c-ac03-c0c5b10397e6",
      "name": "execute_query_tool",
      "type": "@n8n/n8n-nodes-langchain.toolWorkflow",
      "position": [
        1340,
        1720
      ]
    },
    {
      "id": "f2c110db-1097-4b96-830d-f028e08b6713",
      "name": "Google Gemini Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatGoogleGemini",
      "position": [
        880,
        1680
      ]
    },
    {
      "id": "2460801c-5b64-41b3-93f7-4f2fbffabfd6",
      "name": "get_postgres_schema",
      "type": "@n8n/n8n-nodes-langchain.toolWorkflow",
      "position": [
        1160,
        1720
      ]
    },
    {
      "id": "4b43ff94-df0d-40f1-9f51-cf488e33ff68",
      "name": "change_this",
      "type": "n8n-nodes-base.set",
      "position": [
        800,
        2060
      ]
    },
    {
      "id": "a27a47ff-9328-4eef-99e8-280452cff189",
      "name": "is not in database",
      "type": "n8n-nodes-base.if",
      "position": [
        1380,
        2060
      ]
    },
    {
      "id": "8ad9bc36-08b1-408e-ba20-5618a801b4ed",
      "name": "table exists?",
      "type": "n8n-nodes-base.postgres",
      "position": [
        1000,
        2060
      ]
    },
    {
      "id": "f66b7ca7-ecb7-47fc-9214-2d2b37b0fbe4",
      "name": "fetch sheet data",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        1180,
        2060
      ]
    },
    {
      "id": "11ba5da0-e7c4-49ee-8d35-24c8d3b9fea9",
      "name": "remove table",
      "type": "n8n-nodes-base.postgres",
      "position": [
        980,
        2360
      ]
    },
    {
      "id": "3936ecb3-f084-4f86-bd5f-abab0957ebc0",
      "name": "create table",
      "type": "n8n-nodes-base.postgres",
      "position": [
        1460,
        2360
      ]
    },
    {
      "id": "8a3ea239-f3fa-4c72-af99-31f4bd992b58",
      "name": "perform insertion",
      "type": "n8n-nodes-base.postgres",
      "position": [
        1860,
        2360
      ]
    },
    {
      "id": "21239928-b573-4753-a7ca-5a9c3aa8aa3e",
      "name": "Execute Workflow Trigger",
      "type": "n8n-nodes-base.executeWorkflowTrigger",
      "position": [
        1720,
        1720
      ]
    },
    {
      "id": "c94256a9-e44e-4800-82f8-90f85ba90bde",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1920,
        1460
      ],
      "parameters": {
        "width": 500,
        "height": 260,
        "content": "Place this in a separate workflow named:\n### query_executer"
      }
    },
    {
      "id": "daec928e-58ee-43da-bd91-ba8bcd639a4a",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1920,
        1840
      ],
      "parameters": {
        "width": 500,
        "height": 280,
        "content": "place this in a separate workflow named: \n### get database schema"
      }
    },
    {
      "id": "8908e342-fcbe-4820-b623-cb95a55ea5db",
      "name": "When chat message received",
      "type": "@n8n/n8n-nodes-langchain.manualChatTrigger",
      "position": [
        640,
        1540
      ]
    },
    {
      "id": "d0ae90c2-169e-44d7-b3c2-4aff8e7d4be9",
      "name": "response output",
      "type": "n8n-nodes-base.set",
      "position": [
        2220,
        1540
      ]
    },
    {
      "id": "81c58d9b-ded4-4b74-8227-849e665cbdff",
      "name": "sql query executor",
      "type": "n8n-nodes-base.postgres",
      "position": [
        2000,
        1540
      ]
    },
    {
      "id": "377d1727-4577-41bb-8656-38273fc4412b",
      "name": "schema finder",
      "type": "n8n-nodes-base.postgres",
      "position": [
        2000,
        1920
      ]
    },
    {
      "id": "89d3c59c-2b67-454d-a8f3-e90e75a28a8c",
      "name": "schema to string",
      "type": "n8n-nodes-base.code",
      "position": [
        2220,
        1920
      ]
    },
    {
      "id": "42d1b316-60ca-49db-959b-581b162ca1f9",
      "name": "AI Agent With SQL Query Prompt",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        900,
        1540
      ]
    },
    {
      "id": "368d68d0-1fe0-4dbf-9b24-ac28fd6e74c3",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        560,
        1420
      ],
      "parameters": {
        "width": 960,
        "height": 460,
        "content": "## Use a powerful LLM to correctly build the SQL queries, which will be identified from the get schema tool and then executed by the execute query tool."
      }
    }
  ],
  "connections": {
    "change_this": {
      "main": [
        [
          {
            "node": "table exists?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "create table": {
      "main": [
        [
          {
            "node": "create insertion query",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "remove table": {
      "main": [
        [
          {
            "node": "create table query",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "schema finder": {
      "main": [
        [
          {
            "node": "schema to string",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "table exists?": {
      "main": [
        [
          {
            "node": "fetch sheet data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "fetch sheet data": {
      "main": [
        [
          {
            "node": "is not in database",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "create table query": {
      "main": [
        [
          {
            "node": "create table",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "execute_query_tool": {
      "ai_tool": [
        [
          {
            "node": "AI Agent With SQL Query Prompt",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "is not in database": {
      "main": [
        [
          {
            "node": "create table query",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "remove table",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "sql query executor": {
      "main": [
        [
          {
            "node": "response output",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "get_postgres_schema": {
      "ai_tool": [
        [
          {
            "node": "AI Agent With SQL Query Prompt",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "Google Drive Trigger": {
      "main": [
        [
          {
            "node": "change_this",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "create insertion query": {
      "main": [
        [
          {
            "node": "perform insertion",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Execute Workflow Trigger": {
      "main": [
        [
          {
            "node": "sql query executor",
            "type": "main",
            "index": 0
          },
          {
            "node": "schema finder",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Google Gemini Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "AI Agent With SQL Query Prompt",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "When chat message received": {
      "main": [
        [
          {
            "node": "AI Agent With SQL Query Prompt",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}