← 返回 Skills 市场
tengshengbo

postgresql-skill

作者 tengshengbo · GitHub ↗ · v1.0.1 · MIT-0
cross-platform ⚠ suspicious
42
总下载
1
收藏
0
当前安装
2
版本数
在 OpenClaw 中安装
/install postgresql-skill
功能描述
Execute PostgreSQL database operations using psycopg2. List tables, describe schema, execute SQL queries.
使用说明 (SKILL.md)

PostgreSQL Database Operations

Pure Python PostgreSQL skill using psycopg2 (no psql client needed).

When to Use

Use this skill when the user needs to:

  • List database tables: "What tables are in the database?", "show tables"
  • View table structure: "What columns does the users table have?", "describe users"
  • Execute SQL queries: "Get all users", "SELECT * FROM users"
  • Get schema summary: "What's the database structure?", "schema overview"
  • Modify data: Insert, update, or delete records

When NOT to Use

  • User asks conceptual questions (e.g., "What is PostgreSQL?")
  • User needs data analysis or business insights (requires LLM reasoning)
  • User wants natural language to SQL conversion (should be handled by upper-layer Agent)
  • Database is not PostgreSQL

Usage Guidelines for AI Agents

Step 1: Check Prerequisites

Before using this skill, ensure:

  1. Python 3 is installed
  2. Dependencies are installed: pip install psycopg2-binary pyyaml
  3. config.yaml exists with valid database connection details

If config.yaml is missing, instruct the user to:

cp config.example.yaml config.yaml
# Then edit config.yaml with real database credentials

Step 2: Choose the Right Command

User Intent Command Example
List tables python scripts/pgsql_skill.py list-tables Get all table names
Describe table python scripts/pgsql_skill.py describe-table \x3Ctable> describe-table users
Query data python scripts/pgsql_skill.py execute-sql "\x3CSQL>" execute-sql "SELECT * FROM users LIMIT 10"
Schema overview python scripts/pgsql_skill.py schema-summary Full database structure

Step 3: Parse Results

All commands return JSON (except schema-summary):

list-tables output:

{"tables": ["users", "orders", "products"]}

describe-table output:

{
  "table": "users",
  "columns": [
    {"name": "id", "type": "integer", "nullable": false, "default": null},
    {"name": "username", "type": "varchar", "nullable": false, "default": null}
  ]
}

execute-sql SELECT output:

{
  "columns": ["id", "username", "email"],
  "rows": [
    {"values": ["1", "alice", "[email protected]"]},
    {"values": ["2", "bob", "[email protected]"]}
  ]
}

execute-sql INSERT/UPDATE/DELETE output:

{"affected_rows": 1}

Step 4: Handle Errors

If a command fails, check the error message:

  • "error": "config.yaml not found" → Guide user to create config
  • "error": "psycopg2 not installed" → Run pip install psycopg2-binary
  • "error": "connection failed" → Verify database credentials
  • "error": "Forbidden operation" → SQL violates safety rules

Safety Rules

This skill enforces strict SQL safety:

Allowed:

  • SELECT queries
  • INSERT (single row only)
  • UPDATE (must have WHERE clause)
  • DELETE (must have WHERE clause)

Blocked:

  • DROP TABLE/DATABASE
  • TRUNCATE TABLE
  • ALTER TABLE
  • Batch INSERT (multiple rows)
  • UPDATE/DELETE without WHERE

Example of blocked query:

python scripts/pgsql_skill.py execute-sql "DROP TABLE users"
# Output: {"error": "Forbidden operation: DROP"}

Programmatic Integration

For advanced usage, import directly in Python:

import sys
from pathlib import Path
sys.path.insert(0, 'scripts')

from pgsql_skill import Database, load_config

# Initialize database connection
config = load_config()
db = Database(
    host=config['host'],
    port=config['port'],
    dbname=config['dbname'],
    user=config['user'],
    password=config.get('password', '')
)

# Use database methods
tables = db.list_tables()
structure = db.describe_table("users")
result = db.execute_sql("SELECT count(*) FROM users")

