Query PostgreSQL Database with Natural Language using GPT-4o-mini
This Database SQL Query Agent convert natural language into sql query to get results
Turn your PostgreSQL database into a conversational AI agent! Ask questions in plain English and get instant data results without writing SQL.
✨ What It Does
Natural Language Queries**: "Show laptops under $500 in stock" → Automatic SQL generation Smart Column Mapping**: Understands your terms and maps them to actual database columns Conversational Memory**: Maintains context across multiple questions Universal Compatibility**: Works with any PostgreSQL table structure
🎯 Perfect For
Business analysts querying data without SQL knowledge Customer support finding information quickly Product managers analyzing inventory/sales data Anyone who needs database insights fast
🚀 Quick Setup
Step 1: Prerequisites n8n instance (cloud/self-hosted) PostgreSQL database with read access OpenAI API key/You can use other LLM as well
Step 2: Import & Configure Import this workflow template into n8n Add Credentials: OpenAI API: Add your API key PostgreSQL: Configure database connection Set Table Name: Edit "Set Table Name" node → Replace "table_name" with your actual table Test Connection: Ensure your database user has SELECT permissions
Step 3: Deploy & Use Start the workflow Open the chat interface Ask questions like: "Show all active users" "Find orders from last month over $100" "List products with low inventory"
🔧 Configuration Details
Required Settings Table Name**: Update in "Set Table Name" node Database Schema**: Default is 'public' (modify SQL if different) Result Limit**: Default 50 rows (adjustable in system prompt)
Optional Customizations Multi-table Support**: Modify system prompt and add table selection logic Custom Filters**: Add business rules to restrict data access Output Format**: Customize response formatting in the agent prompt
💡 Example Queries
E-commerce "Show me all electronics under $200 that are in stock"
HR Database "List employees hired in 2024 with salary over 70k"
Customer Data "Find VIP customers from California with recent orders"
🛡️ Security Features
Read-only Operations**: Only SELECT queries allowed SQL Injection Prevention**: Parameterized queries and validation Result Limits**: Prevents overwhelming queries Safe Schema Discovery**: Uses information_schema tables
🔍 How It Works
Schema Discovery: Agent fetches table structure and column info Query Planning: Maps natural language to database columns SQL Generation: Creates safe, optimized queries Result Formatting: Returns clean, user-friendly data
⚡ Quick Troubleshooting
No Results**: Check table name and ensure data exists Permission Error**: Verify database user has SELECT access Connection Failed**: Confirm PostgreSQL credentials and network access Unexpected Results**: Try more specific queries with exact column names
🎨 Use Cases
Inventory Management**: "Show low-stock items by category" Sales Analysis**: "Top 10 products by revenue this quarter" Customer Support**: "Find customer orders with status 'pending'" Data Exploration**: "What are the unique product categories?"
🔧 Advanced Tips
Performance**: Add database indexes on frequently queried columns Customization**: Modify the system prompt for domain-specific terminology Scaling**: Use read replicas for high-query volumes Integration**: Connect to Slack/Teams for team-wide data access
Tags: AI, PostgreSQL, Natural Language, SQL, Business Intelligence, LangChain, Database Query
Difficulty: Beginner to Intermediate
Setup Time: 10-15 minutes
Related Templates
Use OpenRouter in n8n versions <1.78
What it is: In version 1.78, n8n introduced a dedicated node to use the OpenRouter service, which lets you to use a lot...
Task Deadline Reminders with Google Sheets, ChatGPT, and Gmail
Intro This template is for project managers, team leads, or anyone who wants to automatically remind teammates of tasks ...
🤖 Build Resilient AI Workflows with Automatic GPT and Gemini Failover Chain
This workflow contains community nodes that are only compatible with the self-hosted version of n8n. How it works This...
🔒 Please log in to import templates to n8n and favorite templates
Workflow Visualization
Loading...
Preparing workflow renderer
Comments (0)
Login to post comments