Psql列出所有表

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

我想执行SELECT * FROM applications;liferay数据库中。 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=# 

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

 \dt *.* 

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

您可能希望以编程方式执行此操作,在这种情况下, psql反斜杠命令将不会执行此任务。 这是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在获取数据库列表时正在searchpg_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专有的信息。 在这种情况下,可以直接查询系统目录 , psql -E可以作为一个有用的指导。

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

 \c liferay \dt 

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

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

 \c liferay \dt 

要查看公共表,你可以做

列表表格

 \dt 

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

 \dp or \z 

或者只是表名

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

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

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

用YOUR_TABLE_SCHEMEreplace你的表格scheme;

例:

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

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

 select table_name from information_schema.tables 

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

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

你可以input\? 获取有关psql支持的所有命令的信息。