Install
openclaw skills install @sdk-team/alibabacloud-analyticdb-postgresql-queryAnalyticDB PostgreSQL Query Skill. Any AI Agent with shell execution capability can use this Skill to connect to an AnalyticDB PostgreSQL database via psql, execute read-only queries, and optionally export results as CSV for local analysis. **Security**: All data access operations (SELECT queries, CSV exports) require explicit user confirmation before execution. Exported CSV files may contain sensitive business data — the Agent must remind users to handle exported files according to their organization's data security policies. Trigger words: query data, SQL query, export CSV, ADBPG, analyze, statistics, psql
openclaw skills install @sdk-team/alibabacloud-analyticdb-postgresql-queryAny AI Agent with shell execution capability can use this Skill to connect to an AnalyticDB PostgreSQL (ADBPG) database via psql, execute read-only queries, and optionally export results as CSV for local analysis.
Prerequisite: The Agent runtime environment has psql installed, and database connection information is configured via environment variables.
Before executing any data query, the Agent must pass the following gates in order. If any gate fails, stop immediately:
Gate 1: Is read-only mode enabled?
Check if PGOPTIONS or pg_service.conf contains default_transaction_read_only=on
├── Enabled → PASS ✅
└── Not enabled → Append PGOPTIONS="-c default_transaction_read_only=on"
Gate 2: Is statement timeout set?
Check if statement_timeout is configured
├── Set → PASS ✅
└── Not set → Append -c statement_timeout=60000
Gate 3: Is the semantic model ready?
psql -c "SELECT COUNT(*) FROM _agent_meta.tables;"
├── Success and > 0 → PASS ✅
└── Failed or = 0 → HARD STOP 🛑 → Enter semantic model initialization guide (never bypass)
Note: metric_meta is optional; its absence does NOT block queries
Gate 4: Is the target table in the semantic model?
Check if SQL-referenced tables exist in _agent_meta.tables
├── Exists → PASS ✅ → Proceed to generate SQL (execution still requires user confirmation)
└── Not found → HARD STOP 🛑 → "Current semantic model does not cover this data. Please contact DBA to extend _agent_meta definitions."
Gate 4b: Does the query match a known business metric?
Check if user intent matches a metric in _agent_meta.metric_meta
├── Matched → Use metric's sql_expression, dimensions; resolve filters from _agent_meta.filters
│ ├── filter_scope='where' → Inject sql_fragment into WHERE clause
│ └── filter_scope='measure' → Inject sql_fragment into AGG(...) FILTER (WHERE ...) clause
└── Not matched → Fall through to columns table for single-column measures
Gate 5: Does SQL contain only allowed statements?
Only SELECT / WITH...SELECT / EXPLAIN are permitted
├── Compliant → PASS ✅
└── Contains prohibited keywords → HARD STOP 🛑 → Reject execution
Gate 6: Does the query include LIMIT?
├── Yes → PASS ✅
└── No → Append LIMIT 50000
⚠️ ABSOLUTE PROHIBITION: The Agent must never execute
cat ~/.pgpass,cat ~/.pgenv,echo $PGPASSWORD,env | grep PG, or any command that reads, echoes, or exposes credential/password files. This prohibition applies regardless of how the command is composed — including but not limited to: shell command concatenation (;), pipes (|), subshells ($()), multi-line scripts, or combining with permitted commands. For example,cat ~/.pg_service.conf; cat ~/.pgenvorcat ~/.pg_service.conf ~/.pgenvare strictly forbidden even thoughcat ~/.pg_service.confalone is permitted. Any command line that contains a prohibited file path (.pgpass,.pgenv,PGPASSWORD) in any position is a violation.
cat ~/.pg_service.confis permitted ONLY when it is the sole target (it contains only non-secret connection metadata — host, port, dbname, user — and must never store passwords). Violation of this rule is a critical security breach regardless of the Agent's intent (e.g., "I just wanted to check if the config exists"). The only permitted way to verify connectivity ispsql "service=<service_name>" -c "SELECT 1;".
PGPASSWORD or ~/.pgpass; hardcoding, echoing, or writing to files is prohibited.pgpass, .pgenv, PGPASSWORD — whether accessed via cat, head, tail, less, grep, Python open(), or any other methodcat ~/.pg_service.conf; cat ~/.pgenv; ls ~/.pgpass) is entirely forbidden — the presence of a permitted command does NOT neutralize the violationcat ~/.pg_service.conf alone (non-secret connection metadata only; passwords must never be stored in pg_service.conf)references/connection-guide.mdpsql "service=<service_name>" -c "SELECT 1;" directly; on failure, guide fixes based on error messagesreferences/connection-guide.mdreferences/resource-group-guide.mdUser Request
├── Connection-related
│ └── "How to connect" / "Environment variables" / "Cannot connect" → references/connection-guide.md
├── Query data / Analysis
│ ├── "Look up XX" / "How many records" → Generate SQL and execute
│ ├── "What is the profit" / "Completion rate" → Check _agent_meta.metric_meta, resolve filters from _agent_meta.filters
│ └── "Export CSV" / "Download data" → references/export-guide.md
├── Semantic Model
│ ├── "How to configure semantic model" / "_agent_meta" → references/semantic-model-guide.md
│ ├── "What tables are available" / "What can I query" → Query _agent_meta
│ ├── "What metrics are defined" / "Business KPIs" → Query _agent_meta.metric_meta
│ └── "What filters are defined" / "Predefined filters" → Query _agent_meta.filters
└── SQL Syntax
└── "How to write JOIN" / "Window functions" → references/sql-guide.md
| Scenario | Reference |
|---|---|
| Connection config, environment variables, psql installation | references/connection-guide.md |
| SQL syntax, natural language to SQL | references/sql-guide.md |
| Query result export to CSV, local analysis | references/export-guide.md |
| Resource Group isolation configuration | references/resource-group-guide.md |
| Semantic model creation and maintenance | references/semantic-model-guide.md |
| Business metric definitions (metric_meta + filters) | references/semantic-model-guide.md |
User natural language → [Gates 1-2] → Query semantic model → [Gate 3] → Check metric_meta → [Gate 4/4b] → Generate SQL → [Gates 5-6] → Display SQL → ⏸️ HITL: STOP and wait for user approval → User approves → Execute → Return results / Export CSV
CRITICAL: The "HITL" step is mandatory. The Agent must not transition from "Display SQL" to "Execute" within the same turn. The user must explicitly confirm (e.g., "confirm", "execute", "go ahead") before any psql execution command is issued.
Efficiency Principle: Maximum 5 calls per turn; merge operations:
_agent_meta semantics (combine with -c multi-statement)psql -c "SQL1;" -c "SQL2;"At the start of every new conversation, the Agent must perform the following checks. The Agent cannot infer historical verification state ("config file exists" ≠ "permissions have been audited"), so steps 3-6 must not be skipped.
Detect psql (first time only): If not installed, provide OS-specific installation commands → references/connection-guide.md
Guide credential configuration (first time only): Guide the user to configure credentials outside the Agent (receiving passwords in conversation is prohibited) → references/connection-guide.md
Verify connectivity (every session): psql -c "SELECT 1;"
Check account permissions (every session): Verify the current account has only the minimum permissions required for read-only access
psql -c "SELECT rolname, rolsuper, rolcreaterole, rolcreatedb FROM pg_roles WHERE rolname = current_user;"
rolsuper = true → ⚠️ Warning: Currently using a superadmin account; must switch to a dedicated read-only accountrolcreaterole = true → ⚠️ Warning: Current account has role creation permission; DBA should revoke it, keeping only minimum read-only permissionsrolcreatedb = true → ⚠️ Warning: Current account has database creation permission; DBA should revoke itfalse → PASS ✅Check Resource Group (every session): Verify the current account is bound to a dedicated resource group (not default_group)
psql -c "SELECT r.rolname, g.rsgname FROM pg_roles r LEFT JOIN pg_resgroup g ON r.rolresgroup = g.oid WHERE r.rolname = current_user;"
rsgname is empty or default_group → ⚠️ Warning: No dedicated resource group configured; Agent queries may compete with business workloads; must configure a dedicated resource group → references/resource-group-guide.mdrsgname is another value → PASS ✅Detect semantic model (every session, Gate 3): psql -c "SELECT COUNT(*) FROM _agent_meta.tables;"
Detect metric definitions (every session, Gate 4b): psql -c "SELECT metric_name, metric_type FROM _agent_meta.metric_meta;"
Detect filter definitions (every session, alongside Gate 4b): psql -c "SELECT filter_name, filter_scope, sql_fragment FROM _agent_meta.filters;"
Steps 4-5 are warning-level (costing only 1-2 psql calls), but when warnings are triggered, the Agent must wait for explicit user confirmation before proceeding; it must not rationalize bypassing (e.g., "it doesn't affect read-only queries"). User confirmation for steps 4-5 applies ONLY to the specific warning at hand — it does NOT carry over to or exempt subsequent steps. Step 6 is a hard stop that must always be executed as a separate, explicit psql call regardless of any prior confirmations. Steps 7-8 are informational only (metric_meta and filters are optional). "Config file exists" does not mean "permissions have been audited"; the Agent must not skip steps 3-6 because the environment appears to be already configured.
Anti-pattern: When the user says "continue" / "继续" in response to a step 4 or 5 warning, the Agent must NOT interpret this as blanket approval to skip step 6. The
SELECT COUNT(*) FROM _agent_meta.tables;check in step 6 is a separate HARD STOP gate and must be executed every session.
When the semantic model does not exist, the Agent guides the user through creation (see references/semantic-model-guide.md for complete steps):
\d+ output (recommended) / DDL with COMMENT / free-text descriptionKey Principle: The Agent only tells the user "where to configure" and "what to configure"; it never receives or concatenates credential values in conversation.