Company RAG and data queries

Local Ollama natural language to SQL workflow for PostgreSQL

Short answer

This n8n workflow lets managers and analysts query a PostgreSQL database in plain language without writing SQL. It caches the database schema locally, uses a self-hosted Ollama model to generate SQL and runs only valid queries with built-in error flags. Sensitive tables and query outputs never leave your own infrastructure.

Catalogue ID
#1245
Functional nodes
23
Connections
25
Trigger
chatTrigger, manualTrigger, executeWorkflowTrigger
AI layer
AI-assisted
Business problem solved

Business problem solved

Operations and finance managers wait days on data engineers for routine revenue or stock queries. Sending database schemas and production rows to third-party cloud models is unacceptable under strict security and GDPR rules.

Trigger

A chat trigger accepts questions, a manual trigger refreshes the schema cache and a sub-workflow trigger isolates query execution.

Connected systems

Connected systems

  • PostgreSQL
  • Local Ollama model
  • Local schema cache file
  • Chat trigger
  • Self-hosted n8n

Every workflow is deployed on your own server with documentation, a run log and an error queue.

Workflow steps

Workflow steps

  1. 01

    Local schema cache load

    The workflow reads table and column definitions from a local cache file, falling back to PostgreSQL when a refresh is needed.

  2. 02

    On-server SQL generation via Ollama

    The plain-language question and schema context pass to your local Ollama model to produce a targeted SQL query.

  3. 03

    Protected PostgreSQL execution

    The query runs on PostgreSQL with built-in node error flags catching syntax or permission faults safely.

  4. 04

    Plain-language result summary

    Returned rows are formatted into a clear summary table so non-technical staff can act immediately.

Error queue and human approval

Error queue and human approval

Built-in state in the catalogue workflow

The verified catalogue workflow contains two built-in node error flags on the schema-loading and PostgreSQL execution nodes.

Production layer added by ALTAI

During live deployment, ALTAI binds the connection to a strict read-only (SELECT) PostgreSQL role, sets row and statement timeouts and writes every query to an audit log.

Frequently asked

Natural language to SQL for PostgreSQL workflow Frequently asked questions

No. Live deployments connect exclusively through a read-only PostgreSQL role, so write, update and drop commands are blocked at database level.

No. Both n8n and the Ollama language model run inside your own infrastructure with zero external model calls.

We enforce query statement timeouts, maximum row limits and dedicated reporting views during set-up.

The schema-refresh branch runs on a schedule or manual trigger so the local schema cache always reflects your latest tables.

Mert Can Gundogdu
The founder takes the call, not a sales team
Next step

We write the proposal after we have found the problem together

In a thirty-minute call we listen to how you work and talk through where time and customers are slipping away. If you do not need a digital fix, we will tell you that too.

30 minutes · no preparation needed

The call and the week of discovery are both free.Read the terms

Chat on WhatsApp