列出PostgreSQL架构中的表

List tables in a PostgreSQL schema

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

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


在所有模式中:

1
=> \dt *.*

在特定模式中:

1
=> \dt public.*

可以使用带有某些限制的正则表达式

1
2
3
4
5
6
7
\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).


您可以从information_schema中选择表。

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


information_schema外,还可以使用pg_tables

1
SELECT * FROM pg_tables WHERE schemaname='public';


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

如果要查看多个架构的关系列表:

1
2
3
4
5
6
7
8
9
10
11
12
$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