n8n

How to Automate PostgreSQL Chat Reporting?

Turn chat into a simple way for your team to ask your PostgreSQL database for answers. An AI agent writes SQL, runs it, and explains results in plain words. Great for operations and data teams that need quick numbers without waiting on analysts.

Here is how it works. A chat message starts the flow. The OpenAI chat model powers the AI agent. The agent uses three database tools: list all schemas and tables, get a table’s columns and keys, and run safe SQL. A short chat memory keeps the last few messages so follow up questions still make sense. The agent always includes schema names to avoid wrong joins or missing tables. You get fast responses that are accurate and tied to your live data.

Setup needs an OpenAI API key and PostgreSQL credentials. Use a read only database role for safety, and adjust the memory window if you expect long chats. Expect fewer ad hoc requests to your data team and faster daily checks. Use it for KPIs, inventory snapshots, or customer lookups through a simple chat link.

What are the key features?

  • Chat trigger starts the flow whenever a new message arrives.
  • OpenAI chat model generates SQL and explains results in simple words.
  • AI agent tool to list schemas and tables so the model knows what data exists.
  • AI agent tool to fetch table definitions, including columns and foreign keys.
  • AI agent tool to execute SQL queries directly on PostgreSQL.
  • Chat history node keeps the last five messages for context.
  • Model and memory window are configurable to fit your data size and tone.

What are the benefits?

  • Reduce ad hoc report requests from hours to minutes
  • Automate up to 70% of common data questions through chat
  • Improve query accuracy by enforcing schema qualified tables
  • Support multiple teams at once without extra analyst time
  • Unify live answers from your database and plain language summaries
  • Lower risk with a read only role and controlled query tools

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. Open the OpenAI Chat Model node. In the 'Credential to connect with' dropdown, click 'Create new credential' and enter your OpenAI API key from the OpenAI account API page. Save the credential.
  4. Double click each PostgreSQL tool node (Execute SQL Query, Get DB Schema and Tables List, Get Table Definition). In 'Credential to connect with', click 'Create new credential' and enter host, port, database, user, and password. Enable SSL if your server requires it. Save the credential.
  5. Use a read only database user for safety. In your PostgreSQL admin tool, grant SELECT on needed schemas and tables. Avoid write permissions.
  6. Open the Chat History node and set the context window length if you want more or fewer past messages kept. The default is five messages.
  7. Test the Get DB Schema and Tables List node by executing it once. You should see a list of schema and table names returned.
  8. Test the Get Table Definition node by passing a known table name and schema. Confirm columns, types, and keys are returned.
  9. Test the Execute SQL Query node with a simple SELECT on a small table to confirm the connection and permissions work.
  10. Open the AI Agent node and confirm the language model, memory, and all three Postgres tools are connected. Keep the system message as a database assistant and ensure it mentions schema qualified tables.
  11. Click Chat in the top bar and ask a question like: What were yesterday’s orders by status. Confirm the agent inspects schema, builds SQL, and returns a clear summary with a result table.
  12. When ready, activate the workflow. Share the public chat link with your team. If you see permission errors, check the database role and schema access. If the model times out, try a smaller query or add limits in the system message.

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.