n8n

How to Generate OpenAI Postgres Data Insights?

Turn chat questions into clear answers from your database. The flow can also add a chart when it helps you understand the result. It suits teams that need quick data checks without waiting for a full report.

A public chat trigger starts the process when a message arrives. An information extractor pulls the user question. A SQL agent then queries PostgreSQL and returns a simple summary. A classifier checks if a visual would add value. If a chart is useful, a subworkflow calls OpenAI to create a valid Chart.js definition, then builds a QuickChart image link and returns the answer with the chart. If a chart is not needed, it sends text only. A memory buffer keeps the session context so follow up questions stay on topic. The language model runs at a low temperature for stable output.

Connect your PostgreSQL database and add your OpenAI API key. Expect faster KPI reviews, trend checks, and ad hoc analysis with less back and forth. Teams in operations and analytics can use it for daily metrics, month to date trends, and product breakdowns in minutes. The chart builder uses a compact width so images render well in chat. The subworkflow waits for the chart tool to finish, so users always get a complete response in one go.

What are the key features?

  • Chat webhook trigger captures user messages and starts the flow
  • Information extractor isolates the true user question from the message
  • SQL agent runs queries on PostgreSQL and returns a clear summary
  • Window buffer memory keeps conversation context by session ID
  • Text classifier decides if a chart will improve the answer
  • HTTP Request to OpenAI creates a Chart.js JSON with structured output
  • Response builder turns the chart JSON into a QuickChart image link
  • Two output paths return text only or text plus chart image
  • Execute subworkflow waits until the chart is ready to send a complete reply

What are the benefits?

  • Reduce manual chart creation from 60 minutes to 2 minutes
  • Automate up to 80 percent of ad hoc SQL questions from chat
  • Improve accuracy by querying the live database and cutting copy paste errors by 60 percent
  • Support multiple concurrent chat sessions with session memory
  • Connect OpenAI and PostgreSQL to turn text questions into data answers

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 OpenAI and PostgreSQL. See the Tools Required section above for links to create accounts with these services.
  3. Create the OpenAI credential: In the n8n Credentials menu, click New > Search for OpenAI > OpenAI API. Create an API key in your OpenAI account, then paste it into the credential and save.
  4. Create the PostgreSQL credential: In the n8n Credentials menu, click New > PostgreSQL. Enter host, port, database, user, and password. If using n8n Cloud, allow the n8n IP in your database firewall and save.
  5. Open the AI Agent node and select your PostgreSQL credential. Set the SQL dialect to match your database and confirm the tables are accessible with the chosen user.
  6. Open the OpenAI - Generate Chart definition node and choose your OpenAI credential. Keep the Content Type header as application/json and verify the model is set to gpt-4o-2024-08-06 or your allowed model.
  7. Check the Window Buffer Memory node and confirm the session key uses the chat trigger session ID so follow up questions keep context.
  8. Publish the chat endpoint: Open the When chat message received node and confirm Public is enabled. Copy the webhook URL if you plan to call it from an app or use the built in chat UI to test.
  9. Run a test: Ask a question like Show monthly sales. If the classifier picks chart required, you should see a text answer with a QuickChart image. If not, you will get text only.
  10. Troubleshoot charts: If the image does not render, check the Set response node width parameter and confirm OpenAI returns valid Chart.js JSON. Try a simpler chart type like bar or line.
  11. Troubleshoot SQL: If queries fail, verify schema and table names, user permissions, and any custom prompt in the agent. Update the prefix prompt if your database needs special instructions.
  12. Tune behavior: Lower the model temperature for consistent outputs, or adjust the classifier prompt to control when charts are used.

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.

OpenAI

Sign up

Pay-as-you-go: GPT-5 at $1.25 per 1M input tokens and $10 per 1M output tokens

PostgreSQL

Sign up

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

Credits:
Baptiste

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.