Skip to main content

Generate & test SQL code with GPT/OpenRouter AI and PostgreSQL sandbox

Workflow preview

Workflow preview
100%
Generate & test SQL code with GPT/OpenRouter AI and PostgreSQL sandbox preview
Open on n8n.io

Important notice

This workflow is provided as-is. Please review and test before using in production.

1. Workflow Overview

This is the core AI agent used for queryverify.com. Don't trust complex AI generated SQL queries without double checking them in a safe environment. That's where queryve...

Best for

  • Engineering automation workflows
  • AI Chatbot automation workflows
  • advanced n8n builders looking for reusable templates

Tools used

@n8n/n8n-nodes-langchain.chattrigger, n8n-nodes-base.code, @n8n/n8n-nodes-langchain.memorybufferwindow, n8n-nodes-base.if, n8n-nodes-base.postgres, n8n-nodes-base.set, n8n-nodes-base.stickynote, @n8n/n8n-nodes-langchain.lmchatopenrouter

Source and attribution

This workflow is cataloged by N8N Workflows and links back to its original n8n.io source page by Muhammadumar.

Original n8n.io source

1.1 Workflow description

Title
Generate & test SQL code with GPT/OpenRouter AI and PostgreSQL sandbox
Workflow name
Generate & test SQL code with GPT/OpenRouter AI and PostgreSQL sandbox

This is the core AI agent used for queryverify.com.

Don't trust complex AI-generated SQL queries without double-checking them in a safe environment. That's where queryverify comes in. It automatically creates a test environment with the necessary data, generates code for your task, runs it to double-check for correctness, and handles errors if necessary. If you enable auto-fixing, queryverify will detect and fix issues on its own. If not, it will ask for your permission before making changes during debugging. In the end, you get thoroughly verified code along with full details about the environment it ran in.

Setup

It is an embedded chat for the website, but you can pin input data and run it on your own n8n instance.

Input data


  1. sessionId: uuid_v4. Required to handle ongoing conversations and to create table names (used as a prefix).
  2. threadId: string | nullable. If aiProvider is openai, conversation history is managed on OpenAI’s side. This is not needed in the first request—it will start a new conversation. For ongoing conversations, you must provide this value. You can get it from the OpenAIMainBrain node output after the first run. If you want to start a new conversation, just leave it as null.
  3. apiKey: string. Your API key for the selected aiProvider.
  4. aiProvider: string. Currently supported values: openai, openrouter.
  5. model: string. The AI model key (e.g., gpt-4.1, o3-mini, or any supported model key from OpenRouter).
  6. autoErrorFixing: boolean. If true, it will automatically fix errors encountered when running code in the environment. If false, it will ask for your permission before attempting a fix.
  7. chatInput: string. The user's prompt or message.
  8. currentDbSchemaWithData: string. A JSON representation of the database schema with sample data. Used to inform the AI about the current database structure during an ongoing conversation. Please use the '[]' value in the first request. Example string for filled db structure : '{"users":[{"id":1,"name":"John Doe","email":"[email protected]"},{"id":2,"name":"Jane Smith","email":"[email protected]"}],"products":[{"product_id":101,"product_name":"Laptop","price":999.99}]}'

Make sure to fill in your credentials:

  1. Your OpenAI or OpenRouter API key
  2. Access to a local PostgreSQL database for test execution

You can view your generated tables using your preferred PostgreSQL GUI. We recommend DBeaver. Alternatively, you can activate the “Deactivated DB Visualization” nodes below. To use them, connect each to the most recent successful Set node and manually adjust the output. However, the easiest and most efficient method is to use a GUI.

Workflow Explanation

  1. We store all input values in the localVariables node. Please use this node to get the necessary data.
  2. OpenAI has a built-in assistant that manages chat history on their side. For OpenRouter, we handle chat history locally. That’s why we use separate nodes like ifOpenAi and isOpenAi. Note that if logic can also be used inside nodes.
  3. The AutoErrorFixing loop will run only a limited number of times, as defined by the isMaxAutoErrorReached node. This prevents infinite loops.
  4. The Execute_AI_result node connects to the PostgreSQL test database used to execute queries.

Guidance on customization

This setup is built for PostgreSQL, but it can be adapted to any programming language, and the logic can be extended to any programming framework.

To customize the logic for other programming languages:

  1. Change instruction parameter in localVariables node.
  2. Replace the Execute_AI_result PostgreSQL node with another executable node. For example, you can use the HTTP Request node.
  3. Update the GenerateErrorPrompt node's prompt parameter to generate code specific to your target language or framework.

Any workflows built on top of this must credit the original author and be released under an open-source license.

1.2 Logical Blocks

This catalog entry is organized from the workflow JSON. The node-level section below shows the executable blocks available for review before importing the template.

