postgres-intelligence
Credential-safe PostgreSQL access for LLM coding agents. The scripts load credentials from .env at runtime and print only safe summaries, schema metadata, query results, and errors. The agent writes SQL, calls the scripts, and reads that output; it never opens or prints .env itself.
Install
.env, scripts/, and schema_metadata.json all live in the skill directory. Run every command in this file from there.
cd postgres-intelligence # repo root
python3 -m venv .venv
. .venv/bin/activate
python -m pip install -r requirements.txt
cd skills/postgres-intelligence # the skill directory
cp ../../.env.example .env
python scripts/config.py
python scripts/db_connector.py
python scripts/schema_extractor.py
Configure
Fill .env from the keys in ../../.env.example, the public template: connections DB1_ through DB10_, or DB1_DSN, which takes precedence over the host/port fields. DB*_NAME is the connection key --db expects. Keep real .env, .venv, and schema_metadata.json out of git.
Commands
# Validate loaded config; prints host, user, and database, never passwords or DSNs
python scripts/config.py
# Test all configured connections
python scripts/db_connector.py
# Extract schema metadata for every connection into schema_metadata.json
python scripts/schema_extractor.py
# Run a read-only query against the default connection
python scripts/query_executor.py "SELECT current_database(), current_schema();"
# Select a named connection
python scripts/query_executor.py --db analytics "SELECT count(*) FROM public.events;"
# Agent-friendly JSON output
python scripts/query_executor.py --json-only "SELECT now();"
# Raise the 30000 ms statement_timeout for a long analytical query
python scripts/query_executor.py --statement-timeout-ms 120000 "SELECT count(*) FROM public.events;"
# Writes require explicit user approval
python scripts/query_executor.py --allow-write "UPDATE table_name SET flag = true WHERE id = 1;"
# DDL requires explicit user approval
python scripts/query_executor.py --allow-ddl "CREATE INDEX CONCURRENTLY idx_name ON table_name (col);"
Agent Workflow
- Identify the target connection, schema, table, time range, and result limit.
- Run
python scripts/config.py to confirm the connection is configured; it validates .env without printing secrets.
- Read
schema_metadata.json in the skill directory before generating SQL; if it is absent or stale, run python scripts/schema_extractor.py to rewrite it.
- If the target table is still missing from that metadata, query
information_schema or pg_catalog for it first.
- Prefer explicit columns over
SELECT *; add LIMIT for exploratory reads.
- On errors, use
sqlstate and suggestions to refine the query, with a maximum of three attempts.
- Report the executed SQL, key rows, row count, and reasoning. Do not report credentials.
Safety Model
- Read-only by default:
SELECT, WITH, SHOW, and EXPLAIN run with no flag.
- Writes require
--allow-write.
- DDL and maintenance commands require
--allow-ddl.
UPDATE and DELETE without WHERE are blocked.
- Multiple SQL statements in one call are blocked.
- Every query runs under a
statement_timeout of 30000 ms unless --statement-timeout-ms raises it.
- Passwords and full DSNs are never printed.
PostgreSQL Guidance
- Use
EXPLAIN (ANALYZE, BUFFERS) for performance work.
- Verify index usage before and after adding indexes.
- Use
CREATE INDEX CONCURRENTLY for large production tables when appropriate.
- Run
ANALYZE after bulk data changes.
- Use B-tree for common equality/range access, GIN for JSONB containment and full-text patterns, BRIN for large append-only time-series tables.
- Use connection pooling such as PgBouncer for long-running applications; agent scripts are short-lived.
Read ../../references/postgres_best_practices.md before recommending an index, a partitioning or schema change, or a maintenance job.
1---2name: postgres-intelligence3description: PostgreSQL for LLM agents: inspect schemas, run safe SQL, translate natural language to queries. Use when connecting to a PostgreSQL database, exploring an unfamiliar schema, debugging a SQL error, or investigating query performance.4---567# postgres-intelligence89Credential-safe PostgreSQL access for LLM coding agents. The scripts load credentials from `.env` at runtime and print only safe summaries, schema metadata, query results, and errors. The agent writes SQL, calls the scripts, and reads that output; it never opens or prints `.env` itself.1011## Install1213`.env`, `scripts/`, and `schema_metadata.json` all live in the skill directory. Run every command in this file from there.1415```bash16cd postgres-intelligence # repo root17python3 -m venv .venv18. .venv/bin/activate19python -m pip install -r requirements.txt20cd skills/postgres-intelligence # the skill directory21cp ../../.env.example .env22python scripts/config.py23python scripts/db_connector.py24python scripts/schema_extractor.py25```2627## Configure2829Fill `.env` from the keys in `../../.env.example`, the public template: connections `DB1_` through `DB10_`, or `DB1_DSN`, which takes precedence over the host/port fields. `DB*_NAME` is the connection key `--db` expects. Keep real `.env`, `.venv`, and `schema_metadata.json` out of git.3031## Commands3233```bash34# Validate loaded config; prints host, user, and database, never passwords or DSNs35python scripts/config.py3637# Test all configured connections38python scripts/db_connector.py3940# Extract schema metadata for every connection into schema_metadata.json41python scripts/schema_extractor.py4243# Run a read-only query against the default connection44python scripts/query_executor.py "SELECT current_database(), current_schema();"4546# Select a named connection47python scripts/query_executor.py --db analytics "SELECT count(*) FROM public.events;"4849# Agent-friendly JSON output50python scripts/query_executor.py --json-only "SELECT now();"5152# Raise the 30000 ms statement_timeout for a long analytical query53python scripts/query_executor.py --statement-timeout-ms 120000 "SELECT count(*) FROM public.events;"5455# Writes require explicit user approval56python scripts/query_executor.py --allow-write "UPDATE table_name SET flag = true WHERE id = 1;"5758# DDL requires explicit user approval59python scripts/query_executor.py --allow-ddl "CREATE INDEX CONCURRENTLY idx_name ON table_name (col);"60```6162## Agent Workflow63641. Identify the target connection, schema, table, time range, and result limit.652. Run `python scripts/config.py` to confirm the connection is configured; it validates `.env` without printing secrets.663. Read `schema_metadata.json` in the skill directory before generating SQL; if it is absent or stale, run `python scripts/schema_extractor.py` to rewrite it.674. If the target table is still missing from that metadata, query `information_schema` or `pg_catalog` for it first.685. Prefer explicit columns over `SELECT *`; add `LIMIT` for exploratory reads.696. On errors, use `sqlstate` and suggestions to refine the query, with a maximum of three attempts.707. Report the executed SQL, key rows, row count, and reasoning. Do not report credentials.7172## Safety Model7374- Read-only by default: `SELECT`, `WITH`, `SHOW`, and `EXPLAIN` run with no flag.75- Writes require `--allow-write`.76- DDL and maintenance commands require `--allow-ddl`.77- `UPDATE` and `DELETE` without `WHERE` are blocked.78- Multiple SQL statements in one call are blocked.79- Every query runs under a `statement_timeout` of 30000 ms unless `--statement-timeout-ms` raises it.80- Passwords and full DSNs are never printed.8182## PostgreSQL Guidance8384- Use `EXPLAIN (ANALYZE, BUFFERS)` for performance work.85- Verify index usage before and after adding indexes.86- Use `CREATE INDEX CONCURRENTLY` for large production tables when appropriate.87- Run `ANALYZE` after bulk data changes.88- Use B-tree for common equality/range access, GIN for JSONB containment and full-text patterns, BRIN for large append-only time-series tables.89- Use connection pooling such as PgBouncer for long-running applications; agent scripts are short-lived.9091Read `../../references/postgres_best_practices.md` before recommending an index, a partitioning or schema change, or a maintenance job.92