Execute a SQL query in the database
Read-only
What do risk levels mean?
- Read-only
- Inspects state without changing anything.
- Low risk
- Reversible with a routine follow-up command.
- Medium risk
- Changes state; undo path documented.
- Destructive
- Deletes or overwrites; confirmation required.
Runs a read-only SQL query (SELECT, EXPLAIN, SHOW, or WITH) against the connected database and prints the result set, so you can inspect data or test a query before embedding it in application code. Write and DDL statements are rejected before the query is ever substituted into a command.
postgresqlquerysqlinspect
Parameters
The read-only SQL query to execute.
Commands
Execute the supplied query and display the result set
psql -c "<query>"Verification
psql -c "SELECT 1 AS alive;"A single row with the value 1 confirms the connection and query execution work.
Undo
Not undoable
Read-only queries do not change state, so there is nothing to undo.
Pitfalls
- Write and DDL queries (INSERT, UPDATE, DELETE, DROP, ALTER, TRUNCATE, CREATE, and similar) are rejected by the query pattern before the recipe ever runs psql.
- The pattern only constrains the leading statement keyword; a semicolon-separated write statement chained after a valid read-only query (e.g. "SELECT 1; DROP TABLE t;") is not caught, so multi-statement input should still be treated with care.
- Only the last result set is displayed by default when a query does contain multiple statements.
- Long-running queries may time out; use EXPLAIN first on complex queries.