skills/postgres-query/SKILL.md
Query a Postgres database (read-only) and return structured results
npx skillsauth add sofer/.agents postgres-queryInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
Execute read-only queries against a Postgres database and return structured results. This skill enables other skills to access business data stored in Postgres (e.g. signup metrics, campaign performance, user data).
This skill is read-only by default. It refuses to execute any statement that would modify data.
Check that database connection credentials are available from environment variables. Two formats are supported:
DATABASE_URL (e.g. postgresql://user:pass@host:5432/dbname). If set, this takes precedence.PGHOST, PGDATABASE, PGUSER, PGPASSWORD (all required), plus PGPORT (optional, defaults to 5432).If neither DATABASE_URL nor the required individual variables are set, stop and report:
Postgres credentials not configured. Set the following environment variables: PGHOST, PGDATABASE, PGUSER, PGPASSWORD (or DATABASE_URL as a connection URI).
Never hardcode credentials. Never log or output credential values.
If the input is an explicit SQL statement (starts with SELECT, WITH, or EXPLAIN), use it directly. Proceed to step 3.
If the input is a natural-language description, inspect the database schema first:
List available tables:
SELECT table_schema, table_name
FROM information_schema.tables
WHERE table_schema NOT IN ('pg_catalog', 'information_schema')
ORDER BY table_schema, table_name;
For tables that appear relevant to the request, inspect their columns:
SELECT column_name, data_type, is_nullable
FROM information_schema.columns
WHERE table_name = '{table}'
ORDER BY ordinal_position;
Formulate an appropriate SELECT query based on the schema and the natural-language request.
Apply a default LIMIT 100 unless the caller specified a different limit or the query already contains a LIMIT clause.
Before executing any SQL, verify the statement is read-only. This check is mandatory and cannot be skipped.
Refuse the query if any of the following are true:
SELECT, WITH, or EXPLAININSERT, UPDATE, DELETE, DROP, ALTER, TRUNCATE, CREATE, GRANT, REVOKECOPY ... FROMImportant nuance: Check for these as SQL commands, not merely as substrings. A column named update_count or a WHERE clause like WHERE deleted = false should not trigger a rejection. Analyse the statement structure. When in doubt, err on the side of caution and refuse.
If the query fails the read-only check, stop and report:
This skill is read-only. The query contains a write operation ({operation}). Only SELECT queries are permitted.
Use one of the following methods, in order of preference:
Option A: Postgres MCP server (preferred if available)
If a Postgres MCP server is configured and accessible, use it to execute the query. This provides structured input/output.
Option B: psql command line (fallback)
Execute the query using psql:
psql -c "{query}" --csv --no-psqlrc -v ON_ERROR_STOP=1
--csv produces machine-readable CSV output--no-psqlrc prevents user configuration from altering output format-v ON_ERROR_STOP=1 stops on errorsIf DATABASE_URL is set, pass it as the connection parameter. Otherwise, psql reads PGHOST, PGPORT, PGDATABASE, PGUSER, and PGPASSWORD from the environment automatically.
Present the results as structured data:
If the query returned no rows, return an empty result set (rows: [], row_count: 0). This is not an error.
Return the following to the invoking context:
query_executed: <the SQL that was run>
row_count: <number of rows>
truncated: <true if result was limited>
<results as a formatted table or structured data>
When used as a step in a composite skill, the labelled output contains the full result set for subsequent steps to use.
tools
Check whether Claude and Codex have equivalent access to shared agent resources, skills, hooks, plugins, MCP servers, permissions, startup behaviour, and provider-specific adapter config. Use when comparing agent environments, debugging missing capabilities after restart, or deciding whether to symlink a resource or configure a runtime.
testing
Record substantive skill use in an append-only local log. Use after choosing or invoking a non-system skill for real work, when a skill is inspected but not used, or when a skill fails to apply. Do not use for routine system skills or incidental file reads.
testing
Turn a vague or underspecified request into a self-contained problem statement. Use when the user has a rough idea, when a request would fail if handed directly to an agent, or before non-trivial work that needs shared understanding.
data-ai
Append a one-line learning to ~/.agents/learning-log.md. Use when the user types /learning, or when something genuinely worth remembering surfaced during work and the user confirms it should be captured.