🤖 Query PostgreSQL database with natural language using GPT-4o-mini

624 views · 🤖 AI Chatbots & Agents

Description

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

🎯 Perfect For

🚀 Quick Setup

Step 1: Prerequisites

Step 2: Import & Configure

  1. Import this workflow template into n8n
  2. Add Credentials:
    • OpenAI API: Add your API key
    • PostgreSQL: Configure database connection
  3. Set Table Name: Edit “Set Table Name” node → Replace "table_name" with your actual table
  4. Test Connection: Ensure your database user has SELECT permissions

Step 3: Deploy & Use

  1. Start the workflow
  2. Open the chat interface
  3. Ask questions like:
    • “Show all active users”
    • “Find orders from last month over $100”
    • “List products with low inventory”

🔧 Configuration Details

Required Settings

Optional Customizations

💡 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

🔍 How It Works

  1. Schema Discovery: Agent fetches table structure and column info
  2. Query Planning: Maps natural language to database columns
  3. SQL Generation: Creates safe, optimized queries
  4. Result Formatting: Returns clean, user-friendly data

⚡ Quick Troubleshooting

🎨 Use Cases

🔧 Advanced Tips


Tags: AI, PostgreSQL, Natural Language, SQL, Business Intelligence, LangChain, Database Query

Difficulty: Beginner to Intermediate
Setup Time: 10-15 minutes

🔗 Nodes Used

AI Agent, OpenAI Chat Model, Simple Memory, Chat Trigger

📥 Import

Download workflow.json and import into n8n: Workflow menu → Import from File

📖 Importing guide · 🔑 Credential setup