# Natural language database queries with PostgreSQL: n8n workflow

> Started by an incoming Telegram message, and works across Postgres and Telegram. It puts OpenAI and OpenRouter behind a retrieval step, so answers are drawn...

Source: https://millionrounds.com/workflow/natural-language-database-queries-with-postgresql (Millionrounds; every figure on the page links to its filing or article)

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

# Natural language database queries with PostgreSQL

Started by an incoming Telegram message, and works across Postgres and Telegram. It puts OpenAI and OpenRouter behind a retrieval step, so answers are drawn from documents you supply rather than from the model alone. 17 steps, 1 branch: a working afternoon to understand properly.

Medium Calls a model: OpenAI, OpenRouter Retrieval

[Download free](https://millionrounds.com/login?next=%2Fapp%2Fworkflow%2Fnatural-language-database-queries-with-postgresql)

**17 steps** Medium: a working afternoon

**1** Branch: places the flow splits

**0** Loops or waits

**2** Services it connects to

## The flow

Starts it 2 Model 9 Logic 3 Service 3

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

- 1 **When Executed by Another Workflow** Execute Workflow trigger *Starts it*
- 2 **Think** Tool Think *Model*
- 3 **OpenRouter Chat Model** Lm Chat Open Router *Model*
- 4 **Postgres Chat Memory** Memory Postgres Chat *Model*
- 5 **OpenRouter Chat Model1** Lm Chat Open Router *Model*
- 6 **Think1** Tool Think *Model*
- 7 **Telegram Trigger1** Telegram trigger *Starts it*
- 8 **CALL QUERY AGENT** Tool Workflow *Model*
- 9 **Query agent** Agent *Model*
- 10 **Main agent** Agent *Model*
- 11 **Voice or Text** Switch *Logic*
- 12 **ACCES DATABASE WITH DYNAMIC QUERYS** Postgres *Service*
- 13 **SEND MESSAGE** Telegram *Service*
- 14 **Download File1** Telegram *Service*
- 15 **Text** Set *Logic*
- 16 **Transcribe1** Open Ai *Model*
- 17 **Merge** Merge *Logic*

## How it's built

- **Starts on:** An incoming Telegram message
- **Steps:** 17, with 1 branch
- **Size:** Medium: a working afternoon
- **Connects to:** Postgres, Telegram
- **Model:** OpenAI, OpenRouter
- **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 and Telegram accounts, and a key for OpenAI and OpenRouter. You connect your own accounts after importing it; none are included.

What starts it?

It starts on an incoming Telegram message.

Does it use AI?

Yes. It calls OpenAI and OpenRouter. 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. 17 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 17 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%2Fnatural-language-database-queries-with-postgresql)
- **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)[Telegram](https://millionrounds.com/workflows?service=Telegram#library)

## Workflows like it

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

- [**AI-newsletter-agent: automate newsletter generation for Mautic** 17 steps · calls a model · Google Sheets, Telegram](https://millionrounds.com/workflow/ai-newsletter-agent-automate-newsletter-generation-for-mauti)
- [**Create AI dream videos with Veo3 and Telegram** 17 steps · calls a model · Google Sheets, Telegram](https://millionrounds.com/workflow/create-ai-dream-videos-with-veo3-and-telegram)
- [**Create AI social posts from Telegram links with OpenAI** 17 steps · calls a model · Telegram](https://millionrounds.com/workflow/create-ai-social-posts-from-telegram-links-with-openai)
- [**Create Instagram carousels and TikTok slideshows with OpenAI** 17 steps · calls a model · Telegram](https://millionrounds.com/workflow/create-instagram-carousels-and-tiktok-slideshows-with-openai)
- [**Create LinkedIn content from workflows with Gemini** 17 steps · calls a model · Google Sheets, LinkedIn](https://millionrounds.com/workflow/create-linkedin-content-from-workflows-with-gemini)
- [**Create and publish AI social posts from Telegram with OpenAI** 17 steps · calls a model · Telegram](https://millionrounds.com/workflow/create-and-publish-ai-social-posts-from-telegram-with-openai)
