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?

初めに

Qiita Engineer Festa 2024に参加中。完走目指してます。
他のメタコマンドについては以下から読んでください。

\dとは

patternにマッチする各リレーション(テーブル、ビュー、マテリアライズドビュー、インデックス、シーケンス、外部テーブル)または複合型について、全ての列、列の型、テーブル空間(デフォルト以外を使用している場合)、NOT NULLやデフォルトなどの特別な属性を表示します。 関連付けられているインデックス、制約、ルールおよびトリガも表示されます。 外部テーブルでは関連する外部サーバも表示されます。 (「パターンのマッチング」については後述のパターンで定義されています。)

一部の種類のリレーションでは、\dは各列について追加の情報を表示します。 例えば、シーケンスでは列の値、インデックスではインデックス式、外部テーブルでは外部データラッパーのオプションです。

\d+というコマンド形式も同一ですが、より多くの情報を表示します。 こちらでは、テーブルの列に関連付けられたコメントやテーブルにOIDが存在するかどうか、リレーションがビューの場合はビューの定義、デフォルトと異なるreplica identityの設定、リレーションにアクセスメソッドがあるならアクセスメソッド名も表示されます。

デフォルトではユーザが作成したオブジェクトのみが表示されます。 システムオブジェクトを含めるためには、パターンまたはS修飾子を付与してください。

試してみた

テーブル、ビュー、シーケンスのリストを表示

postgres=# \d
                 List of relations
 Schema |        Name         |   Type   |  Owner   
--------+---------------------+----------+----------
 public | sales               | table    | postgres
 public | source_table        | table    | postgres
 public | source_table_id_seq | sequence | postgres
 public | target_table        | table    | postgres
 public | test                | table    | postgres
 public | test_data           | table    | postgres
 public | test_data_id_seq    | sequence | postgres
 public | test_id_seq         | sequence | postgres
 public | users               | table    | postgres
 public | users_id_seq        | sequence | postgres
(10 rows)

特定のテーブルのカラム情報、インデックス情報、制約情報などを表示

postgres=# \d users
                                    Table "public.users"
 Column |          Type          | Collation | Nullable |              Default              
--------+------------------------+-----------+----------+-----------------------------------
 id     | integer                |           | not null | nextval('users_id_seq'::regclass)
 name   | character varying(100) |           |          | 
 age    | integer                |           |          | 
Indexes:
    "users_pkey" PRIMARY KEY, btree (id)

+オプションを使用して、さらに詳細な情報を表示

postgres=# \d+ users
                                                               Table "public.users"
 Column |          Type          | Collation | Nullable |              Default              | Storage  | Compression | Stats target | Description 
--------+------------------------+-----------+----------+-----------------------------------+----------+-------------+--------------+-------------
 id     | integer                |           | not null | nextval('users_id_seq'::regclass) | plain    |             |              | 
 name   | character varying(100) |           |          |                                   | extended |             |              | 
 age    | integer                |           |          |                                   | plain    |             |              | 
Indexes:
    "users_pkey" PRIMARY KEY, btree (id)
Access method: heap

まとめ

主に調査などで現状を知るのに利用しています。

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?