This workflow corresponds to n8n.io template #5012 — we link there as the canonical source.
This workflow follows the Agent → Execute Workflow Trigger recipe pattern — see all workflows that pair these two integrations.
The workflow JSON
Copy or download the full n8n JSON below. Paste it into a new n8n workflow, add your credentials, activate. Full import guide →
{
"meta": {
"templateCredsSetupCompleted": true
},
"nodes": [
{
"id": "c4a7b5c1-f74e-4de0-9469-146a197c42f5",
"name": "When Executed by Another Workflow",
"type": "n8n-nodes-base.executeWorkflowTrigger",
"position": [
-1568,
368
],
"parameters": {
"inputSource": "passthrough"
},
"typeVersion": 1.1
},
{
"id": "1f37374e-3c5c-4dad-bfa0-5e948cfbd6e5",
"name": "Query agent",
"type": "@n8n/n8n-nodes-langchain.agent",
"position": [
-1328,
368
],
"parameters": {
"text": "={{ $json.query }}",
"options": {
"systemMessage": "# SQL Query Generator - Sub Agent Template\n\nYou are a PostgreSQL query generator. Convert user requests into SELECT queries for the database table.\n\n## CRITICAL: OUTPUT FORMAT\n- Output ONLY the SQL query\n- NO explanations, descriptions, or additional text\n- NO code blocks or formatting\n- Just the raw SQL query string\n\n## DATABASE SCHEMA:\n**[REPLACE WITH YOUR DATABASE SCHEMA]**\n```\n[TABLE_NAME] Table:\nid (bigint, Primary Key)\n[field_name] (data_type)\n[field_name] (data_type)\n...\n```\n\n## STRICT RULES:\n1. ONLY SELECT queries - NO INSERT/UPDATE/DELETE/DROP/ALTER\n2. Table name: \"[YOUR_TABLE_NAME]\" (use double quotes if needed)\n3. Column names with spaces: use double quotes (\"[column name]\")\n4. Boolean columns: use true/false (not 1/0)\n5. Text searches: use ILIKE for case-insensitive matching\n6. Always end query with semicolon\n\n## EXAMPLES:\n**[CUSTOMIZE THESE EXAMPLES FOR YOUR USE CASE]**\nUser: \"Show verified entries\" \nResponse: SELECT * FROM \"[YOUR_TABLE]\" WHERE [verified_field] = true;\n\nUser: \"Top 10 most popular\" \nResponse: SELECT * FROM \"[YOUR_TABLE]\" ORDER BY [popularity_field] DESC LIMIT 10;\n\nUser: \"Items containing [keyword]\"\nResponse: SELECT * FROM \"[YOUR_TABLE]\" WHERE [description_field] ILIKE '%[keyword]%';\n\nREMEMBER: Output ONLY the SQL query, nothing else."
},
"promptType": "define"
},
"typeVersion": 2
},
{
"id": "1019eeda-425f-4450-934e-0224c689a801",
"name": "Think",
"type": "@n8n/n8n-nodes-langchain.toolThink",
"position": [
-1136,
592
],
"parameters": {
"description": "Think if the ouputed query really matches the input message."
},
"typeVersion": 1
},
{
"id": "2219bc4a-cdf2-4297-9bd6-423d0f138319",
"name": "OpenRouter Chat Model",
"type": "@n8n/n8n-nodes-langchain.lmChatOpenRouter",
"position": [
-1344,
560
],
"parameters": {
"model": "anthropic/claude-3.5-sonnet",
"options": {}
},
"credentials": {
"openRouterApi": {
"name": "<your credential>"
}
},
"typeVersion": 1
},
{
"id": "21a65536-abfd-4d19-94c9-9aa8d839db16",
"name": "Postgres Chat Memory",
"type": "@n8n/n8n-nodes-langchain.memoryPostgresChat",
"position": [
-288,
80
],
"parameters": {
"sessionKey": "C08SU8T0FJ8",
"sessionIdType": "customKey"
},
"credentials": {
"postgres": {
"name": "<your credential>"
}
},
"typeVersion": 1.3
},
{
"id": "32cf9699-649c-4781-b27b-c68e7ff306a4",
"name": "OpenRouter Chat Model1",
"type": "@n8n/n8n-nodes-langchain.lmChatOpenRouter",
"position": [
-448,
80
],
"parameters": {
"model": "anthropic/claude-3.5-sonnet",
"options": {}
},
"credentials": {
"openRouterApi": {
"name": "<your credential>"
}
},
"typeVersion": 1
},
{
"id": "cacfb298-2ee6-4292-ade7-bb18436df301",
"name": "Think1",
"type": "@n8n/n8n-nodes-langchain.toolThink",
"position": [
-144,
80
],
"parameters": {},
"typeVersion": 1
},
{
"id": "6c979f9c-38df-4b8d-a7c2-be499bd37cd7",
"name": "Download File1",
"type": "n8n-nodes-base.telegram",
"position": [
-1040,
-288
],
"parameters": {
"fileId": "={{ $json.message.voice.file_id }}",
"resource": "file"
},
"credentials": {
"telegramApi": {
"name": "<your credential>"
}
},
"typeVersion": 1.2
},
{
"id": "578c6f2d-d738-49d3-95f4-6de6a48c5754",
"name": "Transcribe1",
"type": "@n8n/n8n-nodes-langchain.openAi",
"position": [
-816,
-288
],
"parameters": {
"options": {},
"resource": "audio",
"operation": "transcribe"
},
"credentials": {
"openAiApi": {
"name": "<your credential>"
}
},
"typeVersion": 1.6
},
{
"id": "68b40dcf-46a0-47e8-9f63-ce020f08c4c6",
"name": "Telegram Trigger1",
"type": "n8n-nodes-base.telegramTrigger",
"position": [
-1488,
-176
],
"parameters": {
"updates": [
"message"
],
"additionalFields": {}
},
"credentials": {
"telegramApi": {
"name": "<your credential>"
}
},
"typeVersion": 1.1
},
{
"id": "e28131b3-4c13-4c5a-b6d1-e9be94970c9f",
"name": "Voice or Text",
"type": "n8n-nodes-base.switch",
"position": [
-1264,
-176
],
"parameters": {
"rules": {
"values": [
{
"outputKey": "Voice",
"conditions": {
"options": {
"version": 2,
"leftValue": "",
"caseSensitive": true,
"typeValidation": "strict"
},
"combinator": "and",
"conditions": [
{
"id": "612fa3b1-0e08-47f3-9455-746568546ab6",
"operator": {
"type": "string",
"operation": "exists",
"singleValue": true
},
"leftValue": "={{ $json.message.voice.file_id }}",
"rightValue": ""
}
]
},
"renameOutput": true
},
{
"outputKey": "Text",
"conditions": {
"options": {
"version": 2,
"leftValue": "",
"caseSensitive": true,
"typeValidation": "strict"
},
"combinator": "and",
"conditions": [
{
"id": "8c844924-b2ed-48b0-935c-c66a8fd0c778",
"operator": {
"type": "string",
"operation": "exists",
"singleValue": true
},
"leftValue": "={{ $json.message.text }}",
"rightValue": ""
}
]
},
"renameOutput": true
}
]
},
"options": {}
},
"typeVersion": 3.2
},
{
"id": "b8d80841-beeb-43f9-a51b-3ea3683c4ba1",
"name": "Text",
"type": "n8n-nodes-base.set",
"position": [
-944,
-80
],
"parameters": {
"options": {},
"assignments": {
"assignments": [
{
"id": "fe7ecc99-e1e8-4a5e-bdd6-6fce9757b234",
"name": "text",
"type": "string",
"value": "={{ $json.message.text }}"
}
]
}
},
"typeVersion": 3.4
},
{
"id": "e5e1508a-ceb2-49fd-b3d2-ef87338a230b",
"name": "Merge",
"type": "n8n-nodes-base.merge",
"position": [
-608,
-176
],
"parameters": {},
"typeVersion": 3.1
},
{
"id": "be9cde2d-e131-406b-a061-49193941e159",
"name": "Main agent",
"type": "@n8n/n8n-nodes-langchain.agent",
"position": [
-320,
-176
],
"parameters": {
"text": "={{ $json.text }}",
"options": {
"systemMessage": "# Database Assistant - Main Agent\n\nYou are an intelligent database assistant that helps users find and explore information from a structured database. You work with a specialized SQL sub-agent to retrieve data efficiently.\n\n## DATABASE SCHEMA:\n**[REPLACE WITH YOUR DATABASE SCHEMA]**\n```\nExample format:\nid (bigint, Primary Key)\nname (text)\ndescription (text)\ncategory (text)\n...\n```\n\n## CRITICAL OPERATIONAL RULES:\n- **ALWAYS use LIMIT in queries** - Default: 10, Maximum: 50\n- **NEVER request unlimited data** - this breaks the system\n- Use ORDER BY with LIMIT for meaningful results\n- Apply specific filters and conditions to narrow results\n\n## COMMUNICATION WITH SQL SUB-AGENT:\nSend clear, natural language requests:\n- \"Show me the 10 most popular [YOUR_ITEMS] in [CATEGORY_FIELD]\"\n- \"Find 5 newest verified entries containing '[KEYWORD]'\"\n- \"Get top 15 [YOUR_ITEMS] by [ACTIVITY_METRIC]\"\n- \"Search for entries with [PRICE_FIELD] less than [AMOUNT], limit 20\"\n\n## RESPONSE GUIDELINES:\nPresent results in a user-friendly format:\n- Highlight key information ([NAME_FIELD], [DESCRIPTION_FIELD], metrics)\n- Include relevant metadata ([VERIFICATION_FIELD], [POPULARITY_FIELD], [CATEGORIES_FIELD])\n- Provide actionable insights\n- Suggest related searches or refinements\n\n## DATA INTERPRETATION:\n**[CUSTOMIZE THESE FIELD DESCRIPTIONS]**\n- **[POPULARITY_FIELD]**: [Description of what this metric means]\n- **[ACTIVITY_FIELD]**: [Description of recent activity indicator]\n- **[VERIFICATION_FIELD]**: [Description of quality/trust indicator]\n- **[CATEGORY_FIELD]**: [Description of classification system]\n- **[PRICE_FIELD]**: [Description of pricing (0 = free, etc.)]\n\nAlways respect system limits and focus on delivering relevant, targeted results."
},
"promptType": "define"
},
"typeVersion": 2
},
{
"id": "ec7a16f3-fd62-4952-a074-3c044de6e86f",
"name": "SEND MESSAGE",
"type": "n8n-nodes-base.telegram",
"position": [
128,
-176
],
"parameters": {
"text": "={{ $json.output }}",
"chatId": "(YOUR CHAT ID)",
"additionalFields": {}
},
"credentials": {
"telegramApi": {
"name": "<your credential>"
}
},
"typeVersion": 1.2
},
{
"id": "4dfb46ad-d110-40e6-9a87-3c247fc2d8d3",
"name": "CALL QUERY AGENT",
"type": "@n8n/n8n-nodes-langchain.toolWorkflow",
"position": [
-16,
80
],
"parameters": {
"workflowId": {
"__rl": true,
"mode": "list",
"value": "Iw7dOCXYbpfRJrlW",
"cachedResultName": "query agent"
},
"description": "Call this tool for sql querys in natural language",
"workflowInputs": {
"value": {},
"schema": [],
"mappingMode": "defineBelow",
"matchingColumns": [],
"attemptToConvertTypes": false,
"convertFieldsToString": false
}
},
"typeVersion": 2.2
},
{
"id": "dd79a9fb-cf29-4266-a51d-114713e030c0",
"name": "ACCES DATABASE WITH DYNAMIC QUERYS",
"type": "n8n-nodes-base.postgres",
"position": [
-976,
368
],
"parameters": {
"query": "{{ $json.output }}",
"options": {
"largeNumbersOutput": "numbers",
"replaceEmptyStrings": true
},
"operation": "executeQuery"
},
"credentials": {
"postgres": {
"name": "<your credential>"
}
},
"typeVersion": 2.6
},
{
"id": "a302abcd-e6a0-4150-bcd7-84c549c1dc94",
"name": "Sticky Note",
"type": "n8n-nodes-base.stickyNote",
"position": [
-1600,
-544
],
"parameters": {
"color": 6,
"width": 1960,
"height": 800,
"content": "# MAIN AGENT"
},
"typeVersion": 1
},
{
"id": "cfcf980f-e037-4c6a-9dd3-7ba4359f6b44",
"name": "Sticky Note1",
"type": "n8n-nodes-base.stickyNote",
"position": [
-1600,
272
],
"parameters": {
"color": 4,
"width": 1220,
"height": 440,
"content": "# SUB AGENT"
},
"typeVersion": 1
},
{
"id": "4aaa94db-f919-44d4-9c0f-c703d3838e94",
"name": "Sticky Note2",
"type": "n8n-nodes-base.stickyNote",
"position": [
-2320,
-544
],
"parameters": {
"color": 2,
"width": 1780,
"height": 1760,
"content": "# Setup & Configuration Guide\n\n## \ud83d\udccb **Setup Checklist**\n```\n\u25a1 Install N8N workflow platform\n\u25a1 Set up PostgreSQL database\n\u25a1 Configure AI Agent nodes\n\u25a1 Test sub-workflow connection\n\u25a1 Validate query limits\n\u25a1 Deploy and monitor\n```\n\n---\n\n## \ud83d\udd27 **Database Configuration**\n```\n\ud83c\udfaf CRITICAL SETUP STEPS:\n1. Create your PostgreSQL table\n2. Replace [YOUR_TABLE_NAME] in templates\n3. Update database schema in both agents\n4. Set connection credentials in N8N\n5. Test database connectivity\n```\n\n---\n\n## \ud83e\udd16 **Main Agent Setup**\n```\n\ud83d\udccd WHAT TO REPLACE:\n\u2022 [YOUR_DATABASE_SCHEMA] \u2192 Your actual table structure\n\u2022 [YOUR_ITEMS] \u2192 What you're querying (products, users, etc.)\n\u2022 [CATEGORY_FIELD] \u2192 Your category column name\n\u2022 [POPULARITY_FIELD] \u2192 Your popularity metric column\n\u2022 [PRICE_FIELD] \u2192 Your price column name\n```\n\n---\n\n## \u2699\ufe0f **Sub-Agent Configuration**\n```\n\ud83d\udd04 TEMPLATE REPLACEMENTS:\n\u2022 [YOUR_TABLE_NAME] \u2192 Actual table name\n\u2022 [field_name] \u2192 Your column names\n\u2022 [verified_field] \u2192 Your verification column\n\u2022 [popularity_field] \u2192 Your popularity column\n\u2022 [description_field] \u2192 Your description column\n```\n\n---\n\n## \ud83d\udea8 **Critical System Limits**\n```\n\u26a0\ufe0f NEVER FORGET:\n\u2022 Default LIMIT: 10 results\n\u2022 Maximum LIMIT: 50 results\n\u2022 NO unlimited queries allowed\n\u2022 Always use ORDER BY with LIMIT\n\u2022 System will break without limits!\n```\n\n---\n\n## \ud83d\udd17 **Workflow Architecture**\n```\n\u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\n\u2502 USER QUERY \u2502\n\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\n \u2502\n\u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u25bc\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\n\u2502 MAIN AGENT \u2502 \u2190 Interprets user needs\n\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\n \u2502\n\u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u25bc\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\n\u2502 SUB-AGENT \u2502 \u2190 Generates SQL query\n\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\n \u2502\n\u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u25bc\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\n\u2502 DATABASE \u2502 \u2190 Executes query\n\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\n \u2502\n\u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u25bc\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\n\u2502 FORMATTED \u2502 \u2190 Returns to user\n\u2502 RESPONSE \u2502\n\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\n```"
},
"typeVersion": 1
},
{
"id": "e223ef72-1654-4368-8832-963e485a600b",
"name": "Sticky Note3",
"type": "n8n-nodes-base.stickyNote",
"position": [
-304,
320
],
"parameters": {
"color": 3,
"width": 576,
"height": 560,
"content": "## Need a tailor-made workflow? Tell me about your business and get a free proposal:\n\n**[Start here \u2192 Custom Automation Form](https://taskmorphr.com/contact)**\n\n---\n## \ud83d\udcc8 Cost-Savings Snapshot \nCurious what automation could save you? \nRun the 60-second calculator:\n\n**[ROI / Cost Comparison](https://taskmorphr.com/cost-comparison)**\n\n---\n### \u2709\ufe0f Reach me directly \n`paul@taskmorphr.com`"
},
"typeVersion": 1
},
{
"id": "49e2aa95-5fa3-4576-bb06-c9b8b7bba111",
"name": "Sticky Note4",
"type": "n8n-nodes-base.stickyNote",
"position": [
16,
720
],
"parameters": {
"color": 3,
"width": 224,
"height": 128,
"content": "### \ud83d\udee0\ufe0f Build it yourself \nBrowse every ready-made workflow: \n[Full Template Pack \u2014 coming soon](https://n8n.io/creators/diagopl/)\n"
},
"typeVersion": 1
}
],
"connections": {
"Text": {
"main": [
[
{
"node": "Merge",
"type": "main",
"index": 1
}
]
]
},
"Merge": {
"main": [
[
{
"node": "Main agent",
"type": "main",
"index": 0
}
]
]
},
"Think": {
"ai_tool": [
[
{
"node": "Query agent",
"type": "ai_tool",
"index": 0
}
]
]
},
"Think1": {
"ai_tool": [
[
{
"node": "Main agent",
"type": "ai_tool",
"index": 0
}
]
]
},
"Main agent": {
"main": [
[
{
"node": "SEND MESSAGE",
"type": "main",
"index": 0
}
]
]
},
"Query agent": {
"main": [
[
{
"node": "ACCES DATABASE WITH DYNAMIC QUERYS",
"type": "main",
"index": 0
}
]
]
},
"Transcribe1": {
"main": [
[
{
"node": "Merge",
"type": "main",
"index": 0
}
]
]
},
"Voice or Text": {
"main": [
[
{
"node": "Download File1",
"type": "main",
"index": 0
}
],
[
{
"node": "Text",
"type": "main",
"index": 0
}
]
]
},
"Download File1": {
"main": [
[
{
"node": "Transcribe1",
"type": "main",
"index": 0
}
]
]
},
"CALL QUERY AGENT": {
"ai_tool": [
[
{
"node": "Main agent",
"type": "ai_tool",
"index": 0
}
]
]
},
"Telegram Trigger1": {
"main": [
[
{
"node": "Voice or Text",
"type": "main",
"index": 0
}
]
]
},
"Postgres Chat Memory": {
"ai_memory": [
[
{
"node": "Main agent",
"type": "ai_memory",
"index": 0
}
]
]
},
"OpenRouter Chat Model": {
"ai_languageModel": [
[
{
"node": "Query agent",
"type": "ai_languageModel",
"index": 0
}
]
]
},
"OpenRouter Chat Model1": {
"ai_languageModel": [
[
{
"node": "Main agent",
"type": "ai_languageModel",
"index": 0
}
]
]
},
"When Executed by Another Workflow": {
"main": [
[
{
"node": "Query agent",
"type": "main",
"index": 0
}
]
]
}
}
}
Credentials you'll need
Each integration node will prompt for credentials when you import. We strip credential IDs before publishing — you'll add your own.
openAiApiopenRouterApipostgrestelegramApi
For the full experience including quality scoring and batch install features for each workflow upgrade to Pro
About this workflow
AI Database Assistant with Smart Query's & PostgreSQL Integration
Source: https://n8n.io/workflows/5012/ — original creator credit. Request a take-down →
Related workflows
Workflows that share integrations, category, or trigger type with this one. All free to copy and import.
Turn your Telegram into a personal Bloomberg terminal. Ask any question about any stock — get institutional-grade analysis back in seconds. TwelveData Pro Analyst is a complete, ready-to-import n8n wo
This workflow contains community nodes that are only compatible with the self-hosted version of n8n.
Multi Agent System Benefits. Uses gmailTool, lmChatOpenAi, agent, googleCalendarTool. Event-driven trigger; 46 nodes.
AI marketing Team Agent. Uses toolWorkflow, telegram, memoryBufferWindow, openAi. Event-driven trigger; 46 nodes.
This workflow is for users who want to turn Telegram into a personal AI-powered assistant capable of handling everyday tasks through natural language. It's ideal for solo founders, operators, or profe