Use this robust n8n workflow to generate, execute, and automatically debug PostgreSQL queries using GPT via OpenAI or OpenRouter. A powerful n8n template for developers.
Download this n8n workflow template and start using it instantly.
This advanced n8n workflow template solves the significant challenge of manually validating AI-generated SQL. By integrating large language models (LLMs) like GPT (via OpenAI or OpenRouter) directly with a PostgreSQL database, this n8n automation creates a highly efficient and self-debugging SQL development tool. The core value of this n8n workflow lies in its built-in error handling loop. If the initial SQL execution fails in the sandbox, the system automatically captures the error details via a dedicated n8n node (ExecuteAIresult), sends the error back to the AI for self-correction, and re-attempts execution. This powerful loop ensures the output is always executable, making this a highly robust and reliable n8n workflow.
The entire process is initiated by a specialized n8n trigger and moves through several conditional steps to manage AI interaction and database execution.
When chat message received n8n trigger, capturing the user's natural language request and crucial configuration parameters, such as the desired aiProvider and the autoErrorFixing setting. The localVariables n8n node then sets up the strict, detailed instruction prompt necessary for directing the AI to act as a 'Senior PostgreSQL developer,' ensuring structured SQL output.aiProvider. If OpenAI is selected, the n8n template uses the Assistant API for seamless history management. If OpenRouter is chosen, the Simple Memory n8n node is used to maintain conversational context.OpenAIMainBrain or OpenRouterAgent) generate the complete SQL script, which includes schema creation (CREATE TABLE), mock data insertion, and the final solution query.ExecuteAIresult PostgreSQL n8n node attempts to execute the generated code. If successful, the result is captured. If an error occurs, the flow checks the AutoErrorFixing parameter.GenerateErrorPrompt n8n node constructs a specific prompt detailing the execution error. This prompt is sent back to the AI for refinement, creating a loop. The IsMaxAutoErrorReached n8n node acts as a safeguard, limiting the loop iteration count (to 4) to prevent indefinite execution, ensuring this powerful n8n template remains cost-effective.To deploy this powerful n8n workflow, follow these steps:
ExecuteAIresult n8n node and set up your PostgreSQL connection credentials. This database acts as the SQL sandbox where the generated code is executed.When chat message received n8n trigger requires specific input data (e.g., sessionId, chatInput, aiProvider). Ensure your application or frontend supplies these parameters correctly when invoking this n8n workflow via its webhook ID. When chat message received (LangChain Chat Trigger): The starting n8n trigger that captures user input (natural language prompt) and required configuration parameters like aiProvider, model, and autoErrorFixing.
localVariables (Set): Initializes the core instructional prompt for the AI, defining its role as a senior PostgreSQL developer and establishing the strict SQL formatting rules required by this n8n template.
OpenAIMainBrain (OpenAI): The key n8n node for OpenAI users, interacting with the OpenAI Assistant API. It manages message threads and leverages existing assistants or creates a new one based on the model specified in the n8n trigger data.
OpenRouterAgent (LangChain Agent): The main n8n node for users opting for OpenRouter. It uses the Simple Memory n8n node to maintain the session history for the AI, adapting the n8n workflow for different LLM providers.
ExecuteAIresult (Postgres): The critical database n8n node. It attempts to execute the full SQL script generated by the AI. It is configured to continue to the error output if execution fails, facilitating the self-correction loop.
GenerateErrorPrompt (Code): When execution fails, this custom n8n node generates a new, highly specific prompt. This prompt includes the original user request and the technical PostgreSQL error message, instructing the AI to fix the query.
Use this powerful n8n workflow to automatically generate and send multilingual spare parts price quotes using Gmail, Google Sheets, and Gemini AI. This highly functional n8n template streamlines sales operations.

Use this comprehensive n8n workflow to automate the sales quotation process. Triggered by a Pipedrive stage change, it generates a custom PDF quote and initiates an e-signature request via DottedSign.

Use this robust n8n workflow to instantly generate platform-specific social media posts for LinkedIn and X using Gemini AI, and automatically curate engaging content ideas via a scheduled n8n trigger.

Use this n8n workflow template to automate the transcription of videos using local OpenAI Whisper integration, translate subtitles via LibreTranslate, and deliver the final SRT files via Gmail.

Use this powerful n8n workflow to automatically generate featured images via Leonardo AI and instantly upload them to your WordPress media library. Perfect for automated content pipelines.








































