选择postgres中字段的数据类型

144 postgresql

如何从postgres中的表中获取特定字段的数据类型?例如,我有下表,student_details(stu_id integer,stu_name varchar(30),joined_date timestamp);

在这使用字段名称/或任何其他方式,我需要获取特定字段的数据类型.有可能吗?

Way*_*rad 158

您可以从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)
Run Code Online (Sandbox Code Playgroud)

  • PostgreSQL允许您在多个模式中具有相同的表名(甚至是相同的表).编写WHERE子句的强大方法考虑了这种可能性:`where table_catalog =?和table_schema =?和table_name =?;`但是这个information_schema视图并不认为DDL可能使用过[domains](http://www.postgresql.org/docs/9.1/static/sql-createdomain.html). (2认同)
  • 这不会给你数组的类型,所以它必须与 `pg_typeof` 一起使用 (2认同)
  • 对于 varchar 和类似类型,您应该使用“character_maximum_length”。另外,您可能想获得数字精度...这应该显示:`select column_name, data_type, coalesce(character_maximum_length, numeric_ precision) as precision from information_schema.columns where table_name = 'record';` (2认同)

Nat*_*usa 129

您可以使用pg_typeof()函数,该函数也适用于任意值.

SELECT pg_typeof("stu_id"), pg_typeof(100) from student_details limit 1;
Run Code Online (Sandbox Code Playgroud)

  • 如果表中没有记录,则不起作用 (9认同)
  • 这里聪明的事情是“pg_typeof”适用于来自存储过程的字段,对于这些字段,后端表(如果存在)是未知/不清楚的。`从 listconn() 选择 state, qstart, pg_typeof(qstart) 作为 ty_qstart`。information_schema 在这里没有多大帮助。 (3认同)
  • 如果您需要确定计算的类型,这可以很好地工作。例如,`SELECT pg_typeof(date_part('year',now()))AS expr`可能与您期望的不同。 (2认同)

小智 34

psql -E然后\d student_details

  • 简单又有用 (2认同)

小智 29

试试这个请求:

SELECT column_name, data_type FROM information_schema.columns WHERE 
table_name = 'YOUR_TABLE' AND column_name = 'YOUR_FIELD';
Run Code Online (Sandbox Code Playgroud)

  • `table_name ='YOUR_TABLE'和column_name ='YOUR_FIELD';` (3认同)

Fil*_*Fil 9

如果您喜欢'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'
Run Code Online (Sandbox Code Playgroud)

结果:

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


Mik*_*ll' 7

信息模式视图和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
);
Run Code Online (Sandbox Code Playgroud)

使用psql\d public.test正确显示数据类型的使用,test_domainvarchar(n)列的长度以及数字(p,s)列的精度和比例.

sandbox=# \d public.test
             Table "public.test"
 Column  |         Type          | Modifiers
---------+-----------------------+-----------
 test_id | test_domain           |
 test_vc | character varying(15) |
 test_n  | numeric(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';
Run Code Online (Sandbox Code Playgroud)
 column_name |     data_type
-------------+-------------------
 test_id     | character varying
 test_vc     | character varying
 test_n      | numeric
 big_n       | bigint
 ip_addr     | inet

可以通过加入其他information_schema视图或直接查询系统表来获取所有信息.psql -E可能会有所帮助.

该函数pg_typeof()正确显示了test_domainvarchar(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;
Run Code Online (Sandbox Code Playgroud)
   test_id   |      test_vc      | test_n  | big_n  | ip_addr
-------------+-------------------+---------+--------+---------
 test_domain | character varying | numeric | bigint | inet


Pio*_*sen 6

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
Run Code Online (Sandbox Code Playgroud)

基于https://gis.stackexchange.com/a/97834