0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Postgres コマンド [ 備忘録 ]

0
Posted at

サービスの起動・停止・確認

PostgreSQLサービスの起動

# Homebrewでインストールした場合
brew services start postgresql@15

PostgreSQLサービスの停止

# Homebrewサービスの停止
brew services stop postgresql@15

サービス状態の確認

# サービス一覧を確認
brew services list | grep postgresql

# 実行中のプロセスを確認
ps aux | grep postgres

データベースへの接続

デフォルトデータベースに接続

# ユーザー名を指定して接続
psql -U your_username

# デフォルトユーザーで接続
psql postgres

特定のデータベースに接続

# データベース名を指定
psql -d database_name -U username

# ローカルの特定データベースに接続
psql todo_db

出力例:

psql (15.13)
Type "help" for help.

postgres=#

基本的なpsqlコマンド

データベース操作

-- データベース一覧を表示
\l

-- 新しいデータベースを作成
CREATE DATABASE todo_db;

-- データベースに接続
\c todo_db

-- 現在のデータベースを確認
SELECT current_database();

出力例:

postgres=# \l
                                                List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    | ICU Locale | Locale Provider |   Access privileges   
-----------+----------+----------+-------------+-------------+------------+-----------------+-----------------------
 postgres  | oyne2561 | UTF8     | en_US.UTF-8 | en_US.UTF-8 |            | libc            | 
 template0 | oyne2561 | UTF8     | en_US.UTF-8 | en_US.UTF-8 |            | libc            | =c/oyne2561          +
           |          |          |             |             |            |                 | oyne2561=CTc/oyne2561
 template1 | oyne2561 | UTF8     | en_US.UTF-8 | en_US.UTF-8 |            | libc            | =c/oyne2561          +
           |          |          |             |             |            |                 | oyne2561=CTc/oyne2561
 todo_db   | oyne2561 | UTF8     | en_US.UTF-8 | en_US.UTF-8 |            | libc            | 

テーブル操作

-- テーブル一覧を表示
\dt

-- テーブル構造を確認
\d table_name

-- サンプルテーブルを作成
CREATE TABLE todos (
    id SERIAL PRIMARY KEY,
    title VARCHAR(255) NOT NULL,
    completed BOOLEAN DEFAULT FALSE,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- テーブルにデータを挿入
INSERT INTO todos (title) VALUES ('Clojureを学習する');
INSERT INTO todos (title) VALUES ('PostgreSQLをマスターする');

-- データを確認
SELECT * FROM todos;

出力例:

todo_db=# SELECT * FROM todos;
 id |         title          | completed |         created_at         
----+------------------------+-----------+----------------------------
  1 | Clojureを学習する      | f         | 2025-06-22 10:30:15.123456
  2 | PostgreSQLをマスターする| f         | 2025-06-22 10:30:25.789012
(2 rows)

ユーザー管理

-- ユーザー一覧を表示
\du

-- 新しいユーザーを作成
CREATE USER app_user WITH PASSWORD 'password123';

-- ユーザーに権限を付与
GRANT ALL PRIVILEGES ON DATABASE todo_db TO app_user;

よく使うpsqlメタコマンド

\?          # ヘルプを表示
\q          # psqlを終了
\l          # データベース一覧
\dt         # テーブル一覧
\du         # ユーザー一覧
\d table    # テーブル構造を表示
\c dbname   # データベースに接続
\i file.sql # SQLファイルを実行
\o file.txt # 出力をファイルにリダイレクト
\timing     # クエリ実行時間を表示
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?