n8n

How to Connect OpenAI PostgreSQL Self Service Queries?

Give your team a simple chat box to ask questions about your PostgreSQL data. They type plain English and get answers fast, without writing SQL. Great for operations, support, and revenue teams that need quick numbers during the day.

When a message arrives, a chat event passes it to an AI Agent powered by the OpenAI Chat Model. A short memory window keeps recent context so follow up questions work. The agent turns each request into SQL and sends it to the Postgres tool to run using the sql_statement field, so there are no hardcoded queries. Results come back to the same chat, so people can keep digging. A starter question like Which tables are available helps the agent explore the schema, then you can ask for counts, trends, and filters.

Setup needs an OpenAI API key and a read only PostgreSQL user with network access. Expect faster reporting and fewer handoffs, often cutting ad hoc requests from 20 minutes to under 2 minutes. Use read only rights to avoid accidental updates and limit the schema the agent can query. Adjust the memory size to balance context and precision. Teams can use it for daily KPIs, quick customer lookups, inventory checks, or health checks on orders and tickets. This pattern can be adapted to other SQL engines with minimal changes, but here it is tuned for Postgres for simple, safe self service.

What are the key features?

  • Chat event trigger starts a conversation when a message is received.
  • OpenAI Chat Model interprets plain English questions.
  • AI Agent converts questions into SQL and decides when to run the database tool.
  • Simple Memory keeps recent messages so follow up questions make sense.
  • Postgres node executes the SQL from the agent using the sql_statement field with executeQuery.
  • Results return to the same chat thread for quick, ongoing exploration.

What are the benefits?

  • Reduce manual data requests from 20 minutes to under 2 minutes
  • Streamline ad hoc reporting by about 70 percent
  • Improve accuracy by removing copy paste errors by up to 90 percent
  • Connect OpenAI and PostgreSQL in one chat driven flow
  • Support many team members at once through the chat panel

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 PostgreSQL and OpenAI. See the Tools Required section above for links to create accounts with these services.
  3. Create a read only PostgreSQL user and confirm the database is reachable from n8n. Note the host, port, database name, username, password, and SSL setting.
  4. In the n8n canvas, double click the OpenAI Chat Model node. In the Credential to connect with dropdown, click Create new credential. Paste your OpenAI API key from the OpenAI account API page and save.
  5. Double click the Postgres node. In the Credential to connect with dropdown, click Create new credential. Enter your PostgreSQL host, port, database, user, password, and SSL preference, then save.
  6. If your data uses a non public schema, add the schema to the user search path or grant usage and select permissions to the read only user.
  7. Click the Chat button at the bottom of the canvas. Send Which tables are available to confirm the agent can inspect your schema.
  8. If you see an error, check network access, verify the read only user can list tables, and confirm the database credentials are correct.
  9. Open the Simple Memory node and adjust the window size to control how many recent messages the agent remembers.
  10. Keep the database user read only to prevent updates and limit access to the schemas you want the agent to query.
  11. Activate the workflow. Send a few sample questions like daily order count or tickets by status to validate results.
  12. Review executions in n8n to monitor responses and spot any SQL errors. Refine permissions or question phrasing as needed.

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.