n8n

How to Connect OpenAI and PostgreSQL for Data Insights?

Turn plain English questions into live database answers. Teams can ask for stats, trends, and lists without writing SQL. It fits internal reporting, ad hoc checks, and quick data lookups.

A chat message starts the flow. An AI agent uses an OpenAI chat model and three Postgres tools. It can list schemas and tables, get a table’s columns and keys, and run the final SQL. A short chat history keeps the last few messages so the agent remembers context. Results return to the same chat, so users see both the query and the answer.

Setup needs an OpenAI API key and a PostgreSQL connection. Use a read only database user to keep data safe. Expect faster reporting and fewer requests to analysts. Good for operations teams, sales leaders, and support managers who need quick numbers. Follow the steps to set credentials, pick the model, set the context window, test sample questions, and then make the chat public for your team.

What are the key features?

  • Chat trigger listens for each user message and starts the run.
  • AI agent plans steps and calls tools to inspect the database and run SQL.
  • OpenAI chat model processes natural language and crafts safe queries.
  • Postgres tool lists schemas and tables to guide correct table names.
  • Postgres tool returns table definitions with columns and keys.
  • Postgres tool executes the final SQL and returns results to chat.
  • Chat history keeps the last 5 messages so answers stay on topic.
  • Model node can be swapped for a different chat model if needed.

What are the benefits?

  • Reduce ad hoc report time from hours to minutes
  • Automate up to 70% of routine data questions
  • Improve query accuracy by enforcing schema qualified tables
  • Support many users at once through a shared chat
  • Cut analyst back and forth by giving self serve 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. In the n8n credentials manager, create OpenAI credentials: double click the OpenAI Chat Model node, open the 'Credential to connect with' dropdown, click 'Create new credential', then paste your OpenAI API key from your OpenAI account API page.
  4. In the n8n credentials manager, create PostgreSQL credentials: double click each Postgres Tool node, choose 'Create new credential', then enter host, port, database, user, and password. Use a read only user for safety.
  5. Open the AI Agent node and confirm the connected language model is set to your OpenAI credential. Keep the system message as the DB assistant instructions.
  6. Open the Chat History node and set Context Window Length to the number of past messages you want to keep. Five is the default.
  7. Test schema discovery: run the 'Get DB Schema and Tables List' node manually to confirm it returns table_schema and table_name values.
  8. Test table info: run the 'Get Table Definition' node with a known schema and table to verify columns and keys are returned.
  9. Open the Execute SQL Query node and confirm it uses the AI output for the query field. Ensure queries reference schema qualified tables.
  10. Start a chat from the When chat message received trigger. Ask a safe question like list all schemas. Check that the agent calls the correct tools and returns an answer.
  11. If you see permission errors, switch to a user with SELECT rights on the needed schemas. If table not found, add the correct schema name to the query.
  12. When satisfied, activate the workflow. Optionally make the chat public in n8n so teammates can ask questions without logging into the database.

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.