PostgreSQL Integration (QueryWeaver)
Connect PostgreSQL databases and query them using natural language.
Overview
QueryWeaver allows your chatbot to:
- Understand your database schema
- Convert questions to SQL queries
- Execute queries safely
- Format results for users
How It Works
- User asks a question in natural language
- WizChat understands the database schema
- AI generates appropriate SQL query
- Query is executed safely
- Results are formatted and presented
Example:
- User: "How many orders were placed last month?"
- SQL:
SELECT COUNT(*) FROM orders WHERE created_at >= '2024-01-01' AND created_at < '2024-02-01' - Response: "There were 1,247 orders placed last month."
Connecting a Database
- Go to "Settings" > "Integrations"
- Click "PostgreSQL"
- Enter connection details:
- Host
- Port (default: 5432)
- Database name
- Username
- Password
- Click "Test Connection"
- Click "Save"
Connection Options
| Option | Description |
|---|---|
| SSL Mode | Require, prefer, or disable SSL |
| Connection Timeout | Max connection wait time |
| Query Timeout | Max query execution time |
| Read Only | Restrict to SELECT queries |
Schema Scanning
After connecting, WizChat scans your schema:
- Tables and columns are discovered
- Data types are identified
- Relationships are mapped
- Descriptions can be added
Viewing Schema
- Go to "Integrations" > "PostgreSQL"
- Click "Schema"
- Browse tables and columns
Adding Descriptions
Help the AI understand your schema:
- Click on a table or column
- Add a description
- Click "Save"
Example:
- Table
cust: "Customer information table" - Column
amt: "Order amount in USD"
Query Safety
Read-Only Mode
By default, only SELECT queries are allowed:
- No INSERT, UPDATE, DELETE
- No schema changes
- Safe for production databases
Query Review
Optionally review queries before execution:
- Go to "Settings" > "PostgreSQL"
- Enable "Review Queries"
- Queries show SQL before running
Row Limits
Prevent large result sets:
- Default: 100 rows
- Configurable per database
- Pagination for larger results
Example Queries
| Natural Language | Generated SQL |
|---|---|
| "Show me top 10 customers" | SELECT * FROM customers ORDER BY total_purchases DESC LIMIT 10 |
| "What's our monthly revenue?" | SELECT DATE_TRUNC('month', order_date), SUM(amount) FROM orders GROUP BY 1 |
| "Find orders over $1000" | SELECT * FROM orders WHERE amount > 1000 |
Best Practices
Database Setup
- Use a read-only database user
- Grant minimal required permissions
- Consider using a replica
- Set appropriate timeouts
Schema Optimization
- Add descriptions to tables/columns
- Use clear naming conventions
- Document relationships
- Exclude sensitive tables
Performance
- Add indexes for common queries
- Set appropriate row limits
- Monitor query performance
- Use query timeouts
Security
- Credentials are encrypted
- Connections use SSL when available
- Queries are parameterized
- Access can be revoked anytime
warning
Never connect databases containing sensitive data (passwords, PII) without proper security review.
Troubleshooting
Connection Failed
- Verify host and port
- Check firewall rules
- Confirm credentials
- Test SSL settings
Queries Not Working
- Check schema descriptions
- Verify table permissions
- Review generated SQL
- Add more context to questions
Slow Queries
- Add appropriate indexes
- Reduce row limits
- Check database performance
- Optimize complex queries