Get the FREE Ultimate OpenClaw Setup Guide →

postgres

Use Caution
npx machina-cli add skill dversoza/claude-skills/postgres --openclaw
Files (1)
SKILL.md
3.5 KB

Postgres

Read-only access to PostgreSQL databases via psql. All connections enforce default_transaction_read_only=on at the server level -- writes and DDL are rejected by PostgreSQL itself.

Connection Setup

The script resolves database aliases from a ```pg-databases``` fenced code block in the project's CLAUDE.local.md. It searches from the current working directory upward. Credentials never appear on the command line -- only the alias is passed as an argument.

To discover available aliases:

python3 ~/.claude/skills/postgres/scripts/pg_query.py list

If no CLAUDE.local.md exists or it has no pg-databases block, ask the user for connection details and suggest they add a block like this to their project's CLAUDE.local.md:

```pg-databases
staging=postgres://user:pass@staging-host:5432/myapp
local=postgres://localhost:5432/myapp_dev
analytics=postgres://analyst:pass@analytics-host:5432/warehouse
```

Remind the user that CLAUDE.local.md should be in .gitignore to keep credentials out of version control.

Do NOT pass connection URIs on the command line. Always use the alias.

Subcommands

All subcommands take a database alias as the first argument.

Introspection

python3 ~/.claude/skills/postgres/scripts/pg_query.py schemas staging                    # list non-system schemas
python3 ~/.claude/skills/postgres/scripts/pg_query.py tables staging                     # list tables in public schema
python3 ~/.claude/skills/postgres/scripts/pg_query.py tables staging --schema analytics  # list tables in a specific schema
python3 ~/.claude/skills/postgres/scripts/pg_query.py describe staging users             # columns, types, nullability
python3 ~/.claude/skills/postgres/scripts/pg_query.py describe staging analytics.events  # schema-qualified table
python3 ~/.claude/skills/postgres/scripts/pg_query.py indexes staging users              # indexes on a table

Queries

python3 ~/.claude/skills/postgres/scripts/pg_query.py query staging "SELECT id, name FROM users LIMIT 10"
python3 ~/.claude/skills/postgres/scripts/pg_query.py explain staging "SELECT id, name FROM users WHERE active = true"

All subcommands return JSON to stdout. Errors go to stderr as JSON with an error field.

Query Guidelines

  • Always add LIMIT to queries unless the user explicitly asks for all rows or the query already includes one. Default to LIMIT 100.
  • Prefer specific columns over SELECT * when the table structure is known (use describe first if needed).
  • For large result sets, suggest the user narrow the query rather than removing the limit.
  • Writes are impossible: the connection is read-only at the PostgreSQL session level. If a write is attempted, psql returns ERROR: cannot execute X in a read-only transaction. Report this to the user and explain the read-only constraint.
  • When exploring an unfamiliar database, start with schemas then tables then describe before running queries.

Source

git clone https://github.com/dversoza/claude-skills/blob/main/postgres/SKILL.mdView on GitHub

Overview

This skill provides safe, read-only access to PostgreSQL databases for querying data, inspecting schemas, listing tables, and explaining query plans. It enforces a read-only session to prevent writes or DDL, ensuring safe data exploration. Connection details are managed via aliases defined in CLAUDE.local.md.

How This Skill Works

The tool resolves database aliases from a CLAUDE.local.md block and uses a dedicated Python script to issue introspection and query commands. Credentials never appear on the command line; only the alias is passed. Outputs are returned as JSON, with errors emitted to stderr as a JSON object.

When to Use It

  • Query a table or specific columns (e.g., SELECT id, name FROM users) in a staging or production database
  • Describe a table or schema to understand columns, types, and nullability
  • List available schemas or tables to explore a new database
  • Show indexes on a table to understand performance characteristics
  • Explain a query plan to analyze how PostgreSQL will execute a query

Quick Start

  1. Step 1: Create or update CLAUDE.local.md with a pg-databases block that defines aliases (e.g., staging, local, analytics) and their connection URIs
  2. Step 2: Run a subcommand, for example: python3 ~/.claude/skills/postgres/scripts/pg_query.py tables staging
  3. Step 3: Review the JSON output; if you attempt a write, you will receive a read-only error and guidance on constraints

Best Practices

  • Always add LIMIT to queries unless explicitly requested otherwise; default to LIMIT 100
  • Prefer specific columns over SELECT * when the table structure is known
  • Start with schemas, then tables, then describe a table when exploring an unfamiliar database
  • If a query is large, suggest narrowing the result instead of removing the limit
  • Keep CLAUDE.local.md in .gitignore and never pass credentials on the command line

Example Use Cases

  • python3 ~/.claude/skills/postgres/scripts/pg_query.py query staging "SELECT id, name FROM users LIMIT 10"
  • python3 ~/.claude/skills/postgres/scripts/pg_query.py explain staging "SELECT id, name FROM users WHERE active = true"
  • python3 ~/.claude/skills/postgres/scripts/pg_query.py schemas staging
  • python3 ~/.claude/skills/postgres/scripts/pg_query.py tables staging
  • python3 ~/.claude/skills/postgres/scripts/pg_query.py describe staging users

Frequently Asked Questions

Add this skill to your agents
Sponsor this space

Reach thousands of developers