postgresql 在 postgres 中选择字段的数据类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2146705/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Select datatype of the field in postgres
提问by Wayne Conrad
How do I get datatype of specific field from table in postgres ? For example I have the following table, student_details ( stu_id integer, stu_name varchar(30 ), joined_date timestamp );
如何从 postgres 的表中获取特定字段的数据类型?例如我有下表, student_details ( stu_id integer, stu_name varchar(30 ), connected_date timestamp );
In this using the field name / or any other way, I need to get the datatype of the specific field. Is there any possibility ?
在此使用字段名称 / 或任何其他方式,我需要获取特定字段的数据类型。有没有可能?
回答by Wayne Conrad
You can get data types from the information_schema(8.4 docs referenced here, but this is not a new feature):
您可以从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)
回答by Nathan Villaescusa
You can use the pg_typeof()function, which also works well for arbitrary values.
您可以使用pg_typeof()函数,该函数也适用于任意值。
SELECT pg_typeof("stu_id"), pg_typeof(100) from student_details limit 1;
回答by Romulo Freires
Try this request :
试试这个请求:
SELECT column_name, data_type FROM information_schema.columns WHERE
table_name = 'YOUR_TABLE' AND column_name = 'YOUR_FIELD';
回答by Romulo Freires
run psql -E
and then \d student_details
运行psql -E
然后\d student_details
回答by Fil
If you like 'Mike Sherrill' solution but don't want to use psql, I used this query to get the missing information:
如果您喜欢 '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'
with result:
结果:
column_name | myType
-------------+-------------------
test_id | test_domain
test_vc | varchar(15)
test_n | numeric(15,3)
big_n | bigint
ip_addr | inet
回答by Mike Sherrill 'Cat Recall'
The information schema views and pg_typeof() return incomplete type information. Of these answers, psql
gives the most precise type information. (The OP might not need such precise information, but should know the limitations.)
信息模式视图和 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
);
Using psql
and \d public.test
correctly shows the use of the data type test_domain
, the length of varchar(n) columns, and the precision and scale of numeric(p, s) columns.
使用psql
并\d public.test
正确显示数据类型的使用、test_domain
varchar(n) 列的长度以及 numeric(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 |
This query against an information_schema view does notshow the use of test_domain
at all. It also doesn't report the details of varchar(n) and numeric(p, s) columns.
这个针对 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 | data_type -------------+------------------- test_id | character varying test_vc | character varying test_n | numeric big_n | bigint ip_addr | inet
You mightbe able to get all that information by joining other information_schema views, or by querying the system tables directly. psql -E
might help with that.
您可以通过加入其他 information_schema 视图或直接查询系统表来获取所有这些信息。psql -E
可能会有所帮助。
The function pg_typeof()
correctly shows the use of test_domain
, but doesn't report the details of varchar(n) and numeric(p, s) columns.
该函数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 | character varying | numeric | bigint | inet
回答by Piotr Findeisen
Pulling data type from information_schema
is possible, but not convenient (requires joining several columns with a case
statement). Alternatively one can use format_type
built-in function to do that, but it works on internal type identifiers that are visible in pg_attribute
but not in information_schema
. Example
从中提取数据类型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
Based on https://gis.stackexchange.com/a/97834.