PostgreSQL Read-Only Query
Run safe, read-only SQL against one or more configured PostgreSQL databases. All
write operations (INSERT, UPDATE, DELETE, DROP, etc.) are blocked. Helper script
lives in the source repo at skills/postgres/scripts/query.py (needs
psycopg2-binary, installable via the repo's requirements.txt).
When to use
- User wants to query a Postgres database or inspect its data.
- Exploring schemas/tables of a Postgres instance.
- Running SELECT queries for data analysis.
- Multiple databases configured — the skill auto-selects by matching intent to each db's description.
Setup
Create connections.json in the skill dir OR ~/.config/claude/postgres-connections.json.
It holds credentials, so lock it down:
chmod 600 connections.json
{
"databases": [
{
"name": "production",
"description": "Main app database - users, orders, transactions",
"host": "db.example.com",
"port": 5432,
"database": "app_prod",
"user": "readonly_user",
"password": "your-password",
"sslmode": "require"
}
]
}
Config fields: name (required, case-insensitive id), description (required,
drives auto-selection), host (required), port (default 5432), database
(required), user (required), password (required), sslmode (default
prefer; options: disable, allow, prefer, require, verify-ca, verify-full).
Usage
# List configured databases
python3 scripts/query.py --list
# Run a query
python3 scripts/query.py --db production --query "SELECT * FROM users LIMIT 10"
# List tables
python3 scripts/query.py --db production --tables
# Show schema
python3 scripts/query.py --db production --schema
# Cap result rows
python3 scripts/query.py --db production --query "SELECT * FROM orders" --limit 100
Database selection
Match user intent to a db description:
| User asks about |
description containing |
| users, accounts |
users, accounts, customers |
| orders, sales |
orders, transactions, sales |
| analytics, metrics |
analytics, metrics, reports |
| logs, events |
logs, events, audit |
If unclear, run --list and ask the user which database.
Workflow
--list to show available databases.
- Match user intent to a db description.
--tables / --schema to explore structure.
- Execute the query with an appropriate LIMIT.
Safety features
- Read-only session (
readonly=True connection — primary protection).
- Only SELECT / SHOW / EXPLAIN / WITH queries allowed; multi-statement queries rejected.
- 30-second statement timeout.
- Max 10,000 rows per query (OOM guard); 100-char column-width cap for output.
- Error messages sanitize credentials.
- SSL mode configurable.
Troubleshooting
| Error |
Fix |
| Config not found |
Create connections.json in skill dir |
| Authentication failed |
Check user/password in config |
| Connection timeout |
Verify host/port; check firewall/VPN |
| SSL error |
Try "sslmode": "disable" for local dbs |
| Permission warning |
chmod 600 connections.json |
Exit codes: 0 success, 1 error (config missing, auth fail, invalid query, db error).
1---2name: postgres3description: "Use when querying a PostgreSQL database, exploring db schemas/tables, running read-only SELECT queries against postgres, or checking database contents across multiple configured connections — keywords: postgres, postgresql, psql, SQL query, SELECT, database schema, list tables, psycopg2."4version: 1.0.05license: Apache-2.06tags: [postgres, postgresql, sql, database, query, read-only, schema, psycopg2]7source: https://github.com/sanjay3290/ai-skills/tree/main/skills/postgres8derived_from: awesomeclaude9prerequisites:10 commands: [python3]11metadata:12 source: https://skillmd.com/skills/aashutosh396/postgres13---1415# PostgreSQL Read-Only Query1617Run safe, read-only SQL against one or more configured PostgreSQL databases. All18write operations (INSERT, UPDATE, DELETE, DROP, etc.) are blocked. Helper script19lives in the source repo at `skills/postgres/scripts/query.py` (needs20`psycopg2-binary`, installable via the repo's `requirements.txt`).2122## When to use2324- User wants to query a Postgres database or inspect its data.25- Exploring schemas/tables of a Postgres instance.26- Running SELECT queries for data analysis.27- Multiple databases configured — the skill auto-selects by matching intent to each db's description.2829## Setup3031Create `connections.json` in the skill dir OR `~/.config/claude/postgres-connections.json`.32It holds credentials, so lock it down:3334```bash35chmod 600 connections.json36```3738```json39{40 "databases": [41 {42 "name": "production",43 "description": "Main app database - users, orders, transactions",44 "host": "db.example.com",45 "port": 5432,46 "database": "app_prod",47 "user": "readonly_user",48 "password": "your-password",49 "sslmode": "require"50 }51 ]52}53```5455Config fields: `name` (required, case-insensitive id), `description` (required,56drives auto-selection), `host` (required), `port` (default 5432), `database`57(required), `user` (required), `password` (required), `sslmode` (default58`prefer`; options: disable, allow, prefer, require, verify-ca, verify-full).5960## Usage6162```bash63# List configured databases64python3 scripts/query.py --list6566# Run a query67python3 scripts/query.py --db production --query "SELECT * FROM users LIMIT 10"6869# List tables70python3 scripts/query.py --db production --tables7172# Show schema73python3 scripts/query.py --db production --schema7475# Cap result rows76python3 scripts/query.py --db production --query "SELECT * FROM orders" --limit 10077```7879## Database selection8081Match user intent to a db `description`:8283| User asks about | description containing |84|---|---|85| users, accounts | users, accounts, customers |86| orders, sales | orders, transactions, sales |87| analytics, metrics | analytics, metrics, reports |88| logs, events | logs, events, audit |8990If unclear, run `--list` and ask the user which database.9192## Workflow93941. `--list` to show available databases.952. Match user intent to a db description.963. `--tables` / `--schema` to explore structure.974. Execute the query with an appropriate LIMIT.9899## Safety features100101- Read-only session (`readonly=True` connection — primary protection).102- Only SELECT / SHOW / EXPLAIN / WITH queries allowed; multi-statement queries rejected.103- 30-second statement timeout.104- Max 10,000 rows per query (OOM guard); 100-char column-width cap for output.105- Error messages sanitize credentials.106- SSL mode configurable.107108## Troubleshooting109110| Error | Fix |111|---|---|112| Config not found | Create `connections.json` in skill dir |113| Authentication failed | Check user/password in config |114| Connection timeout | Verify host/port; check firewall/VPN |115| SSL error | Try `"sslmode": "disable"` for local dbs |116| Permission warning | `chmod 600 connections.json` |117118Exit codes: 0 success, 1 error (config missing, auth fail, invalid query, db error).