# Always close connection
db.close()

Troubleshooting

  • ModuleNotFoundError: psycopg2: Run pip install psycopg2-binary
    • On macOS with managed Python: pip install psycopg2-binary --no-binary :all:
  • config.yaml not found: Copy from config.example.yaml
  • Connection failed: Verify host/port/user/password in config.yaml
  • Permission denied: Check database user permissions

Best Practices

  1. Add LIMIT to SELECT queries to avoid large result sets
  2. Always check errors before proceeding
  3. Close connections when using programmatic API
  4. Warn users before data modifications (INSERT/UPDATE/DELETE) and confirm intent
安全使用建议
Install only if you are comfortable giving the skill database credentials and allowing it to run SQL against that database. Use a least-privilege, read-only database account where possible, avoid production or sensitive databases, and be especially cautious with schema-summary because it can return real sample records from every table, not just column metadata.
能力评估
Purpose & Capability
The core purpose is coherent for a PostgreSQL skill: it connects using user-provided DB settings, lists tables, describes tables, executes SQL, and supports limited INSERT/UPDATE/DELETE. The concern is that schema-summary performs database-wide row counts and reads up to three full rows from every public table, while user-facing docs describe it mainly as a schema or structure overview.
Instruction Scope
The skill documents SQL execution and advises warning users before data modifications, but it does not clearly require confirmation, table scoping, or sensitive-column redaction before schema-summary samples live data from all tables.
Install Mechanism
Installation is limited to declared Python dependencies psycopg2-binary and pyyaml, with python3, config.yaml, and DB_* environment variables declared in the OpenClaw metadata.
Credentials
Database credentials and broad database read/write authority are expected for this kind of skill, but automatic all-table sample extraction is broader than a typical schema-inspection request and could expose PII, tokens, or business records.
Persistence & Privilege
No background worker, persistence mechanism, privilege escalation, or external exfiltration path was found. The skill does rely on user-provided database credentials in config.yaml or environment variables.
如何使用
  1. 确保已安装 OpenClaw(本地或 Docker 部署)
  2. 在对话框中输入安装命令:/install postgresql-skill
  3. 安装完成后,直接呼叫该 Skill 的名称或使用 /postgresql-skill 触发
  4. 根据 Skill 的参数说明提供必要输入,即可获得结构化输出
版本历史
v1.0.1
- Added example config files: `config.example.yaml` - Improved documentation to explain config file setup and usage - Added environment variable support for database connection details in SKILL.md
v1.0.0
- Initial release of the PostgreSQL skill for database operations via psycopg2, without requiring the psql client. - Supports listing tables, describing table schema, executing SQL queries (with enforced safety rules), and retrieving a full schema overview. - Outputs results as JSON for easy parsing and integration. - Requires Python 3, psycopg2-binary, pyyaml, and a configured config.yaml for database credentials. - Provides both CLI usage instructions and examples for programmatic integration in Python scripts. - Built-in SQL safety: blocks destructive or dangerous commands, and enforces WHERE clause for UPDATE/DELETE.
元数据
Slug postgresql-skill
版本 1.0.1
许可证 MIT-0
累计安装 0
当前安装数 0
历史版本数 2
常见问题

postgresql-skill 是什么?

Execute PostgreSQL database operations using psycopg2. List tables, describe schema, execute SQL queries. 它是一个面向 Claude Code / OpenClaw 的 AI Agent Skill 插件,目前累计下载 42 次。

如何安装 postgresql-skill?

在 OpenClaw 或 Claude Code 对话框中运行命令「/install postgresql-skill」即可一键安装,无需额外配置。

postgresql-skill 是免费的吗?

是的,postgresql-skill 完全免费,采用 MIT-0 许可证,可自由下载、安装和使用。

postgresql-skill 支持哪些平台?

postgresql-skill 跨平台运行,可在任意部署了 OpenClaw / Claude Code 的环境中使用(cross-platform)。

谁开发了 postgresql-skill?

由 tengshengbo(@tengshengbo)开发并维护,当前版本 v1.0.1。

💬 留言讨论