n8n

How to Generate Postgres Chat Data Answers?

Ask your data a question and get answers from your database in plain English. The system turns simple chat messages into safe SQL for PostgreSQL, runs the query, and returns both the results and a clear explanation. It suits teams that need fast checks without writing SQL.

There are two parts. A manual path scans your database, lists all tables and columns, then saves a compact schema file for later use. The chat path loads that schema, combines it with the user message and session id, and sends it to a local language model through an AI Agent. A parser pulls the SELECT query from the model output, adds a missing semicolon if needed, and only runs the query when it passes the check. The system executes the SQL on PostgreSQL, formats a readable table, and merges it with the AI explanation. It accepts messages from a chat webhook, a manual test, or as a sub workflow input. If no valid query is found, it returns a friendly empty result instead of failing.

Set up a PostgreSQL connection and a running Ollama server with a small model. Use a read only database user to keep data safe. Expect a big time drop for common questions, like moving from a 30 minute request to a 2 minute chat. Good for quick KPI checks, ad hoc counts, operations dashboards, and record lookups across teams.

What are the key features?

  • Chat webhook trigger to receive natural language questions
  • Database scan that lists tables and columns using the information schema
  • Schema cache saved as a JSON file to speed up each request
  • AI Agent with an Ollama chat model that proposes SQL and a plain English answer
  • Query extraction that keeps only the SELECT part and adds a missing semicolon
  • Safety check that runs the database step only when a query exists
  • PostgreSQL query execution and clean text table formatting
  • Works as a sub workflow so other automations can reuse it

What are the benefits?

  • Reduce manual work from 30 minutes to 2 minutes for common data questions
  • Automate about 80 percent of simple SELECT queries with chat
  • Improve data accuracy by cutting copy paste mistakes and missing semicolons
  • Connect chat input to PostgreSQL through one secure endpoint
  • Support many chat sessions at once using a session id

How do you set it up?

  1. Import the template into n8n: Create a new workflow in n8n > Click the three dots menu > Select 'Import from File' > Choose the downloaded JSON file.
  2. You'll need accounts with PostgreSQL and Ollama. See the Tools Required section above for links to create accounts with these services.
  3. In the n8n Cloud credentials manager, create a new PostgreSQL credential. Enter host, port, database, user, and password. Use a read only user for safety.
  4. Open each PostgreSQL node and select the new credential in the Credential to connect with field. Save the node.
  5. Prepare Ollama on your server or laptop and make sure it is reachable from n8n. Ensure a small chat model is available.
  6. In the AI Agent or language model node, choose the Ollama Chat Model and set the base URL for your Ollama server. Pick the model you installed.
  7. Build the schema cache. Click the manual trigger at the top and let the scan finish. It will query tables and columns and store a schema file.
  8. Confirm the cache. Run the file load node and check that schema items appear in the output.
  9. Open the Chat Trigger node, copy the test URL, and send a message such as Show the top 10 customers by revenue. Use the n8n interface to send a test message if needed.
  10. Check the result. You should see the SQL, a formatted table, and a short explanation. If the result is empty, the system will show a friendly message.
  11. If the database step fails, verify network access and that the user can run SELECT on the needed tables. Also confirm that the model can produce a valid SELECT line.
  12. For safe use in production, keep the database user read only, since the extractor only allows SELECT. Optionally call this as a sub workflow using the natural language query input.

Tools Required

$24 / mo or $20 / mo billed annually to use n8n in the cloud. However, the local or self-hosted n8n Community Edition is free.

Ollama

Sign up

Free tier: $0 (self-hosted local API)

PostgreSQL

Sign up

Free: $0 (open-source PostgreSQL License; self-hosted)

Similar Templates

Join Futurise to access 1,200+ automation templates

Get instant access to ready-made automation workflows for n8n, Make.com, AI agents, and more. Download, customise, and deploy in minutes.