pg_query_sql
Execute a single read-only SQL query (SELECT, WITH, EXPLAIN) on PostgreSQL with write protection, row limits, and bind parameters for safe data retrieval.
Instructions
Run a single read-only SQL query against a PostgreSQL database.
Only SELECT, WITH and EXPLAIN are permitted. Writes and DDL (INSERT, UPDATE, DELETE, MERGE, COPY, CREATE, DROP, ALTER, TRUNCATE) are rejected before reaching the database, as are data-modifying CTEs.
Exactly one statement per call — do not send two statements separated by a semicolon. A single trailing semicolon is fine.
Results are capped at 1000 rows and 15s. The response reports "truncated": true when more rows matched than were returned, and "rowCount" is the number of rows returned, not the number matched — use a COUNT(*) query if you need the true total.
Pass literals through "values" as $1, $2 placeholders rather than building them into the SQL string.
bigint and numeric columns are returned as JSON strings, not numbers.
Use pg_describe to discover tables and columns instead of guessing at names.
EXPLAIN ANALYZE is rejected by default because it executes the statement it explains; plain EXPLAIN always works.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| sql | Yes | A single SELECT, WITH or EXPLAIN statement. Use $1, $2 for values. | |
| values | No | Positional bind parameters for the $1, $2 placeholders in sql. |