n8n

How to Generate OpenAI PostgreSQL Chart Insights?

Chat with your database and get clear answers fast. The flow turns natural questions into SQL results, explains the answer in plain text, and adds a helpful chart when a visual makes the point. Great for teams that need quick data checks without opening a BI tool.

A chat message starts the process. An information extractor pulls the user question. A smart SQL agent runs the query on PostgreSQL and writes an easy summary. A text classifier then decides if a chart would help. If a chart is needed, a subflow asks OpenAI to produce a Chart JS style definition, builds a Quickchart link, and returns the final image with the text. Session memory keeps the conversation context, and a low temperature model keeps answers stable and consistent.

You will need a PostgreSQL database and an OpenAI API key. Connect the database credentials and set the chat endpoint live. Expect faster answers to daily metrics, ad hoc sales and operations checks, and team questions that are hard to explain with text alone. Most teams save time and reduce back and forth by letting users ask in plain English and see the right chart on demand.

What are the key features?

  • Chat message trigger collects user questions in real time
  • Information extractor pulls the exact question from the message
  • SQL agent connects to PostgreSQL and runs the right query
  • Window memory keeps recent chat history for better context
  • Text classifier decides when a chart is helpful
  • Subworkflow generates a Chart JS style definition using OpenAI
  • HTTP request calls OpenAI for structured JSON output
  • Response node builds a Quickchart link for instant images
  • Output formatter returns either text only or text plus chart
  • Low temperature model setting keeps replies consistent

What are the benefits?

  • Reduce manual report work from 30 minutes to 2 minutes
  • Automate up to 80 percent of ad hoc data requests
  • Improve answer clarity by 50 percent with auto charts
  • Handle more team questions without extra analyst time
  • Connect OpenAI and PostgreSQL in one simple chat flow

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. In the n8n credentials manager, create an OpenAI credential. In your OpenAI account, create an API key. Paste the key into the OpenAI credential in n8n and save.
  4. In the n8n credentials manager, create a PostgreSQL credential. Enter host, port, database, user, and password. Enable SSL if your host requires it. Test the connection and save.
  5. Open the Chat trigger node and confirm it is public. Copy the chat URL or open the n8n Chat view to send test messages.
  6. Open the AI Agent node. Choose the SQL agent type, select your PostgreSQL credential, and keep the default prompt or edit it to match your schema style.
  7. Open the Window Memory node and make sure the session key uses the chat trigger session id so each user keeps context across messages.
  8. Open the OpenAI language model nodes and select a capable model. Keep temperature low for consistent results.
  9. Open the HTTP Request node inside the chart subflow and select the OpenAI credential. Confirm the JSON body requests a Chart JS style definition and the response mode is JSON.
  10. Open the Set response node and confirm the Quickchart link builder is correct. Adjust width if your images look cramped.
  11. Send a plain question that does not need a chart, like What was total sales last week. Confirm you receive a text answer.
  12. Send a question that needs a visual, like Show sales by product for the last 6 months. Confirm a chart image link appears with the text.
  13. If charts do not render, shorten labels or reduce dataset size. If the database does not connect, check firewall allowlist and SSL settings. If chart choices seem off, adjust the classifier prompt to be stricter.
  14. Activate the workflow when tests pass and share the chat link with your team.

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)

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.