jpskill.com
🛠️ 開発・MCP コミュニティ

psql

Run PostgreSQL queries and meta-commands via CLI

⚡ おすすめ: コマンド1行でインストール(60秒)

下記のコマンドをコピーしてターミナル(Mac/Linux)または PowerShell(Windows)に貼り付けてください。 ダウンロード → 解凍 → 配置まで全自動。

🍎 Mac / 🐧 Linux
mkdir -p ~/.claude/skills && cd ~/.claude/skills && curl -L -o psql.zip https://jpskill.com/download/17538.zip && unzip -o psql.zip && rm psql.zip
🪟 Windows (PowerShell)
$d = "$env:USERPROFILE\.claude\skills"; ni -Force -ItemType Directory $d | Out-Null; iwr https://jpskill.com/download/17538.zip -OutFile "$d\psql.zip"; Expand-Archive "$d\psql.zip" -DestinationPath $d -Force; ri "$d\psql.zip"

完了後、Claude Code を再起動 → 普通に「動画プロンプト作って」のように話しかけるだけで自動発動します。

💾 手動でダウンロードしたい(コマンドが難しい人向け)
  1. 1. 下の青いボタンを押して psql.zip をダウンロード
  2. 2. ZIPファイルをダブルクリックで解凍 → psql フォルダができる
  3. 3. そのフォルダを C:\Users\あなたの名前\.claude\skills\(Win)または ~/.claude/skills/(Mac)へ移動
  4. 4. Claude Code を再起動

⚠️ ダウンロード・利用は自己責任でお願いします。当サイトは内容・動作・安全性について責任を負いません。

🎯 このSkillでできること

下記の説明文を読むと、このSkillがあなたに何をしてくれるかが分かります。Claudeにこの分野の依頼をすると、自動で発動します。

📦 インストール方法 (3ステップ)

  1. 1. 上の「ダウンロード」ボタンを押して .skill ファイルを取得
  2. 2. ファイル名の拡張子を .skill から .zip に変えて展開(macは自動展開可)
  3. 3. 展開してできたフォルダを、ホームフォルダの .claude/skills/ に置く
    • · macOS / Linux: ~/.claude/skills/
    • · Windows: %USERPROFILE%\.claude\skills\

Claude Code を再起動すれば完了。「このSkillを使って…」と話しかけなくても、関連する依頼で自動的に呼び出されます。

詳しい使い方ガイドを見る →
最終更新
2026-05-18
取得日時
2026-05-18
同梱ファイル
1

📖 Skill本文(日本語訳)

※ 原文(英語/中国語)を Gemini で日本語化したものです。Claude 自身は原文を読みます。誤訳がある場合は原文をご確認ください。

概要

PostgreSQLデータベースに対してSQLクエリとpsqlメタコマンドを実行するためのCLIツールです。各クエリは、永続的な接続を必要とせずに、psql CLIを介して直接実行されます。

前提条件

  • bun ランタイムがインストールされていること
  • psql クライアントがインストールされていること
  • PostgreSQL接続環境変数が<git-root>/.envに設定されているか、エクスポートされていること

環境変数

これらの変数を.envファイルに設定するか、エクスポートしてください。

変数 必須 デフォルト 説明
PGHOST Yes - データベースホスト
PGPORT No 5432 データベースポート
PGDATABASE Yes - データベース名
PGUSER Yes - データベースユーザー
PGPASSWORD Yes - データベースパスワード
PGSSLMODE No - SSLモード (disable, require, etc.)

.envの例:

PGHOST=localhost
PGPORT=5432
PGDATABASE=myapp
PGUSER=myapp
PGPASSWORD=secret
PGSSLMODE=disable

コマンド

Query

SQLクエリ、メタコマンド、またはSQLファイルを実行します。

bun .opencode/skill/psql/query.js <query> [options]
bun .opencode/skill/psql/query.js --file <path> [options]

引数:

  • query - 実行するSQLクエリまたはメタコマンド

