列出 PostgreSQL 模式中的表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15644152/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 00:06:32  来源:igfitidea点击:

List tables in a PostgreSQL schema

postgresqlpostgresql-9.1psql

提问by Nyxynyx

When I do a \dtin psql I only get a listing of tables in the current schema (publicby default).

当我\dt在 psql 中执行 a 时,我只能获得当前模式中的表列表(public默认情况下)。

How can I get a list of all tables in all schemas or a particular schema?

如何获取所有模式或特定模式中所有表的列表?

回答by Clodoaldo Neto

In all schemas:

在所有模式中:

=> \dt *.*

In a particular schema:

在特定模式中:

=> \dt public.*

It is possible to use 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

Advanced users can use regular-expression notations such as character classes, for example [0-9] to match any digit. All regular expression special characters work as specified in Section 9.7.3, except for .which is taken as a separator as mentioned above, *which is translated to the regular-expression notation .*, ?which is translated to ., and $which is matched literally. You can emulate these pattern characters at need by writing ?for ., (R+|)for R*, or (R|)for R?. $is not needed as a regular-expression character since the pattern must match the whole name, unlike the usual interpretation of regular expressions (in other words, $is automatically appended to your pattern). Write *at the beginning and/or end if you don't wish the pattern to be anchored. Note that within double quotes, all regular expression special characters lose their special meanings and are matched literally. Also, the regular expression special characters are matched literally in operator name patterns (i.e., the argument of \do).

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

回答by Jakub Kania

You can select the tables from information_schema

您可以从表中选择 information_schema

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

回答by Radek Posto?owicz

Alternatively to information_schemait is possible to use pg_tables:

或者information_schema可以使用pg_tables

select * from pg_tables where schemaname='public';

回答by Blee

For those coming across this in the future:

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

If you would like to see a list of relations for several schemas:

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

$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