# Build an OpenAI RAG system with Postgres: n8n workflow template

> Started by another system calling 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/build-an-openai-rag-system-with-postgres (Millionrounds; every figure on the page links to its filing or article)

[Automation library](https://millionrounds.com/workflows) · [Documents and files](https://millionrounds.com/workflows?category=Documents+and+files#library)

# Build an OpenAI RAG system with Postgres

Started by another system calling 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. 20 steps, 2 branches: a working afternoon to understand properly.

Medium Calls a model: OpenAI Retrieval

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

**20 steps** Medium: a working afternoon

**2** Branches: places the flow splits

**0** Loops or waits

**1** Service it connects to

## The flow

Starts it 4 Model 8 Logic 4 Service 4

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

- 1 **Webhook Trigger** Webhook *Starts it*
- 2 **Text Splitter** Text Splitter Recursive Character Text Splitter *Model*
- 3 **OpenAI Embeddings** Embeddings Open Ai *Model*
- 4 **OpenAI Chat Model** Lm Chat Open Ai *Model*
- 5 **Workflow Configuration** Set *Logic*
- 6 **Document Loader** Document Default Data Loader *Model*
- 7 **Store Embeddings in PGVector** Vector Store PGVector *Model*
- 8 **Retrieve Relevant Chunks** Vector Store PGVector *Model*
- 9 **Answer Query with Context** Agent *Model*
- 10 **Route by Action** Switch *Logic*
- 11 **Log Upload to Cache** Postgres *Service*
- 12 **Answer questions with a vector store** Tool Vector Store *Model*
- 13 **Save to Query Cache** Postgres *Service*
- 14 **Extract Text from Document** Extract From File *Service*
- 15 **Check Query Cache** Postgres *Service*
- 16 **Respond Upload Success** Respond To Webhook *Starts it*
- 17 **Respond with Answer** Respond To Webhook *Starts it*
- 18 **Cache Hit or Miss** Switch *Logic*
- 19 **Format Cached Response** Set *Logic*
- 20 **Respond with Cached Answer** Respond To Webhook *Starts it*

## How it's built

- **Starts on:** Another system calling it
- **Steps:** 20, with 2 branches
- **Size:** Medium: a working afternoon
- **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 another system calling 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?

Plan an afternoon. 20 steps is enough that you should read each one before running it.

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 20 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%2Fbuild-an-openai-rag-system-with-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 documents and files, with the same services where possible, closest in size first.

- [**Intelligent legal document review and compliance automation** 17 steps · calls a model · Postgres](https://millionrounds.com/workflow/intelligent-legal-document-review-and-compliance-automation)
- [**Telegram AI chatbot with OpenAI and PGVector RAG** 24 steps · calls a model · Google Drive, Postgres](https://millionrounds.com/workflow/telegram-ai-chatbot-with-openai-and-pgvector-rag)
- [**Automated document sync between SharePoint and Google Drive** 15 steps · Google Drive, Postgres](https://millionrounds.com/workflow/automated-document-sync-between-sharepoint-and-google-drive)
- [**Index Google Drive files into a Supabase vector store** 26 steps · calls a model · Google Drive, Postgres](https://millionrounds.com/workflow/index-google-drive-files-into-a-supabase-vector-store)
- [**Generate PDF documents from HTML with PDF Generator API** 10 steps · Gmail, Postgres](https://millionrounds.com/workflow/generate-pdf-documents-from-html-with-pdf-generator-api)
- [**Create RAG vector database from Google Drive documents** 9 steps · calls a model · Google Drive, Postgres](https://millionrounds.com/workflow/create-rag-vector-database-from-google-drive-documents)
