如何从Postgres表中获取特定字段的数据类型? 例如,我有以下表格, student_details ( stu_id integer, stu_name varchar(30 ), joined_date timestamp );
使用字段名称或其他方式,我需要获取特定字段的数据类型。是否有可能实现?
如何从Postgres表中获取特定字段的数据类型? 例如,我有以下表格, student_details ( stu_id integer, stu_name varchar(30 ), joined_date timestamp );
使用字段名称或其他方式,我需要获取特定字段的数据类型。是否有可能实现?
你可以从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)
where table_catalog = ? and table_schema = ? and table_name = ?;
但是,这个information_schema视图并没有考虑DDL可能使用了domains。 - Mike Sherrill 'Cat Recall'pg_typeof
一起使用。 - Frankie Drakecharacter_maximum_length
。此外,你可能想要获取数字精度... 这应该显示:select column_name, data_type, coalesce(character_maximum_length, numeric_precision) as precision from information_schema.columns where table_name = 'record';
- NuxORDER BY ordinal_position
,就像这样SELECT column_name, data_type FROM information_schema.columns where table_name = '{table_name}' ORDER BY ordinal_position;
。 - undefinedSELECT pg_typeof("stu_id"), pg_typeof(100) from student_details limit 1;
SELECT pg_typeof( date_part( 'year', now() ) ) AS expr
,它的结果可能与您预期的不同。请注意,这并非解释,而是翻译原文的简要概括。 - Leo Orientispg_typeof
可以用于存储过程输出的字段,对于这些字段,后台表格(如果存在)是未知/不清楚的。示例: select state, qstart, pg_typeof(qstart) as ty_qstart from listconn()
。 在这种情况下,information_schema 没有什么用处。 - JL Peyretinformation_schema.columns
获取该信息。 - NuxLIMIT
的文档,"查询优化器在生成查询计划时会考虑LIMIT",因此它很可能不会检查所有行。 - undefined尝试以下请求:
SELECT column_name, data_type FROM information_schema.columns WHERE
table_name = 'YOUR_TABLE' AND column_name = 'YOUR_FIELD';
运行 psql -E
然后输入 \d student_details
如果您喜欢'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
信息模式视图和pg_typeof()返回不完整的类型信息。其中,psql
提供了最精确的类型信息。(楼主可能不需要如此精确的信息,但应该知道其局限性。)
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
);
使用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 |
对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';
列名 | 数据类型 -------------+------------------- test_id | 变长字符串类型 test_vc | 变长字符串类型 test_n | 数值类型 big_n | 大整数类型 ip_addr | IP地址类型
你可以通过连接其他 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;
测试ID | 测试VC | 测试N | 大N | IP地址 -------------+-------------------+---------+--------+--------- 测试域名 | 变长字符串类型 | 数字型 | 大整型 | 网际网路协议地址
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
b.relfilenode
替换为b.oid
。 - tswatershttps://www.postgresql.org/docs/current/app-psql.html#APP-PSQL-PATTERNS
\gdesc
显示当前查询缓冲区结果的描述信息(即列名和数据类型)。实际上并不执行查询,但是如果查询包含某种语法错误,则会按照正常方式报告该错误。如果当前查询缓冲区为空,则显示最近发送的查询的描述信息。
因此,您可以使用 table student_details limit 0 \gdesc
命令来输出占用更少空间的结果,而不是使用 \d
命令。
SELECT pg_typeof(bike_launch_date)
FROM public.bike_details
LIMIT 1;
为了避免重复,将LIMIT设置为1。
运行以下命令,并附上表名,以获取所有列的数据类型和其他信息。
示例
\d table_name;