【问题标题】:Psql list all tablespsql列出所有表
【发布时间】:2012-09-16 08:59:58
【问题描述】:

我想在我的 PostgreSQL 安装中列出 liferay 数据库中的所有表。我该怎么做?

我想在liferay 数据库中执行SELECT * FROM applications;applications 是我的 liferay 数据库中的一张表。这是怎么做到的?

这是我所有数据库的列表:

postgres=# \list
                              List of databases
Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
 -----------+----------+----------+-------------+-------------+-----------------------
 liferay   | postgres | UTF8     | en_GB.UTF-8 | en_GB.UTF-8 | =Tc/postgres         +
           |          |          |             |             | postgres=CTc/postgres+
           |          |          |             |             | liferay=CTc/postgres
 lportal   | postgres | UTF8     | en_GB.UTF-8 | en_GB.UTF-8 | 
 postgres  | postgres | UTF8     | en_GB.UTF-8 | en_GB.UTF-8 | 
 template0 | postgres | UTF8     | en_GB.UTF-8 | en_GB.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_GB.UTF-8 | en_GB.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
(5 rows)

postgres=# 

【问题讨论】:

    标签: postgresql psql


    【解决方案1】:

    如果你想列出所有个表,你必须使用:

    \dt *.*
    

    表示您希望所有表在所有模式中。这将包括pg_catalog 中的表、系统表和information_schema 中的表。没有内置方式可以说“所有用户定义模式中的所有表”;但是,您可以在运行 \dt 之前将您的 search_path 设置为所有感兴趣的模式的列表。

    您可能希望以编程方式执行此操作,在这种情况下,psql 反斜杠命令将无法完成此工作。这就是the INFORMATION_SCHEMA 来救援的地方。列出表格:

    SELECT table_name FROM information_schema.tables WHERE table_schema = 'public';
    

    顺便说一句,如果您想查看 psql 对反斜杠命令的响应,请使用 -E 标志运行 psql。例如:

    $ psql -E regress    
    regress=# \list
    ********* QUERY **********
    SELECT d.datname as "Name",
           pg_catalog.pg_get_userbyid(d.datdba) as "Owner",
           pg_catalog.pg_encoding_to_char(d.encoding) as "Encoding",
           d.datcollate as "Collate",
           d.datctype as "Ctype",
           pg_catalog.array_to_string(d.datacl, E'\n') AS "Access privileges"
    FROM pg_catalog.pg_database d
    ORDER BY 1;
    **************************
    

    所以您可以看到psql 在获取数据库列表时正在搜索pg_catalog.pg_database。同样,对于给定数据库中的表:

    SELECT n.nspname as "Schema",
      c.relname as "Name",
      CASE c.relkind WHEN 'r' THEN 'table' WHEN 'v' THEN 'view' WHEN 'i' THEN 'index' WHEN 'S' THEN 'sequence' WHEN 's' THEN 'special' WHEN 'f' THEN 'foreign table' END as "Type",
      pg_catalog.pg_get_userbyid(c.relowner) as "Owner"
    FROM pg_catalog.pg_class c
         LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
    WHERE c.relkind IN ('r','')
          AND n.nspname <> 'pg_catalog'
          AND n.nspname <> 'information_schema'
          AND n.nspname !~ '^pg_toast'
      AND pg_catalog.pg_table_is_visible(c.oid)
    ORDER BY 1,2;
    

    在可能的情况下,最好使用 SQL 标准的可移植INFORMATION_SCHEMA 而不是 Pg 系统目录,但有时您需要 Pg 特定的信息。在这些情况下,可以直接查询system catalogs,而psql -E 可以作为如何查询的有用指南。

    【讨论】:

    • information_schema.tables 出于某种原因包含视图。 (无论如何,在 PostgreSQL 9.2 中。)
    • @jpmc26 是的,table_type = 'VIEW',所以很容易排除。一般来说,SQL 会尽量将视图视为表。
    【解决方案2】:

    连接数据库,然后列出表:

    \c liferay
    \dt
    

    反正我就是这么干的。

    如果您愿意,可以将这两个命令合并到一行中:

    \c liferay \dt
    

    【讨论】:

    • 如果不是所有感兴趣的表都在search_path 上,您实际上想要\dt *.*
    【解决方案3】:

    要查看您可以执行的公共表

    列出表格

    \dt
    

    列出表、视图和访问权限

    \dp or \z
    

    或者只是表名

    select table_name from information_schema.tables where table_schema = 'public';
    

    【讨论】:

      【解决方案4】:

      在 SQL Query 中,您可以编写以下代码:

      select table_name from information_schema.tables where table_schema='YOUR_TABLE_SCHEME';
      

      用 YOUR_TABLE_SCHEME 替换您的表方案;

      例子:

      select table_name from information_schema.tables where table_schema='eLearningProject';
      

      查看所有scheme和所有表,不需要where子句:

      select table_name from information_schema.tables
      

      【讨论】:

        【解决方案5】:

        单行示例是:

        \dt schemaname.* 
        

        在你的场景中:

        \dt public.*
        

        并获取模式列表:

        \dn
        

        请注意,public 是未指定时的默认架构。引用documentation

        在前面的部分中,我们创建了没有指定任何表的表 架构名称。默认情况下,此类表(和其他对象)是 自动放入名为“public”的模式中。每个新数据库 包含这样的架构。

        使用\dt *.* 将输出所有模式中所有表的长列表,包括pg_catalog 等内部表。以上可以帮助过滤。

        【讨论】:

          【解决方案6】:

          如果您不需要所有模式中的所有表,这可以在自动化脚本中使用:

            for table in $(psql -qAntc '\dt' | cut -d\| -f2); do
                ...
            done
          

          【讨论】:

            猜你喜欢
            • 2021-02-13
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-12-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多