PostgreSQL Read-Only Query Skill
When to Use
- Use when querying PostgreSQL databases and access must stay strictly read-only
- Use when exploring schemas, tables, and data across multiple configured connections
- Use when you want defense-in-depth protection against accidental INSERT/UPDATE/DELETE or DDL
Execute safe, read-only queries against configured PostgreSQL databases.
Requirements
- Python 3.8+
- psycopg2-binary:
pip install -r requirements.txt
Setup
Create connections.json in the skill directory or ~/.config/claude/postgres-connections.json.
Security: Set file permissions to 600 since it contains credentials:
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
| Field |
Required |
Description |
| name |
Yes |
Identifier for the database (case-insensitive) |
| description |
Yes |
What data this database contains (used for auto-selection) |
| host |
Yes |
Database hostname |
| port |
No |
Port number (default: 5432) |
| database |
Yes |
Database name |
| user |
Yes |
Username |
| password |
Yes |
Password |
| sslmode |
No |
SSL mode: disable, allow, prefer (default), require, verify-ca, verify-full |
Usage
List configured databases
python3 scripts/query.py --list
Query a database
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
Limit results
python3 scripts/query.py --db production --query "SELECT * FROM orders" --limit 100
Database Selection
Match user intent to database description:
| User asks about |
Look for 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 user which database.
Safety Features
- Read-only session: Connection uses PostgreSQL
readonly=True mode (primary protection)
- Query validation: Only SELECT, SHOW, EXPLAIN, WITH queries allowed
- Single statement: Multiple statements per query rejected
- SSL support: Configurable SSL mode for encrypted connections
- Query timeout: 30-second statement timeout enforced
- Memory protection: Max 10,000 rows per query to prevent OOM
- Column width cap: 100 char max per column for readable output
- Credential sanitization: Error messages don't leak passwords
Troubleshooting
| Error |
Solution |
| Config not found |
Create connections.json in skill directory |
| Authentication failed |
Check username/password in config |
| Connection timeout |
Verify host/port, check firewall/VPN |
| SSL error |
Try "sslmode": "disable" for local databases |
| Permission warning |
Run chmod 600 connections.json |
Exit Codes
- 0: Success
- 1: Error (config missing, auth failed, invalid query, database error)
Workflow
- Run
--list to show available databases
- Match user intent to database description
- Run
--tables or --schema to explore structure
- Execute query with appropriate LIMIT
Limitations
- Read-only protections reduce accidental writes but cannot override database-server policy,
triggers, extensions, or an over-privileged account. Use a database role with read-only
permissions as the primary control.
- Query results can contain personal, confidential, or regulated data. Confirm the intended
database and avoid exporting or sharing results without explicit authorization.
- The script is not a replacement for backups, auditing, access reviews, or production change
controls.
1---2name: postgres-readonly-queries3description: "Execute safe read-only SQL queries against PostgreSQL databases with multi-connection support and defense-in-depth write protection."4category: data5risk: safe6source: https://github.com/sanjay3290/ai-skills/tree/main/skills/postgres7source_repo: sanjay3290/ai-skills8source_type: community9date_added: "2026-07-09"10author: sanjay329011tags: [postgres, sql, database, read-only]12tools: [claude, cursor, gemini]13license: "Apache-2.0"14license_source: "https://github.com/sanjay3290/ai-skills/blob/main/LICENSE"15metadata:16 source: https://skillmd.com/skills/sanjay3290/postgres-readonly-queries17---1819# PostgreSQL Read-Only Query Skill2021## When to Use2223- Use when querying PostgreSQL databases and access must stay strictly read-only24- Use when exploring schemas, tables, and data across multiple configured connections25- Use when you want defense-in-depth protection against accidental INSERT/UPDATE/DELETE or DDL2627Execute safe, read-only queries against configured PostgreSQL databases.2829## Requirements3031- Python 3.8+32- psycopg2-binary: `pip install -r requirements.txt`3334## Setup3536Create `connections.json` in the skill directory or `~/.config/claude/postgres-connections.json`.3738**Security**: Set file permissions to `600` since it contains credentials:39```bash40chmod 600 connections.json41```4243```json44{45 "databases": [46 {47 "name": "production",48 "description": "Main app database - users, orders, transactions",49 "host": "db.example.com",50 "port": 5432,51 "database": "app_prod",52 "user": "readonly_user",53 "password": "your-password",54 "sslmode": "require"55 }56 ]57}58```5960### Config Fields6162| Field | Required | Description |63|-------|----------|-------------|64| name | Yes | Identifier for the database (case-insensitive) |65| description | Yes | What data this database contains (used for auto-selection) |66| host | Yes | Database hostname |67| port | No | Port number (default: 5432) |68| database | Yes | Database name |69| user | Yes | Username |70| password | Yes | Password |71| sslmode | No | SSL mode: disable, allow, prefer (default), require, verify-ca, verify-full |7273## Usage7475### List configured databases76```bash77python3 scripts/query.py --list78```7980### Query a database81```bash82python3 scripts/query.py --db production --query "SELECT * FROM users LIMIT 10"83```8485### List tables86```bash87python3 scripts/query.py --db production --tables88```8990### Show schema91```bash92python3 scripts/query.py --db production --schema93```9495### Limit results96```bash97python3 scripts/query.py --db production --query "SELECT * FROM orders" --limit 10098```99100## Database Selection101102Match user intent to database `description`:103104| User asks about | Look for description containing |105|-----------------|--------------------------------|106| users, accounts | users, accounts, customers |107| orders, sales | orders, transactions, sales |108| analytics, metrics | analytics, metrics, reports |109| logs, events | logs, events, audit |110111If unclear, run `--list` and ask user which database.112113## Safety Features114115- **Read-only session**: Connection uses PostgreSQL `readonly=True` mode (primary protection)116- **Query validation**: Only SELECT, SHOW, EXPLAIN, WITH queries allowed117- **Single statement**: Multiple statements per query rejected118- **SSL support**: Configurable SSL mode for encrypted connections119- **Query timeout**: 30-second statement timeout enforced120- **Memory protection**: Max 10,000 rows per query to prevent OOM121- **Column width cap**: 100 char max per column for readable output122- **Credential sanitization**: Error messages don't leak passwords123124## Troubleshooting125126| Error | Solution |127|-------|----------|128| Config not found | Create `connections.json` in skill directory |129| Authentication failed | Check username/password in config |130| Connection timeout | Verify host/port, check firewall/VPN |131| SSL error | Try `"sslmode": "disable"` for local databases |132| Permission warning | Run `chmod 600 connections.json` |133134## Exit Codes135136- **0**: Success137- **1**: Error (config missing, auth failed, invalid query, database error)138139## Workflow1401411. Run `--list` to show available databases1422. Match user intent to database description1433. Run `--tables` or `--schema` to explore structure1444. Execute query with appropriate LIMIT145146## Limitations147148- Read-only protections reduce accidental writes but cannot override database-server policy,149 triggers, extensions, or an over-privileged account. Use a database role with read-only150 permissions as the primary control.151- Query results can contain personal, confidential, or regulated data. Confirm the intended152 database and avoid exporting or sharing results without explicit authorization.153- The script is not a replacement for backups, auditing, access reviews, or production change154 controls.