Skip to main content

Email assistant: convert natural language to SQL queries with Phi4-mini and PostgreSQL

Workflow preview

Workflow preview
100%
Email assistant: convert natural language to SQL queries with Phi4-mini and 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

Who is this for? 🏻 πŸ»β€ 🏻 Humans and Robots alike. This workflow can be used as a Chat Trigger , as well as a Workflow Trigger . It will take a natural language request, and then generate a ...

Best for

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

Tools used

n8n-nodes-base.set, n8n-nodes-base.converttofile, n8n-nodes-base.readwritefile, n8n-nodes-base.extractfromfile, @n8n/n8n-nodes-langchain.chattrigger, n8n-nodes-base.stickynote, n8n-nodes-base.manualtrigger, n8n-nodes-base.if

Source and attribution

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

Original n8n.io source

1.1 Workflow description

Title
Email assistant: convert natural language to SQL queries with Phi4-mini and PostgreSQL
Workflow name
Email assistant: convert natural language to SQL queries with Phi4-mini and PostgreSQL

Who is this for?

πŸ§‘πŸ»πŸ«±πŸ»β€πŸ«²πŸ»πŸ€– Humans and Robots alike.

This workflow can be used as a Chat Trigger, as well as a Workflow Trigger.

It will take a natural language request, and then generate a SQL query. The resulting query parameter will contain the query, and a sqloutput parameter will contain the results of executing such query.

What's the use case?

This template is most useful paired with other workflows that extract e-mail information and store it in a structured Postgres table, and use LLMs to understand inquiries about information contained in an e-mail inbox and formulate questions that needs answering.

Plus, the prompt can be easily adapted to formulate SQL queries over any kind of structured database.

Privacy and Economics

As LLM provider I'm using Ollama locally, as I consider my e-mail extremely sensitive information. As model, phi4-mini does an excellent job balancing quality and efficiency.

Setup

Upon running for the first time, this workflow will automatically trigger a sub-section to read all tables and extract their schema into a local file.

Then, either by chatting with the workflow in n8n's interface or by using it as a sub-workflow, you will get a query and a sqloutput response.

Customizations

If you want to work with just one particular table yet keep edits at bay, append a condition to the List all tables in a database step, like so:

WHERE table_schema='public' AND table_name='my_emails_table_name'

To repurpose this workflow to work with any other data corpus in a structured database, inspect the AI Agent user and system prompts and edit them accordingly.

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 - Add table name to output

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

Block 2 - Convert data to binary

Type / Role
n8n-nodes-base.convertToFile - convertToFile
Config choices
Version 1.1

Block 3 - Save file locally

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

Block 4 - Extract data from file

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

Block 5 - Chat Trigger

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

Block 6 - Sticky Note

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

Block 7 - When clicking "Test workflow"

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

Block 8 - Combine schema data and chat input

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

Block 9 - Load the schema from the local file

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

Block 10 - Extract SQL query

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

Block 11 - Check if query exists

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

Block 12 - Format query results

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

Block 13 - Combine query result and chat answer

Type / Role
n8n-nodes-base.merge - merge
Config choices
Version 3

Block 14 - List all columns in a table

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

Block 15 - List all tables in a database

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

Block 16 - Ollama Chat Model

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

Block 17 - Postgres

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

Block 18 - Add trailing semicolon

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

Block 19 - Check for trailing semicolon

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

Block 20 - WorkflowTrigger

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

Block 21 - If ran manually

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

Block 22 - If file exists or already retried generating it

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

Block 23 - Sticky Note1

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

Block 24 - AI Agent

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

Showing the first 24 of 26 workflow blocks. Download the JSON for the full node graph.

3. Summary Table

Workflow Email assistant: convert natural language to SQL queries with Phi4-mini and PostgreSQL
Complexity advanced
Nodes 26
Categories Engineering, AI RAG
Author Alfonso Corretti
Published 27 Apr 2025

4. Reproducing the Workflow from Scratch

  1. 1. Download the workflow JSON

    Use the JSON export at /data/workflows/3761/3761.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 Email assistant: convert natural language to SQL queries with Phi4-mini and PostgreSQL do?

Who is this for? 🏻 πŸ»β€ 🏻 Humans and Robots alike. This workflow can be used as a Chat Trigger , as well as a Workflow Trigger . It will take a natural language request, and then generate a ...

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 Engineering, AI RAG use case.