オプション:

  • --file <path> - インラインクエリの代わりにSQLファイルを実行
  • --tuples - タプルのみの出力 (ヘッダーまたは行数なし)
  • --timeout <ms> - クエリのタイムアウト(ミリ秒単位)(デフォルト: 30000)
  • --json - 出力をJSONでラップ
  • --help - ヘルプを表示

例:

# SQLクエリ
bun .opencode/skill/psql/query.js "SELECT * FROM users LIMIT 5;"
bun .opencode/skill/psql/query.js "SELECT COUNT(*) FROM orders WHERE status = 'pending';"

# メタコマンド
bun .opencode/skill/psql/query.js "\dt"
bun .opencode/skill/psql/query.js "\d users"
bun .opencode/skill/psql/query.js "\di"
bun .opencode/skill/psql/query.js "\l"

# SQLファイルを実行
bun .opencode/skill/psql/query.js --file migrations/001_create_users.sql
bun .opencode/skill/psql/query.js --file scripts/seed_data.sql

# タプルのみ (スクリプト/解析用)
bun .opencode/skill/psql/query.js "SELECT id FROM users;" --tuples

# 低速なクエリのためにタイムアウトを長くする
bun .opencode/skill/psql/query.js "SELECT * FROM large_table;" --timeout 60000

一般的なワークフロー

データベーススキーマの探索

# すべてのテーブルをリスト
bun .opencode/skill/psql/query.js "\dt"

# 特定のテーブルを記述
bun .opencode/skill/psql/query.js "\d users"

# インデックスを表示
bun .opencode/skill/psql/query.js "\di"

# テーブルの外部キーを表示
bun .opencode/skill/psql/query.js "\d+ orders"

分析クエリの実行

# レコード数をカウント
bun .opencode/skill/psql/query.js "SELECT COUNT(*) FROM orders;"

# グループごとの集計
bun .opencode/skill/psql/query.js "SELECT status, COUNT(*) FROM orders GROUP BY status;"

# 最近のアクティビティ
bun .opencode/skill/psql/query.js "SELECT * FROM orders WHERE created_at > NOW() - INTERVAL '1 day' ORDER BY created_at DESC LIMIT 10;"

データベース管理

# テーブルサイズを確認
bun .opencode/skill/psql/query.js "SELECT relname, pg_size_pretty(pg_total_relation_size(relid)) FROM pg_catalog.pg_statio_user_tables ORDER BY pg_total_relation_size(relid) DESC LIMIT 10;"

# アクティブな接続を確認
bun .opencode/skill/psql/query.js "SELECT count(*) FROM pg_stat_activity WHERE state = 'active';"

# データベースをリスト
bun .opencode/skill/psql/query.js "\l"

マイグレーションの実行

# マイグレーションファイルを実行
bun .opencode/skill/psql/query.js --file migrations/001_create_users.sql

# シードデータを実行
bun .opencode/skill/psql/query.js --file scripts/seed.sql

出力動作

  • クエリの出力は、ターミナルでユーザーに直接表示されます
  • クエリの出力を再集計または再フォーマットしないでください - ユーザーはすでにそれを見ることができます
  • ヘッダーなしのクリーンな出力には--tuplesを使用します (他のツールへのパイプ処理に便利です)
  • プログラムで解析する場合は、構造化された出力に--jsonを使用します

注記

  • 各クエリは、個別のpsql呼び出しとして実行されます (永続的な接続はありません)
  • メタコマンド (\で始まる) は、SQLクエリと同じように機能します
  • 実行時間の長いクエリでは、--timeoutをデフォルトの30秒から増やす必要がある場合があります
  • --tuplesフラグは、出力を解析したり、他のコマンドにパイプしたりする必要がある場合に役立ちます
📜 原文 SKILL.md(Claudeが読む英語/中国語)を展開

Overview

CLI tool for running SQL queries and psql meta-commands against PostgreSQL databases. Each query is executed directly via the psql CLI - no persistent connection required.

Prerequisites

  • bun runtime installed
  • psql client installed
  • PostgreSQL connection environment variables set in <git-root>/.env or exported

