Query PostgreSQL from Slack
Convert natural language questions in Slack into read-only PostgreSQL queries, run them, and get summarized data insights delivered back to your channel.
Tags
Introduction
QueryMate is a workflow designed to turn natural language questions from Slack into precise PostgreSQL SELECT queries that fetch relevant data from your database. It then summarizes the query results and posts a clear, formatted response back to the Slack channel.
It accomplishes this through a sequence of steps:
- Listens for new messages in a specified Slack channel.
- Retrieves the database schema from PostgreSQL to understand the available tables and columns.
- Merges the Slack message with the schema information.
- Uses an AI model to convert the natural language question into a valid, read-only SQL SELECT query.
- Executes the generated SQL query on the PostgreSQL database.
- Summarizes the query results into a concise and user-friendly message and sends it back to the Slack channel.
What you need
- A Slack workspace with access to the target channel.
- A PostgreSQL database containing your data.
- API connectors set up in Needle for Slack (permissions to read messages and post in channels) and PostgreSQL (read access to the required schema and tables).
- Access to a Needle AI model for natural language processing.
How the flow works
Here is a breakdown of the steps and what they do in the workflow:
| Step | Node | Function |
|---|---|---|
| 1 | Slack Trigger | Listens for any new message posted in the configured Slack channel. |
| 2 | PostgreSQL Query | Fetches the current public schema details (tables, columns, data types). |
| 3 | Merge Node | Combines the Slack message text with the retrieved database schema. |
| 4 | AI SQL Generator | Translates the natural language question and schema into a safe, read-only SELECT query. |
| 5 | PostgreSQL Execution | Runs the AI-generated SELECT query against your database. |
| 6 | AI Summary | Analyzes the results and produces a concise, formatted summary message. |
| 7 | Slack Action | Posts the AI-generated summary back to the Slack channel. |
Output
At the end of the workflow, the user receives a neatly formatted, human-friendly summary in Slack that answers their natural language data question. This includes key insights or statistics extracted from the database query results, making data access conversational and effortless.
Notes
- The generated SQL is strictly read-only to prevent accidental data changes.
- If the question cannot be answered from the available schema, the workflow explains why no query was run.
- Ensure your Slack bot and PostgreSQL credentials are properly configured for seamless operation.
- You can customize the AI prompt to better fit your database schema naming conventions or specific query rules.
Want to showcase your own workflows?
Become a Needle workflow partner and turn your expertise into recurring revenue.
