「PostgreSQL」テーブルのカラムを一覧で取得する

2022年3月17日

環境
Windows10 64bit
PostgreSQL 9.6.5

書式
select * from information_schema.columns
where table_name= 'テーブル名’
order by ordinal_position;
「information_schema.columns」を使用して、テーブルのカラムを一覧で取得します。

操作例
「user_tblというテーブルのカラムを一覧で取得します
SQL構文

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
select
*
from
information_schema.columns
where
table_name = 'user_tbl'
order by
ordinal_position;
select * from information_schema.columns where table_name = 'user_tbl' order by ordinal_position;
select
  * 
from
  information_schema.columns 
where
  table_name = 'user_tbl' 
order by
  ordinal_position;

実行結果

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
table_catalog table_schema table_name column_name ordinal_position column_default is_nullable
testdb public user_tbl id 1(NULL)​ NO
table_catalog table_schema table_name column_name ordinal_position column_default is_nullable testdb public user_tbl id 1 ​(NULL)​ NO
table_catalog	table_schema	table_name	column_name	ordinal_position	column_default	is_nullable
testdb	         public      	user_tbl	id	         1	                  ​(NULL)​	      NO

 

 

PostgreSQL

Posted by arkgame