ps列出所有表

时间:2012-09-16 08:59:58

标签: postgresql psql

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

我想在SELECT * FROM applications;数据库中执行liferayapplications是我的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=# 

7 个答案:

答案 0 :(得分:183)

如果您希望列出所有表,则必须使用:

\dt *.*

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

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

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

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

$ 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 catalogspsql -E可以作为指导如何执行此操作。

答案 1 :(得分:88)

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

\c liferay
\dt

无论如何,我就是这样做的。

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

\c liferay \dt

答案 2 :(得分:7)

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

列表

\dt

列出表格,视图和访问权限

\dp or \z

或只是表名

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

答案 3 :(得分:1)

在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';

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

select table_name from information_schema.tables

答案 4 :(得分:0)

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

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

答案 5 :(得分:0)

一个单行示例是

\dt schemaname.* 

在您的Senario中

\dt public.*

答案 6 :(得分:-2)

您可以键入\?以获取有关psql支持的所有命令的信息。