A secure and efficient PostgreSQL query tool for Dify that allows you to execute SQL queries directly from your AI applications. This version includes enhanced validation, better error handling, and support for schema discovery queries.
- Secure Query Execution: Validates queries to block dangerous operations (e.g. DROP, TRUNCATE, GRANT)
- Extended Query Support: Works with
SELECT
,INSERT
,UPDATE
,DELETE
,WITH (CTE)
,SHOW
, andEXPLAIN
- Schema Discovery: Safe access to
information_schema
andpg_catalog
queries - Markdown Handling: Cleans queries wrapped in ```sql blocks
- Error Handling: Clear, detailed error messages for debugging
- Result Formatting: Returns results in both text and JSON formats, with previews for schema queries
- Connection Management: Efficient connection pooling and automatic rollback for SELECT queries
- Configurable Fetch Size: Control the number of rows returned
-
Clone or download this plugin repository
-
Build the plugin package:
dify plugin package
-
Install the generated
.difypkg
file in Dify Plugin Marketplace -
Configure your PostgreSQL database credentials
When setting up the plugin, you'll need to provide:
- Database Host: PostgreSQL server address
- Database Port: PostgreSQL port (default: 5432)
- Database Name: Database to connect to
- Database User: Username for authentication
- Database Password: Password for authentication
The plugin provides one tool: Execute Query
- query (required): SQL query to execute
- fetch_size (optional): Max rows to return for SELECT queries (default: 100)
-- Simple select
SELECT * FROM users LIMIT 5;
-- Insert with RETURNING
INSERT INTO logs (message, level) VALUES ('Started', 'INFO') RETURNING id;
-- Update records
UPDATE users SET last_login = NOW() WHERE id = 123;
-- Delete expired sessions
DELETE FROM sessions WHERE expires_at < NOW();
-- Schema discovery
SELECT column_name, data_type
FROM information_schema.columns
WHERE table_name = 'users';
-- With clause (CTE)
WITH active_users AS (
SELECT * FROM users WHERE is_active = TRUE
)
SELECT * FROM active_users;
- Dangerous operations (
DROP
,TRUNCATE
,GRANT
, etc.) are blocked - Only single safe statements are executed (no stacked queries)
- Schema inspection