この記事には広告を含む場合があります。
記事内で紹介する商品を購入することで、当サイトに売り上げの一部が還元されることがあります。
テーブル一覧を確認するコマンド
PostgreSQL にて作成したテーブル一覧を確認する方法について解説します。
\dt コマンド
まずは psql のコマンドラインにて実行する「\dt」コマンドです。PostgreSQL にて作成したデータベースに接続した後に
〇実行するコマンド
\dt
を実行すると、データベースにある全てのテーブルを確認することができます。
〇コマンドの実行履歴
|
1 2 3 4 5 6 7 8 9 |
test_db=> \dt List of relations Schema | Name | Type | Owner --------+----------+----------+----------- public | sample1 | table | test_owner public | sample2 | table | test_owner public | sample11 | sequence | test_owner public | sample12 | sequence | test_owner (4 rows) |
システムカタログ pg_tables を確認する
次の方法は PostgreSQL のシステムカタログにあるテーブルに関する情報をまとめた pg_tables から参照する方法です。
〇実行する SQL 文
select schemaname, tablename, tableowner from pg_tables;
pg_tables で提供されているカラムは下記の通りです。
| 名前 | 型 | 参照先 | 説明 |
|---|---|---|---|
| schemaname | name | pg_namespace.nspname | テーブルを持つスキーマ名 |
| tablename | name | pg_class.relname | テーブル名 |
| tableowner | name | pg_authid.rolname | テーブルの所有者 |
| tablespace | name | pg_tablespace.spcname | テーブルを含むテーブル空間の名前(データベースのデフォルトの場合はNULL) |
| hasindexes | boolean | pg_class.relhasindex | テーブルがインデックスを持っている(もしくは最近まで持っていた)なら真 |
| hasrules | boolean | pg_class.relhasrules | テーブルにルールがある(もしくは以前あった)時は真 |
| hastriggers | boolean | pg_class.relhastriggers | テーブルにトリガがある(もしくは以前あった)時は真 |
| rowsecurity | boolean | pg_class.relrowsecurity | テーブルの行セキュリティが有効なら真 |
紹介した SQL 文は、提供された機能の中からスキーマ名、テーブル名、テーブルの所有者を表示しています。
〇SQL 文の実行履歴
|
1 2 3 4 5 6 7 8 |
test_db=> select schemaname, tablename, tableowner from pg_tables; schemaname | tablename | tableowner ------------+--------------+------------- public | test | test_owner public | color_master | test_owner public | city_master | test_owner public | user_info | test_owner (4 rows) |
さいごに
SQL 文は、MySQL や SQLServer、Oracle など複数の言語で共通性が見えるものの、データベースやテーブルの参照についてはデータベース言語ごとに独自の構文が多く見受けられます。
ふと思い出せないときに本記事が参考になれば幸いです。
Linuxの知識については下記の本も参考になるので、スキルアップにお役立てください。


