n8n

How to Connect OpenAI to PostgreSQL Self Serve Analytics?

Chat with your PostgreSQL data using an AI assistant. Team members ask plain questions and get summaries, counts, or records without writing SQL. Useful for operations, support, and sales teams that need quick answers from a Supabase or PostgreSQL database.

When a chat message arrives, the n8n chat trigger passes the text to an AI Agent powered by the OpenAI chat model. The agent uses three database tools. One lists all tables so the model knows what data exists. Another pulls the columns and types for a chosen table. The third runs SQL and returns rows. The agent builds queries from the schema, executes them, and replies with totals, lists, or short explanations.

Set your OpenAI and database credentials, then adjust the system prompt to match your rules and tone. Expect faster answers to common data questions and fewer analyst tickets. Typical uses include daily KPIs, quick record lookups, ad hoc checks on JSON or text fields, and simple trend reviews for non technical staff.

What are the key features?

  • Chat message trigger starts the flow when a user asks a question
  • OpenAI chat model interprets plain language and plans the query
  • AI Agent selects the right tool and coordinates steps automatically
  • Database schema tool lists all public tables for context
  • Table definition tool loads columns and types to guide safe SQL
  • SQL execution tool runs read queries and returns rows to the agent
  • Editable system prompt lets you set rules, tone, and limits
  • Works with Supabase or any PostgreSQL endpoint using secure credentials

What are the benefits?

  • Reduce manual SQL work from 60 minutes to 5 minutes per request
  • Streamline internal data requests by 70% with self serve chat
  • Improve response time to common questions by 80%
  • Unify OpenAI and PostgreSQL in one simple flow
  • Handle many chat requests at once without extra analyst time

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, then paste it into the OpenAI API field in n8n. Name it clearly, for example OpenAI Prod.
  4. In the n8n credentials manager, create a PostgreSQL credential. Enter host, port, database, user, and password. If you use Supabase, copy these from the Supabase project settings and enable SSL if required. Name it clearly, for example Supabase Prod.
  5. Open the OpenAI Chat Model node and choose your OpenAI credential from the Credential to connect with dropdown.
  6. Open the DB Schema, Get table definition, and Run SQL Query nodes. For each, select your PostgreSQL credential in the Credential to connect with dropdown.
  7. Review the AI Agent node. Edit the system message to set your guardrails, allowed schemas, and the preferred answer format. Keep instructions short and clear.
  8. Check the DB Schema query. It targets the public schema. If your tables live in a different schema, update the WHERE clause to match your schema name.
  9. Activate the workflow. Open the n8n chat interface and send a simple question such as How many orders were created last week. Confirm you receive a reply with counts or rows.
  10. Validate accuracy by running the same query in your SQL editor and comparing results. Adjust the system prompt if the agent needs stricter rules.
  11. Troubleshoot connection errors by verifying host, port, SSL, and that your database allows remote connections. For not found table errors, confirm the schema filter and table names.
  12. Once results look correct, save changes and keep the workflow active so your team can self serve data questions.

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:
Made by Mark Shcherbakov from community 5minAI. Setup video: YouTube video

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.