# Generate & test SQL code with OpenRouter AI and PostgreSQL: n8n workflow

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

Source: https://millionrounds.com/workflow/generate-and-test-sql-code-with-openrouter-ai-and-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)

# Generate & test SQL code with OpenRouter AI and PostgreSQL

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

Large Calls a model: OpenAI, OpenRouter Retrieval

[Download free](https://millionrounds.com/login?next=%2Fapp%2Fworkflow%2Fgenerate-and-test-sql-code-with-openrouter-ai-and-postgresql)

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

**9** Branches: places the flow splits

**0** Loops or waits

**2** Services it connects to

## The flow

Starts it 1 Model 6 Logic 24 Service 2

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

- 1 **When chat message received** Chat trigger *Starts it*
- 2 **Simple Memory** Memory Buffer Window *Model*
- 3 **OpenRouter Chat Model** Lm Chat Open Router *Model*
- 4 **localVariables** Set *Logic*
- 5 **OpenRouterAgent** Agent *Model*
- 6 **IfOpenAI** If *Logic*
- 7 **isExecutable** If *Logic*
- 8 **AgentName** Set *Logic*
- 9 **setOutputByProvider** Set *Logic*
- 10 **wordsForUser1** Set *Logic*
- 11 **getAssistantsList** Open Ai *Model*
- 12 **IfError** If *Logic*
- 13 **If** If *Logic*
- 14 **AutoErrorFixing** If *Logic*
- 15 **Switch** Switch *Logic*
- 16 **isAssistantExistsCode** Code *Logic*
- 17 **issueOnOpenAiSide** Set *Logic*
- 18 **IsMaxAutoErrorReached** If *Logic*
- 19 **askUserHowToHandleError** Set *Logic*
- 20 **Execute_AI_result** Postgres *Service*
- 21 **Execute a SQL query** My Sql *Service*
- 22 **isAssistantExists** If *Logic*
- 23 **collectErrorLoopDetails1** Code *Logic*
- 24 **mergeExecutionsResult** Code *Logic*
- 25 **GenerateErrorPrompt** Code *Logic*
- 26 **assistant** Set *Logic*
- 27 **createOpenAiAssistant** Open Ai *Model*
- 28 **maxAutoErrorLimitReached** Set *Logic*
- 29 **collectErrorLoopDetails** Code *Logic*
- 30 **isOpenAI** If *Logic*
- 31 **OpenAIMainBrain** Open Ai *Model*
- 32 **executedSQLQuery** Set *Logic*
- 33 **setThreadId** Set *Logic*

## How it's built

- **Starts on:** Someone opening a chat with it
- **Steps:** 33, with 9 branches
- **Size:** Large: read it before trusting it with anything live
- **Connects to:** MySQL, Postgres
- **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 MySQL and Postgres 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 someone opening a chat with it.

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?

Take your time. At 33 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 33 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%2Fgenerate-and-test-sql-code-with-openrouter-ai-and-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.

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

## Workflows like it

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

- [**Automate testing and collect responses with Telegram** 32 steps · Postgres, Telegram](https://millionrounds.com/workflow/automate-testing-and-collect-responses-with-telegram)
- [**Govern AI agent responses with OpenAI and Postgres** 32 steps · Gmail, Postgres](https://millionrounds.com/workflow/govern-ai-agent-responses-with-openai-and-postgres)
- [**Detect and email top job matches with RemoteOK and Arbeitnow** 31 steps · calls a model · Gmail, Postgres](https://millionrounds.com/workflow/detect-and-email-top-job-matches-with-remoteok-and-arbeitnow)
- [**Automate WhatsApp tests and rank results with Postgres** 30 steps · Postgres, WhatsApp](https://millionrounds.com/workflow/automate-whatsapp-tests-and-rank-results-with-postgres)
- [**Create a RAG Telegram bot with Google Drive and PostgreSQL** 29 steps · Google Drive, Postgres](https://millionrounds.com/workflow/create-a-rag-telegram-bot-with-google-drive-and-postgresql)
- [**Detect semantic duplicate website pages with Google Drive** 29 steps · Google Drive, Postgres](https://millionrounds.com/workflow/detect-semantic-duplicate-website-pages-with-google-drive)
