【问题标题】:List tables in a PostgreSQL schema列出 PostgreSQL 模式中的表
【发布时间】:2013-03-16 15:59:43
【问题描述】:

当我在 psql 中执行\dt 时,我只会得到当前架构中的表列表(默认为public)。

如何获取所有架构或特定架构中的所有表的列表?

【问题讨论】:

    标签: postgresql postgresql-9.1 psql


    【解决方案1】:

    在所有模式中:

    => \dt *.*
    

    在特定架构中:

    => \dt public.*
    

    可以使用regular expressions with some restrictions

    \dt (public|s).(s|t)
           List of relations
     Schema | Name | Type  | Owner 
    --------+------+-------+-------
     public | s    | table | cpn
     public | t    | table | cpn
     s      | t    | table | cpn
    

    高级用户可以使用诸如字符类之类的正则表达式表示法,例如 [0-9] 来匹配任何数字。所有正则表达式特殊字符都按照第 9.7.3 节中的规定工作,除了 . 被用作上面提到的分隔符,* 被转换为正则表达式符号 .*? 这是翻译成.$ 是字面匹配的。您可以根据需要模拟这些模式字符,方法是为. 编写?,为R* 编写(R+|),或为R? 编写(R|)$ 不需要作为正则表达式字符,因为模式必须匹配整个名称,这与正则表达式的通常解释不同(换句话说,$ 会自动附加到您的模式中)。如果您不希望模式被锚定,请在开头和/或结尾写 *。请注意,在双引号内,所有正则表达式特殊字符都失去了它们的特殊含义,并按字面意思匹配。此外,正则表达式特殊字符在运算符名称模式中按字面意思匹配(即\do 的参数)。

    【讨论】:

    • 只是\dt 等同于\dt public.*,对吗?
    • 比如说,特定模式中的两个特定表怎么样?喜欢\dt public.user_info, public.user_scope
    • 没关系,在一行上做\dt public.a; \dt public.b; 更容易。
    • 这是一种隐含的..如果 \dt 只给出“公共”表,人们不会期望通过正则表达式得到更多的东西..
    • @FrozenFlame 不是!默认情况下,它会显示您的search_path 中的任何内容,而that 默认为"$user", public.*。因此,set search_path=s; \dt 将列出架构 s 中的所有表。
    【解决方案2】:

    您可以从information_schema中选择表格

    SELECT * FROM information_schema.tables 
    WHERE table_schema = 'public'
    

    【讨论】:

    • 如果您的界面不支持快捷方式,这将非常有用。谢谢。
    • 这也很好,因为您可以执行类似 select table_schema, table_name from information_schema.tables where table_name like '%whatever%'; 之类的操作。如果您需要知道表位于哪个架构中。不确定您是否可以使用 \dt
    • 谢谢,它适用于 Amazon Redshift 而 \dt(已接受的答案)不能。
    • 这是最常用的答案。 information_schema 是在 SQL 标准中定义的,并且适用于大多数符合要求的数据库
    • 要排除视图,请将AND table_type = 'BASE TABLE' 放在 where 子句中。
    【解决方案3】:

    除了information_schema,还可以使用pg_tables

    select * from pg_tables where schemaname='public';
    

    【讨论】:

    • 请注意,如果您只希望表名是结果查询,则为 SELECT tablename FROM pg_tables WHERE schemaname = 'public';
    • 发现information_schema 没有列出来自public 架构的项目的权限问题,但pg_tables 方法运行良好。非常感谢!
    【解决方案4】:

    对于那些将来遇到这种情况的人:

    如果您想查看多个模式的关系列表:

    $psql mydatabase
    mydatabase=# SET search_path TO public, usa;   #schema examples
    SET
    mydatabase=# \dt
                  List of relations
     Schema |      Name       | Type  |  Owner
    --------+-----------------+-------+----------
     public | counties        | table | postgres
     public | spatial_ref_sys | table | postgres
     public | states          | table | postgres
     public | us_cities       | table | postgres
     usa    | census2010      | table | postgres
    

    【讨论】:

      【解决方案5】:

      如果您有兴趣列出 特定 架构中的所有表,我发现 this answer 相关:

      SELECT table_schema||'.'||table_name AS full_rel_name
        FROM information_schema.tables
       WHERE table_schema = 'yourschemaname';
      

      【讨论】:

      • 要排除视图,请将AND table_type = 'BASE TABLE' 放在 where 子句中。
      猜你喜欢
      • 2015-01-31
      • 2021-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-12
      • 1970-01-01
      • 2011-01-17
      • 1970-01-01
      相关资源
      最近更新 更多