# Conversing with OpenAI and Postgres: n8n workflow template

> Started by someone opening a chat with it, and works across Postgres. It puts OpenAI behind a retrieval step, so answers are drawn from documents you supply...

Source: https://millionrounds.com/workflow/conversing-with-openai-and-postgres (Millionrounds; every figure on the page links to its filing or article)

[Automation library](https://millionrounds.com/workflows) · [Data collection](https://millionrounds.com/workflows?category=Data+collection#library)

# Conversing with OpenAI and Postgres

Started by someone opening a chat with it, and works across Postgres. It puts OpenAI behind a retrieval step, so answers are drawn from documents you supply rather than from the model alone. 25 steps, 1 branch: large, and worth reading before you trust it with anything live.

Large Calls a model: OpenAI Retrieval

[Download free](https://millionrounds.com/login?next=%2Fapp%2Fworkflow%2Fconversing-with-openai-and-postgres)

**25 steps** Large: read it before trusting it with anything live

**1** Branch: places the flow splits

**0** Loops or waits

**1** Service it connects to

## The flow

Starts it 2 Model 6 Logic 10 Service 7

The steps in the order they run, from what starts it.

- 1 **Window Buffer Memory** Memory Buffer Window *Model*
- 2 **Chat Trigger** Chat trigger *Starts it*
- 3 **When clicking "Test workflow"** Manual trigger *Starts it*
- 4 **Structured Output Parser** Output Parser Structured *Model*
- 5 **deepseek-chat** Lm Chat Open Ai *Model*
- 6 **Deepseek-chat** Lm Chat Open Ai *Model*
- 7 **AI Agent** Agent *Model*
- 8 **Load the schema from the local file** Read Write File *Service*
- 9 **List all tables in a database** Postgres *Service*
- 10 **plot agent** Agent *Model*
- 11 **Extract SQL query** Set *Logic*
- 12 **Extract data from file** Extract From File *Service*
- 13 **Schema Extractor** Postgres *Service*
- 14 **Edit Fields1** Set *Logic*
- 15 **Check if query exists** If *Logic*
- 16 **Combine schema data and chat input** Set *Logic*
- 17 **Add table name to output** Set *Logic*
- 18 **Combine query result and chat answer** Merge *Logic*
- 19 **Final SQL result** Postgres *Service*
- 20 **No Operation, do nothing** No Op *Logic*
- 21 **Convert data to Json** Convert To File *Service*
- 22 **Prepare final output** Set *Logic*
- 23 **Format query results** Set *Logic*
- 24 **Save file locally** Read Write File *Service*
- 25 **Edit Fields** Set *Logic*

## How it's built

- **Starts on:** Someone opening a chat with it
- **Steps:** 25, with 1 branch
- **Size:** Large: read it before trusting it with anything live
- **Connects to:** Postgres
- **Model:** OpenAI
- **Retrieval:** Answers are drawn from documents you supply, not from the model alone

## Questions

What does it need to run?

An n8n account, plus your own Postgres account, and a key for OpenAI. You connect your own accounts after importing it; none are included.

What starts it?

It starts on someone opening a chat with it.

Does it use AI?

Yes. It calls OpenAI. Its answers draw on documents you give it, not on the model alone. Model calls are billed by the provider, not by n8n.

How long does it take to set up?

Take your time. At 25 steps it is large: read it before trusting it with anything live.

How do I import it?

Copy the workflow, open a new workflow in n8n and paste. The steps appear on the canvas, ready for your own accounts.

**Before you run it**

Nothing here has been tested by us. It is somebody's working automation, read and described from its own steps. Treat the connections it wants as a list of accounts it will touch, and read the 25 steps before you point it at anything that matters.

## Run it in n8n

- **Create your n8n account** Free to start. n8n is where the workflow runs. [Create an n8n account ↗](https://millionrounds.com/n8n)
- **Download the workflow** Free with a Millionrounds account. [Download free](https://millionrounds.com/login?next=%2Fapp%2Fworkflow%2Fconversing-with-openai-and-postgres)
- **Paste it into n8n** Open a new workflow in n8n and paste. Then connect your own accounts.

Stored with the author's traces removed: canvas notes, instance identifiers, saved credential names and any live document they had left behind.

## Connects to

Each opens the other workflows that use it.

[Postgres](https://millionrounds.com/workflows?service=Postgres#library)

## Workflows like it

Also data collection, with the same services where possible, closest in size first.

- [**The title is very good, clearly conveying the template's purpose** 25 steps · calls a model · Gmail, Postgres](https://millionrounds.com/workflow/the-title-is-very-good-clearly-conveying-the-template-s-purp)
- [**Taxi service provider with Grok and Postgres** 24 steps · calls a model · Postgres, Redis](https://millionrounds.com/workflow/taxi-service-provider-with-grok-and-postgres)
- [**Automated email responses with OpenAI and Supabase** 27 steps · calls a model · Outlook, Postgres](https://millionrounds.com/workflow/automated-email-responses-with-openai-and-supabase)
- [**Serve a RAG chat backend for Next.js with OpenAI and Redis** 23 steps · calls a model · Pinecone, Postgres](https://millionrounds.com/workflow/serve-a-rag-chat-backend-for-next-js-with-openai-and-redis)
- [**Automate customer retention with Stripe** 22 steps · calls a model · Gmail, Google Sheets](https://millionrounds.com/workflow/automate-customer-retention-with-stripe)
- [**Automatic service with Gemini and Postgres** 22 steps · calls a model · Postgres, Redis](https://millionrounds.com/workflow/automatic-service-with-gemini-and-postgres)
