What This Automation Does
This workflow answers chat questions by making SQL queries to a Postgres database automatically.
It stops manual querying and saves time.
Users get quick, accurate answers when they ask in chat.
Inputs come from chat messages.
The AI reads user questions, turns them into SQL commands, and runs those on your database.
The results get sent back as replies.
This cuts work for data teams and stops mistakes from hand-made queries.
Memory keeps chat history so the bot can answer follow-up questions naturally.
Tools and Services Used
- LangChain Chat Trigger: Starts workflow when chat messages come in.
- LangChain AI Agent: Understands questions, remembers chats, and runs SQL.
- OpenAI Chat Model (GPT-4o-mini): Turns words into SQL queries.
- Postgres Node: Runs generated SQL commands.
- LangChain Simple Memory Buffer: Saves past messages for context.
Beginner step-by-step: How to use this workflow in n8n
Import the Workflow
- Open your n8n editor.
- Use the Download button on this page to get the workflow file.
- Click “Import from File” inside n8n.
- Select the downloaded workflow file to load it.
Configure Credentials and IDs
- Add your Postgres database credentials in the Postgres node.
- Input your OpenAI API key inside the OpenAI Chat Model node.
- If needed, update any webhook IDs or channel names in the Chat Trigger node.
- Check SQL queries or any custom prompts if provided.
Test and Activate
- Send a test chat message using the chat client connected to your webhook.
- Verify the AI generates SQL on the fly and returns data correctly.
- Once tests pass, turn on the workflow toggle to enable it for production.
- If self hosting the workflow, consider using self-host n8n for public webhook access.
Inputs, Process, and Outputs
Inputs
- User chat messages sent via LangChain interface.
Processing Steps
- Chat Trigger node catches incoming chat.
- AI Agent node processes natural language question.
- AI creates SQL query based on question and database schema.
- Postgres node runs the SQL.
- Simple Memory node keeps track of conversation history.
- AI Agent combines all to form a chat reply.
Outputs
- Chat responses showing data pulled from the database.
Common Issues and How to Fix Them
- No reply after sending message
Check webhook URL is correct.
Make sure n8n instance can receive external calls. - Errors running SQL queries
Verify AI-generated SQL is correct and tables exist.
Run queries manually for debugging. - Chatbot does not remember past messages
Check if the Simple Memory node is linked to the AI Agent memory input.
Customization Ideas
- Change Postgres node credentials and query style to support MySQL or SQLite.
- Swap out the OpenAI Chat Model GPT model to a more advanced or cheaper one.
- Adjust the Simple Memory node to keep less or more conversation to balance memory and speed.
- Modify the Chat Trigger node to filter which users trigger the flow or add commands.
Summary
✓ Workflow turns chat questions into SQL queries automatically.
✓ Answers come quickly without expert help.
✓ Saves time and lowers errors in database querying.
✓ Keeps chat context for natural conversation.
✓ Works with Postgres and can switch to other databases.
✓ User-friendly setup by importing on n8n and entering keys.
