postgres
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChinesePostgreSQL Read-Only Query Skill
PostgreSQL只读查询工具
Execute safe, read-only queries against configured PostgreSQL databases.
对已配置的PostgreSQL数据库执行安全的只读查询。
Requirements
依赖要求
- Python 3.8+
- psycopg2-binary:
pip install -r requirements.txt
- Python 3.8+
- psycopg2-binary:
pip install -r requirements.txt
Setup
配置步骤
Create in the skill directory or .
connections.json~/.config/claude/postgres-connections.jsonSecurity: Set file permissions to since it contains credentials:
600bash
chmod 600 connections.jsonjson
{
"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"
}
]
}在工具目录下创建文件,也可以放置在路径。
connections.json~/.config/claude/postgres-connections.json安全提示:由于该文件包含凭证信息,请将文件权限设置为:
600bash
chmod 600 connections.jsonjson
{
"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 |
| 字段 | 是否必填 | 描述 |
|---|---|---|
| name | 是 | 数据库的标识符(不区分大小写) |
| description | 是 | 该数据库包含的数据内容(用于自动选择) |
| host | 是 | 数据库主机地址 |
| port | 否 | 端口号(默认:5432) |
| database | 是 | 数据库名称 |
| user | 是 | 用户名 |
| password | 是 | 密码 |
| sslmode | 否 | SSL模式:disable、allow、prefer(默认)、require、verify-ca、verify-full |
Usage
使用说明
List configured databases
列出已配置的数据库
bash
python3 scripts/query.py --listbash
python3 scripts/query.py --listQuery a database
查询指定数据库
bash
python3 scripts/query.py --db production --query "SELECT * FROM users LIMIT 10"bash
python3 scripts/query.py --db production --query "SELECT * FROM users LIMIT 10"List tables
列出所有数据表
bash
python3 scripts/query.py --db production --tablesbash
python3 scripts/query.py --db production --tablesShow schema
查看表结构
bash
python3 scripts/query.py --db production --schemabash
python3 scripts/query.py --db production --schemaLimit results
限制返回结果数量
bash
python3 scripts/query.py --db production --query "SELECT * FROM orders" --limit 100bash
python3 scripts/query.py --db production --query "SELECT * FROM orders" --limit 100Database 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 and ask user which database.
--list将用户意图与数据库的字段匹配:
description| 用户查询内容 | 查找包含以下关键词的描述 |
|---|---|
| 用户、账号 | users、accounts、customers |
| 订单、销售 | orders、transactions、sales |
| 分析、指标 | analytics、metrics、reports |
| 日志、事件 | logs、events、audit |
如果无法明确匹配,运行命令询问用户需要使用哪个数据库。
--listSafety Features
安全特性
- Read-only session: Connection uses PostgreSQL mode (primary protection)
readonly=True - 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
- 只读会话:连接使用PostgreSQL 模式(核心防护机制)
readonly=True - 查询校验:仅允许SELECT、SHOW、EXPLAIN、WITH类型的查询
- 单语句限制:拒绝单次请求包含多条语句的查询
- SSL支持:可配置SSL模式实现加密连接
- 查询超时:强制设置30秒的语句超时时间
- 内存防护:单次查询最多返回10000行数据,避免内存溢出
- 列宽限制:单列最多显示100个字符,保证输出可读性
- 凭证脱敏:错误信息不会泄露密码内容
Troubleshooting
故障排查
| Error | Solution |
|---|---|
| Config not found | Create |
| Authentication failed | Check username/password in config |
| Connection timeout | Verify host/port, check firewall/VPN |
| SSL error | Try |
| Permission warning | Run |
| 错误信息 | 解决方案 |
|---|---|
| Config not found | 在工具目录下创建 |
| Authentication failed | 检查配置中的用户名和密码 |
| Connection timeout | 验证主机/端口配置,检查防火墙/VPN设置 |
| SSL error | 本地数据库可尝试设置 |
| Permission warning | 执行 |
Exit Codes
退出码
- 0: Success
- 1: Error (config missing, auth failed, invalid query, database error)
- 0:执行成功
- 1:发生错误(配置缺失、认证失败、查询无效、数据库错误)
Workflow
工作流程
- Run to show available databases
--list - Match user intent to database description
- Run or
--tablesto explore structure--schema - Execute query with appropriate LIMIT
- 运行命令查看可用数据库
--list - 将用户意图与数据库描述进行匹配
- 运行或
--tables命令探索数据库结构--schema - 执行带有合适LIMIT限制的查询