本文へスキップ
Skillpedia
一般的なプログラミング48k · #5471

postgresql-cli

できること

psqlは、ターミナルから直接PostgreSQLデータベースを操作できるツールです。コマンドを書いて実行したり、テーブルなどのデータベースのオブジェクトに関する情報を確認したりできます。

使う場面

  • ✓PostgreSQLデータベースでSQLコマンドを実行する必要があるとき。
  • ✓テーブル、ビュー、その他のデータベース情報の構造を確認したいとき。
  • ✓データベースのデータをインポートまたはエクスポートするとき。

スキルのリンク

https://github.com/sickn33/agentic-awesome-skills/blob/main/plugins/agentic-awesome-skills-claude/skills/postgresql-cli/SKILL.md

コピーしてAI(Claude、ChatGPT、Cursor)に貼り付け、指示に従うよう頼んでください。

使い方

いちばん簡単な方法:メッセージをコピーしてAIに貼り付けるだけ。AIがスキルを読んで指示に従います。

貼り付け用メッセージ

このスキルを使って手伝ってください:https://github.com/sickn33/agentic-awesome-skills/blob/main/plugins/agentic-awesome-skills-claude/skills/postgresql-cli/SKILL.md このリンクのSKILL.mdファイルを読んで、私の作業でその指示に従ってください。
こういうスキルを、AIが自分で見つけてくれたら?無料のSkillpedia MCPをインストール。1行で、作業の前に15,000+のスキルから探してくれます。1分でインストール →

AIへの頼み方の例

“ホスト'my_host'上のデータベース'my_database'にユーザー'my_user'として接続し、クエリ'SELECT * FROM customers;'を実行してください。”

毎週5つの新しいスキル。1分で読めます。

仕組み

ターミナルでコマンドを入力してデータベースに接続し、その後SQLコマンドを書きます。psqlがそれらのコマンドを実行し、結果を表示します。

技術的な内容(元のSKILL.md)

作者による元のテキスト(英語)をGitHubからそのまま表示しています。

psql — PostgreSQL Interactive Terminal

psql is PostgreSQL's feature-rich interactive terminal. It lets you write and execute queries, inspect database objects, import/export data, script batch operations, and customize output formatting — all from the command line.

Prerequisites

Before using psql, verify it is installed and available:

# Check if psql is installed
psql --version

# If not found, install PostgreSQL client tools:

# macOS (Homebrew)
brew install libpq
brew link --force libpq

# Ubuntu / Debian
sudo apt install postgresql-client

# CentOS / RHEL
sudo yum install postgresql

# Alpine
apk add postgresql-client

# Windows — install PostgreSQL via the official installer or use WSL

psql ships as part of the postgresql-client package. The server (postgresql) is not required — you only need the client to connect to a remote PostgreSQL instance.

Quick Reference

Connecting

# 1. CLI flags
psql -h host -p port -U user -d dbname

# 2. Connection URI
# WARNING: Password in URI is visible in shell history and process listings.
#          Prefer ~/.pgpass for production use (see method 4 below).
psql "postgresql://user:YOUR_PASSWORD@host:port/dbname"

# 3. Environment variables (no flags needed)
export PGHOST=localhost
export PGPORT=5432
export PGDATABASE=mydb
export PGUSER=postgres
# WARNING: PGPASSWORD is visible in process listings (e.g. `ps aux`).
#          Use ~/.pgpass in production instead.
export PGPASSWORD=YOUR_PASSWORD
psql                       # picks up all params from env

# 4. ~/.pgpass file (RECOMMENDED for passwords)
#    Format: hostname:port:database:username:password
touch ~/.pgpass && chmod 600 ~/.pgpass
# Then manually edit ~/.pgpass and add entries (avoids password in shell history):
# hostname:port:database:username:password
# Example: localhost:5432:mydb:postgres:YOUR_PASSWORD
psql -h localhost -U postgres -d mydb   # no password prompt

# 5. Execute and exit
psql -f script.sql dbname                        # execute file then exit
psql -c "SELECT 1" dbname                        # run single command then exit
psql -1 -f migration.sql dbname                  # run in single transaction

# 6. Service connection (reads from pg_service.conf)
psql service=mydb_prod

# 7. Reconnect within a session
\c dbname                                       # reconnect to different db
\c -reuse-previous=on sslmode=require           # change only sslmode
\c "host=newhost port=5432 dbname=mydb"         # conninfo string

On connection failure: interactive mode keeps the previous connection; script mode closes it and all subsequent database commands fail until the next successful \c.

Key flags: -h host, -p port, -U user, -d database, -w no password prompt, -W force password prompt, -1 single transaction, -f execute file, -c execute command, -t tuples only, -x expanded, -A unaligned, -E echo hidden queries (\d internals), -L log file, -X skip ~/.psqlrc.

Connection precedence: CLI flags > environment variables > pg_service.conf > defaults. Password precedence: connection string/password flag > PGPASSWORD env > ~/.pgpass. Use ~/.pgpass instead of PGPASSWORD in production — PGPASSWORD is visible in process listings (ps aux).

Object Inspection (\d family)

Command Shows
\d All tables, views, materialized views, sequences, foreign tables (equiv.\dtvmsE)
\dP Partitioned tables
\dt Tables only
\dv Views only
\di Indexes only
\ds Sequences only
\dm Materialized views only

…

よくある質問

psqlを使うのに、PostgreSQL本体をすべてインストールする必要がありますか?

いいえ、psqlを含む'postgresql-client'だけをインストールすれば十分です。他のコンピュータ上のPostgreSQLデータベースにも接続できます。

データベースに接続するにはどうすればよいですか?

`psql -h host -p port -U user -d dbname`のようなコマンドで接続できます。環境変数や設定ファイルを使う方法もあります。

データベース内のテーブルを一覧表示するにはどうすればよいですか?

接続後にターミナルで`\dt`と入力すると、すべてのテーブルが一覧表示されます。

詳細

作者による元の説明

PostgreSQL interactive terminal (psql) reference and usage guide.

repo: sickn33/agentic-awesome-skills

人気

GitHubのスター
48k
ランキング
#5471
ライセンス
MIT
更新日
2026/10/15

このスキルは役に立ちますか?

0

ログイン して投票しましょう。

コメント

  • …

このリポジトリの他のスキル

似ているスキル