2. Block-by-Block Analysis

Block 1 - When chat message received

Type / Role
@n8n/n8n-nodes-langchain.chatTrigger - chatTrigger
Config choices
Version 1.1

Block 2 - GenerateErrorPrompt

Type / Role
n8n-nodes-base.code - code
Config choices
Version 2

Block 3 - Simple Memory

Type / Role
@n8n/n8n-nodes-langchain.memoryBufferWindow - memoryBufferWindow
Config choices
Version 1.3

Block 4 - AutoErrorFixing

Type / Role
n8n-nodes-base.if - if
Config choices
Version 2.2

Block 5 - IfError

Type / Role
n8n-nodes-base.if - if
Config choices
Version 2.2

Block 6 - Execute_AI_result

Type / Role
n8n-nodes-base.postgres - postgres
Config choices
Version 2.6

Block 7 - isAssistantExists

Type / Role
n8n-nodes-base.if - if
Config choices
Version 2.2

Block 8 - isAssistantExistsCode

Type / Role
n8n-nodes-base.code - code
Config choices
Version 2

Block 9 - isOpenAI

Type / Role
n8n-nodes-base.if - if
Config choices
Version 2.2

Block 10 - assistant

Type / Role
n8n-nodes-base.set - set
Config choices
Version 3.4

Block 11 - IfOpenAI

Type / Role
n8n-nodes-base.if - if
Config choices
Version 2.2

Block 12 - Sticky Note

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 13 - Sticky Note2

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 14 - IsMaxAutoErrorReached

Type / Role
n8n-nodes-base.if - if
Config choices
Version 2.2

Block 15 - OpenRouter Chat Model

Type / Role
@n8n/n8n-nodes-langchain.lmChatOpenRouter - lmChatOpenRouter
Config choices
Version 1

Block 16 - AgentName

Type / Role
n8n-nodes-base.set - set
Config choices
Version 3.4

Block 17 - setOutputByProvider

Type / Role
n8n-nodes-base.set - set
Config choices
Version 3.4

Block 18 - OpenAIMainBrain

Type / Role
@n8n/n8n-nodes-langchain.openAi - openAi
Config choices
Version 1.8

Block 19 - askUserHowToHandleError

Type / Role
n8n-nodes-base.set - set
Config choices
Version 3.4

Block 20 - maxAutoErrorLimitReached

Type / Role
n8n-nodes-base.set - set
Config choices
Version 3.4

Block 21 - If

Type / Role
n8n-nodes-base.if - if
Config choices
Version 2.2

Block 22 - wordsForUser1

Type / Role
n8n-nodes-base.set - set
Config choices
Version 3.4

Block 23 - isExecutable

Type / Role
n8n-nodes-base.if - if
Config choices
Version 2.2

Block 24 - getAssistantsList

Type / Role
@n8n/n8n-nodes-langchain.openAi - openAi
Config choices
Version 1.8

Showing the first 24 of 39 workflow blocks. Download the JSON for the full node graph.

3. Summary Table

Workflow Generate & test SQL code with GPT/OpenRouter AI and PostgreSQL sandbox
Complexity advanced
Nodes 39
Categories Engineering, AI Chatbot
Author Muhammadumar
Published 28 Jul 2025

4. Reproducing the Workflow from Scratch

  1. 1. Download the workflow JSON

    Use the JSON export at /data/workflows/6583/6583.json as the source template for this automation.

  2. 2. Import the template into n8n

    Open n8n, import the downloaded JSON, and review each node before activating the workflow.

  3. 3. Configure credentials and variables

    Replace placeholder credentials, API keys, webhook URLs, account IDs, and environment-specific values with your own settings.

  4. 4. Test with sample data

    Run the workflow manually or in a staging workspace, inspect node output, and confirm downstream systems receive the expected data.

  5. 5. Activate and monitor

    Enable the workflow only after testing, then monitor executions, errors, and rate limits during the first production runs.

5. General Notes & Resources

Review imported nodes carefully before activation. This catalog entry is intended to help you inspect the workflow structure, understand required services, and find related templates faster.

Node names, credentials, schedules, webhook paths, and external service limits may need adjustment for your workspace.

Frequently asked questions

What does Generate & test SQL code with GPT/OpenRouter AI and PostgreSQL sandbox do?

This is the core AI agent used for queryverify.com. Don't trust complex AI generated SQL queries without double checking them in a safe environment. That's where queryve...

What do I need before importing this workflow?

Review the workflow JSON, configure any required credentials in n8n, and test the automation in a safe workspace before using it in production.

Can I customize this workflow?

Yes. Use the block-by-block analysis and the downloadable JSON to inspect each node, then adjust credentials, prompts, schedules, filters, or destinations for your Engineering, AI Chatbot use case.