如何列出PostgreSQL中的模式表?

问题描述 投票:0回答:6

当我在 psql 中执行

\dt
操作时,我仅获得当前模式中的表列表(默认为
public
)。

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

postgresql command psql postgresql-9.1 dbtable
6个回答
784
投票

在所有模式中:

=> \dt *.*

在特定模式中:

=> \dt public.*

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

\dt (public|s).(s|t)
       List of relations
 Schema | Name | Type  | Owner 
--------+------+-------+-------
 public | s    | table | cpn
 public | t    | table | cpn
 s      | t    | table | cpn

高级用户可以使用正则表达式表示法(例如字符类),例如 [0-9] 来匹配任何数字。所有正则表达式特殊字符均按第 9.7.3 节中的规定工作,但如上所述,

.
被用作分隔符,
*
被转换为正则表达式符号
.*
?
为翻译为
.
,以及字面匹配的
$
。您可以根据需要模拟这些模式字符,只需为
?
编写
.
,为
(R+|)
编写
R*
,或为
(R|)
编写
R?
$
不需要作为正则表达式字符,因为模式必须与整个名称匹配,这与正则表达式的通常解释不同(换句话说,
$
会自动附加到您的模式中)。如果您不希望锚定该模式,请在开头和/或结尾写下
*
。请注意,在双引号内,所有正则表达式特殊字符都会失去其特殊含义并按字面匹配。此外,正则表达式特殊字符在运算符名称模式中按字面匹配(即
\do
的参数)。


389
投票

您可以从

information_schema

选择表格
SELECT * FROM information_schema.tables 
WHERE table_schema = 'public'

90
投票

除了

information_schema
之外,还可以使用
pg_tables
:

select * from pg_tables where schemaname='public';

15
投票

对于那些将来遇到此问题的人:

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

$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

11
投票

如果您有兴趣列出特定模式中的所有表,我发现这个答案相关:

SELECT table_schema||'.'||table_name AS full_rel_name
  FROM information_schema.tables
 WHERE table_schema = 'yourschemaname';

0
投票

之前的所有答案都涵盖了公共模式。但是,由于 Postgres 支持多种模式,您也可以在其他模式中进行查询,只需将您的模式名称替换为 public 即可。

例如:

select * from pg_tables where schemaname='your_own_schema_name';
© www.soinside.com 2019 - 2024. All rights reserved.