Environment Variables

Set these in your .env file or export them:

Variable Required Default Description
PGHOST Yes - Database host
PGPORT No 5432 Database port
PGDATABASE Yes - Database name
PGUSER Yes - Database user
PGPASSWORD Yes - Database password
PGSSLMODE No - SSL mode (disable, require, etc.)

Example .env:

PGHOST=localhost
PGPORT=5432
PGDATABASE=myapp
PGUSER=myapp
PGPASSWORD=secret
PGSSLMODE=disable

Command

Query

Run a SQL query, meta-command, or SQL file.

bun .opencode/skill/psql/query.js <query> [options]
bun .opencode/skill/psql/query.js --file <path> [options]

Arguments:

  • query - SQL query or meta-command to execute

Options:

  • --file <path> - Execute SQL file instead of inline query
  • --tuples - Tuples only output (no headers or row count)
  • --timeout <ms> - Query timeout in milliseconds (default: 30000)
  • --json - Wrap output in JSON
  • --help - Show help

Examples:

# SQL queries
bun .opencode/skill/psql/query.js "SELECT * FROM users LIMIT 5;"
bun .opencode/skill/psql/query.js "SELECT COUNT(*) FROM orders WHERE status = 'pending';"

# Meta-commands
bun .opencode/skill/psql/query.js "\dt"
bun .opencode/skill/psql/query.js "\d users"
bun .opencode/skill/psql/query.js "\di"
bun .opencode/skill/psql/query.js "\l"

# Execute SQL file
bun .opencode/skill/psql/query.js --file migrations/001_create_users.sql
bun .opencode/skill/psql/query.js --file scripts/seed_data.sql

# Tuples only (for scripting/parsing)
bun .opencode/skill/psql/query.js "SELECT id FROM users;" --tuples

# With longer timeout for slow queries
bun .opencode/skill/psql/query.js "SELECT * FROM large_table;" --timeout 60000

Common Workflows

Explore Database Schema

# List all tables
bun .opencode/skill/psql/query.js "\dt"

# Describe a specific table
bun .opencode/skill/psql/query.js "\d users"

# Show indexes
bun .opencode/skill/psql/query.js "\di"

# Show foreign keys for a table
bun .opencode/skill/psql/query.js "\d+ orders"

Run Analytical Queries

# Count records
bun .opencode/skill/psql/query.js "SELECT COUNT(*) FROM orders;"

# Group by aggregation
bun .opencode/skill/psql/query.js "SELECT status, COUNT(*) FROM orders GROUP BY status;"

# Recent activity
bun .opencode/skill/psql/query.js "SELECT * FROM orders WHERE created_at > NOW() - INTERVAL '1 day' ORDER BY created_at DESC LIMIT 10;"

Database Administration

# Check table sizes
bun .opencode/skill/psql/query.js "SELECT relname, pg_size_pretty(pg_total_relation_size(relid)) FROM pg_catalog.pg_statio_user_tables ORDER BY pg_total_relation_size(relid) DESC LIMIT 10;"

# Check active connections
bun .opencode/skill/psql/query.js "SELECT count(*) FROM pg_stat_activity WHERE state = 'active';"

# List databases
bun .opencode/skill/psql/query.js "\l"

Run Migrations

# Execute a migration file
bun .opencode/skill/psql/query.js --file migrations/001_create_users.sql

# Execute seed data
bun .opencode/skill/psql/query.js --file scripts/seed.sql

Output Behavior

  • Query output is displayed directly to the user in the terminal
  • Do not re-summarize or reformat query output - the user can already see it
  • Use --tuples for clean output without headers (useful for piping to other tools)
  • Use --json for structured output when parsing programmatically

Notes

  • Each query is executed as a separate psql invocation (no persistent connection)
  • Meta-commands (starting with \) work the same as SQL queries
  • Long-running queries may need --timeout increased from the default 30 seconds
  • The --tuples flag is useful when you need to parse output or pipe to other commands