Execute Read-Only SQL Query
pg_queryExecute read-only SQL SELECT queries on a PostgreSQL database. Supports CTEs and EXPLAIN, with configurable row limit and timeout.
Instructions
Execute a read-only SQL SELECT query against the PostgreSQL database.
Only SELECT, WITH (CTE), TABLE, and EXPLAIN statements are allowed. Any attempt to execute INSERT, UPDATE, DELETE, DDL, or other mutating statements will be rejected.
Args:
sql: The SQL SELECT statement to execute (required)
limit: Maximum rows to return, 1-1000 (default: 100)
timeout_ms: Query timeout in milliseconds (default: 30000)
response_format: Output format
Returns: JSON: { rows: object[], row_count: number, column_names: string[] } Markdown: formatted table of results
Examples:
"SELECT * FROM users WHERE active = true" → rows with limit applied
"WITH stats AS (SELECT ...) SELECT * FROM stats" → CTE supported
"EXPLAIN SELECT * FROM orders" → query plan
Errors:
"Only read-only queries allowed" if non-SELECT statement attempted
PostgreSQL syntax/permission errors returned as-is
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| sql | Yes | SELECT SQL statement to execute | |
| limit | No | Maximum rows to return (default: 100, max: 1000) | |
| timeout_ms | No | Query timeout in milliseconds (default: 30000) | |
| response_format | No | Output format: 'markdown' for human-readable, 'json' for machine-readable | markdown |