【问题标题】:Select datatype of the field in postgres在 postgres 中选择字段的数据类型
【发布时间】:2011-01-09 22:36:43
【问题描述】:

如何从 postgres 的表中获取特定字段的数据类型? 例如 我有下表, 学生详细信息( stu_id 整数, stu_name varchar(30 ), 加入日期时间戳 );

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

【问题讨论】:

标签: postgresql


【解决方案1】:

https://www.postgresql.org/docs/current/app-psql.html#APP-PSQL-PATTERNS

\gdesc 显示描述(即列名和数据 types) 当前查询缓冲区的结果。查询不是 实际执行;但是,如果它包含某种类型的语法错误, 该错误会以正常方式报错。

如果当前查询缓冲区为空,则最近发送的查询为 而是描述。

所以你可以table student_details limit 0 \gdesc 输出占用的空间小于\d

【讨论】:

    【解决方案2】:

    试试这个请求:

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

    【讨论】:

    • table_name = 'YOUR_TABLE' AND column_name = 'YOUR_FIELD';
    • 如果您有多个同名的表,则可能需要在请求中包含 table_schema:table_schema='YOUR_SCHEMA' AND table_name='YOUR_TABLE' AND column_name='YOUR_FIELD'
    【解决方案3】:

    可以从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

    【讨论】:

    • 为了后代,用 pg10 将 b.relfilenode 替换为 b.oid
    【解决方案4】:

    如果您喜欢“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
    

    【讨论】:

      【解决方案5】:

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

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

      【讨论】:

      • 这将返回表中每条记录的一行。如果您有数百万条记录,请不要运行它
      • 如果您需要确定计算的类型,这将非常有效。例如,SELECT pg_typeof( date_part( 'year', now() ) ) AS expr 可能与您的预期不同。
      • 这里的聪明之处在于pg_typeof 适用于来自存储过程的字段,对于这些字段,即使存在后端表,也是未知/不清楚的。 select state, qstart, pg_typeof(qstart) as ty_qstart from listconn()。 information_schema 在这里没有多大帮助。
      • @JLPeyret 完全正确!我可以多次投票
      【解决方案6】:

      信息模式视图和 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
      );
      

      使用psql\d public.test 正确显示了数据类型test_domain 的使用、varchar(n) 列的长度以及 numeric(p, s) 列的精度和小数位数。

      沙箱=# \d public.test 表“public.test” 专栏 |类型 |修饰符 ---------+-----------+------------ test_id |测试域 | 测试_vc |字符变化(15) | 测试_n |数字(15,3) | 大_n |大整数 | ip_addr |网络 |

      这个针对 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 |性格多变 测试_vc |性格多变 测试_n |数字 大_n |大整数 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 |测试_vc |测试_n |大_n | ip_addr -------------+-------------------+------------+------ --+--------- 测试域 |性格变化|数字 |大整数 |网络

      【讨论】:

        【解决方案7】:

        运行psql -E,然后运行\d student_details

        【讨论】:

        • 简单实用
        【解决方案8】:

        您可以从 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' 和其他类型作为类型而不是整数。谢谢!
        • PostgreSQL 允许您在多个模式中拥有相同的表名(甚至是相同的表)。编写 WHERE 子句的稳健方法考虑了这种可能性:where table_catalog = ? and table_schema = ? and table_name = ?; 但是这个 information_schema 视图没有考虑到 DDL 可能使用了domains
        • 这不会给你数组的类型,所以它必须与pg_typeof一起使用
        猜你喜欢
        • 2017-04-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-15
        • 1970-01-01
        • 2019-12-20
        • 1970-01-01
        • 2023-03-28
        相关资源
        最近更新 更多