2012-09-16 55 views
101

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

我想在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=# 

回答

163

,你必须使用:

\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可以作为一个有用的指导。

+0

哇!非常感谢你的顺便说一句。这非常有价值。 –

+0

'information_schema.tables'包含某些原因的视图。 (无论如何,在PostgreSQL 9.2中。) – jpmc26

+0

@ jpmc26是的,'table_type ='VIEW'',所以它们很容易排除。一般来说,SQL尽可能地尝试将表视为与表相同。 –

85

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

\c liferay 
\dt 

这就是我怎么做也无妨。

可以这两个命令组合成一条线,如果你喜欢:如果您希望列出所有

\c liferay \dt 
+2

你真的想'\ DT * *'如果不是所有感兴趣的表是在'SEARCH_PATH。 '。 –

+2

感谢'\ c db_name' –

+0

我得到的只是'没有找到关系.' – surfer190

4

要看到公共表,你可以做

清单表

\dt 

名单表,视图和访问权限

\dp or \z 

或只是表名

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

在SQL Que RY,你可以这样写代码:

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 
-1

您可以键入\?获得在PSQL支持的所有命令的信息。

0

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

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