postgresでフィールドのデータ型を選択します


165

postgresのテーブルから特定のフィールドのデータ型を取得するにはどうすればよいですか?たとえば、次のテーブル、student_details(stu_id integer、stu_name varchar(30)、joined_date timestamp)があります。

これでフィールド名/またはその他の方法を使用して、特定のフィールドのデータ型を取得する必要があります。可能性はありますか?


回答:


173

information_schemaからデータ型を取得できます(8.4ドキュメントはここで参照されますが、これは新機能ではありません)。

=# select column_name, data_type from information_schema.columns
-# where table_name = 'config';
    column_name     | data_type 
--------------------+-----------
 id                 | integer
 default_printer_id | integer
 master_host_enable | boolean
(3 rows)

とてもシンプルでいい!これで、310文字(テーブル名なし)、4つのテーブル結合、スキーマ非対応、高コストであり、 'int4'などを整数ではなく型として与えるという、現在のクエリを置き換えることができます。ありがとうございました!
いくつかの

2
PostgreSQLでは、複数のスキーマで同じテーブル名(同じテーブルであっても)を使用できます。そのWHERE句を記述する堅牢な方法は、その可能性をwhere table_catalog = ? and table_schema = ? and table_name = ?;考慮しています。ただし、このinformation_schemaビューでは、DDLがドメインを使用した可能性があるとは考慮されていません。
マイクシェリル「キャットリコール」、

1
これは配列のタイプを与えないので、それとともに使用する必要がありますpg_typeof
Daria

146

pg_typeof()関数を使用できます。これは、任意の値に対しても適切に機能します。

SELECT pg_typeof("stu_id"), pg_typeof(100) from student_details limit 1;

これはテーブルのレコードごとに行を返します。何百万ものレコードがある場合は実行しないでください
Saarang

3
計算のタイプを決定する必要がある場合、これは美しく機能します。たとえば、SELECT pg_typeof( date_part( 'year', now() ) ) AS exprおそらくあなたが期待するものとは異なります。
レオオリエン

ここでの賢いことはpg_typeof、バックエンドテーブルが存在する場合でも、不明/不明確であるストアドプロシージャからのフィールドに対して機能することです。 select state, qstart, pg_typeof(qstart) as ty_qstart from listconn()。information_schemaはここではあまり役に立ちません。
JL Peyret

40

このリクエストを試してください:

SELECT column_name, data_type FROM information_schema.columns WHERE 
table_name = 'YOUR_TABLE' AND column_name = 'YOUR_FIELD';

4
table_name = 'YOUR_TABLE' AND column_name = 'YOUR_FIELD';
ハイサム

38

実行psql -Eしてから\d student_details


シンプルで便利
horoyoi o

11

「Mike Sherrill」ソリューションが好きで、psqlを使用したくない場合は、次のクエリを使用して不足している情報を取得します。

select column_name,
case 
    when domain_name is not null then domain_name
    when data_type='character varying' THEN 'varchar('||character_maximum_length||')'
    when data_type='numeric' THEN 'numeric('||numeric_precision||','||numeric_scale||')'
    else data_type
end as myType
from information_schema.columns
where table_name='test'

結果:

column_name |     myType
-------------+-------------------
 test_id     | test_domain
 test_vc     | varchar(15)
 test_n      | numeric(15,3)
 big_n       | bigint
 ip_addr     | inet

8

情報スキーマビューとpg_typeof()は不完全な型情報を返します。これらの回答のうちpsql、最も正確な型情報を提供します。(OPはそのような正確な情報を必要としない場合がありますが、制限を知っている必要があります。)

create domain test_domain as varchar(15);

create table test (
  test_id test_domain, 
  test_vc varchar(15), 
  test_n numeric(15, 3), 
  big_n bigint,
  ip_addr inet
);

データ型の使用、varchar(n)列の長さ、numeric(p、s)列の精度とスケールを使用psqlして\d public.test正しく示していtest_domainます。

sandbox =#\ d public.test
             テーブル "public.test"
 コラム| タイプ| 修飾子
--------- + ----------------------- + -----------
 test_id | test_domain |
 test_vc | 文字の変化(15)|
 test_n | 数値(15,3)|
 big_n | bigint |
 ip_addr | inet |

information_schemaビューに対するこのクエリは、の使用をまったく示していませtest_domain。また、varchar(n)およびnumeric(p、s)列の詳細も報告しません。

select column_name, data_type 
from information_schema.columns 
where table_catalog = 'sandbox'
  and table_schema = 'public'
  and table_name = 'test';
column_name | データ・タイプ
------------- + -------------------
 test_id | キャラクターの変化
 test_vc | キャラクターの変化
 test_n | 数値
 big_n | bigint
 ip_addr | イネット

あなたは可能性がある、またはシステムテーブルを直接照会することによって、他のすべてのINFORMATION_SCHEMAビューに参加することによって、その情報を得ることができます。psql -E助けになるかもしれません。

関数pg_typeof()はの使用を正しく示しますがtest_domain、varchar(n)およびnumeric(p、s)列の詳細を報告しません。

select pg_typeof(test_id) as test_id, 
       pg_typeof(test_vc) as test_vc,
       pg_typeof(test_n) as test_n,
       pg_typeof(big_n) as big_n,
       pg_typeof(ip_addr) as ip_addr
from test;
   test_id | test_vc | test_n | big_n | ip_addr
------------- + ------------------- + --------- + ------ -+ ---------
 test_domain | キャラクターの変化| 数値| bigint | イネット

4

からデータ型をプルするinformation_schemaことは可能ですが、便利ではありません(caseステートメントで複数の列を結合する必要があります)。別の方法として、format_type組み込み関数を使用してそれを行うこともできますが、それはには表示されますpg_attributeがには表示されない内部型識別子に対して機能しますinformation_schema。例

SELECT a.attname as column_name, format_type(a.atttypid, a.atttypmod) AS data_type
FROM pg_attribute a JOIN pg_class b ON a.attrelid = b.relfilenode
WHERE a.attnum > 0 -- hide internal columns
AND NOT a.attisdropped -- hide deleted columns
AND b.oid = 'my_table'::regclass::oid; -- example way to find pg_class entry for a table

https://gis.stackexchange.com/a/97834に基づいています


1
後世のために、PG10と交換しb.relfilenodeb.oid
tswaters
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.