ARTIFACT // N8N-SAFE-TEXT-TO-SQL.JSON (JSON)
n8n + Local LLM Safe Text-to-SQL Workflow (AST Guard + Read-Only Role + Row Cap) (n8n-safe-text-to-sql.json)
Converts natural language questions into PostgreSQL/SQLite queries using local Qwen2.5-Coder, blocks DDL/DML mutations, and enforces LIMIT 100.
[KEY VERIFIED TAKEAWAY / TL;DR]This reproducible configuration artifact "n8n + Local LLM Safe Text-to-SQL Workflow (AST Guard + Read-Only Role + Row Cap)" (n8n-safe-text-to-sql.json, JSON) is paired with Tinker AI Lab's hardware-verified guide "n8n-sql-readonly-database-analyst". It exposes 3 customizable browser variables (OLLAMA_URL=http://host.docker.internal:11434, SQL_MODEL=qwen2.5-coder:14b, MAX_SQL_ROWS=100) with loopback-first security defaults.
02 // WORKFLOWNo-Code AI Workflows· Verified: 2026-10-26
Browse All 42 Artifacts →n8n-safe-text-to-sql.json
View Paired Guide →n8n + Local LLM Safe Text-to-SQL Workflow (AST Guard + Read-Only Role + Row Cap)
Converts natural language questions into PostgreSQL/SQLite queries using local Qwen2.5-Coder, blocks DDL/DML mutations, and enforces LIMIT 100.
Customizable Variables (Saved in Browser)
UTF-8 Encoded · Live Client-Side Compilation
{
"name": "Tinker AI - Safe Local Text-to-SQL Analyst",
"nodes": [
{
"parameters": {
"httpMethod": "POST",
"path": "safe-sql-query",
"responseMode": "lastNode"
},
"name": "Question Webhook",
"type": "n8n-nodes-base.webhook",
"typeVersion": 2,
"position": [200, 300]
},
{
"parameters": {
"method": "POST",
"url": "http://host.docker.internal:11434/api/generate",
"sendBody": true,
"specifyBody": "json",
"jsonBody": "={{ JSON.stringify({ model: 'qwen2.5-coder:14b', format: 'json', stream: false, options: { temperature: 0, num_ctx: 8192 }, prompt: 'You are a PostgreSQL read-only analyst. Given schema: orders(id, customer_id, status, amount_cents, created_at). Return JSON {"sql": "SELECT ..."} for question: ' + String($json.body?.question || '').slice(0, 500) }) }}",
"options": { "timeout": 45000 }
},
"name": "Generate SQL via Local Coder",
"type": "n8n-nodes-base.httpRequest",
"typeVersion": 4.2,
"retryOnFail": true,
"maxTries": 2,
"continueOnFail": true,
"position": [440, 300]
},
{
"parameters": {
"jsCode": "const raw = JSON.parse($input.first().json.response || '{}');\nlet sql = String(raw.sql || '').trim().replace(/;+$/, '');\nconst forbidden = /\\b(INSERT|UPDATE|DELETE|DROP|ALTER|TRUNCATE|GRANT|REVOKE|COPY|CREATE|EXEC|INTO|pg_sleep)\\b/i;\nif (!/^SELECT\\b/i.test(sql) || forbidden.test(sql) || sql.includes(';')) {\n throw new Error('SECURITY_BLOCK: Only single-statement read-only SELECT queries are permitted.');\n}\nif (!/\\bLIMIT\\s+\\d+/i.test(sql)) {\n sql += ' LIMIT 100';\n}\nreturn [{ json: { verifiedSql: sql } }];"
},
"name": "Deterministic SQL AST & Limit Guard",
"type": "n8n-nodes-base.code",
"typeVersion": 2,
"position": [680, 300]
}
],
"connections": {
"Question Webhook": {
"main": [[{ "node": "Generate SQL via Local Coder", "type": "main", "index": 0 }]]
},
"Generate SQL via Local Coder": {
"main": [[{ "node": "Deterministic SQL AST & Limit Guard", "type": "main", "index": 0 }]]
}
}
}
PARAMETER SPECIFICATION // 参数化变量规范说明表
Customizable Parameter Reference for n8n-safe-text-to-sql.json
This JSON artifact exposes 3 template variables compiled client-side in your browser.
| Template Token | Parameter Description | Verified Default | Example Format |
|---|---|---|---|
{{OLLAMA_URL}} | Ollama Base URL | http://host.docker.internal:11434 | http://host.docker.internal:11434 |
{{SQL_MODEL}} | SQL Generation Model | qwen2.5-coder:14b | qwen2.5-coder:14b |
{{MAX_SQL_ROWS}} | Hard Row Limit Cap | 100 | 100 |
PAIRED HARDWARE MANIFEST // 配套实测环境与边界记录
Verified Environment for "n8n + Local LLM Read-Only Text-to-SQL Analyst: AST Guardrails, Row Limits & Zero-Write Security"
Tested Hardware & OS
Docker n8n 1.76+ · PostgreSQL 16 / MySQL 8 · Local Ollama (`qwen2.5-coder:14b`)
Pinned Stack & Model Versions
n8n 1.76.xPostgreSQL 16Ollama 0.5.7qwen2.5-coder:14b
Offline Boundary (100% Intranet Database Schema & Query Privacy)
Neither your DDL table schemas nor query result rows ever leave your local network.
Known Operational Limits & Failure Modes
- Never connect an LLM Text-to-SQL workflow using your application’s read-write `postgres` superuser! A prompt injection could attempt `DELETE` or `pg_read_file()`.
- Do not dump 150 tables of DDL into the prompt; pass only the 5–8 relevant reporting tables/views (with column comments and 2 sample enum values) so accuracy stays above 94%.
DEPLOYMENT CHECKLIST // 配置落地核验步骤
3-Step Verification Checklist After Deploying n8n-safe-text-to-sql.json
- Connected n8n using a dedicated database role with `default_transaction_read_only = on` and `statement_timeout = 5000ms`.
- Verified the n8n Code guard node blocks semicolons (`;`) and DDL/DML verbs before reaching the Postgres node.
- Enforced an automatic `LIMIT 100` cap and annotated enum column values in the schema prompt.