列出PostgreSQL架构中的表

Nyx*_*nyx 300 postgresql postgresql-9.1 psql

当我\dt在psql中执行一个操作时,我只获得当前模式中的表列表(public默认情况下).

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

Clo*_*eto 458

在所有模式中:

=> \dt *.*
Run Code Online (Sandbox Code Playgroud)

在特定架构中:

=> \dt public.*
Run Code Online (Sandbox Code Playgroud)

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

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

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

  • 简单地说`\ dt`等同于`\ dt public.*`,我是对的吗? (5认同)
  • @FrozenFlame 不是的!默认情况下,它显示“search_path”中的所有内容,并且默认为““$user”,public.*”。因此,`set search_path=s; \dt` 将列出模式 `s` 中的所有表。 (3认同)

Jak*_*nia 234

您可以从中选择表格 information_schema

SELECT * FROM information_schema.tables 
WHERE table_schema = 'public'
Run Code Online (Sandbox Code Playgroud)

  • 如果您的界面不支持快捷方式,则非常有用.谢谢. (5认同)
  • 要排除视图,请将“AND table_type = 'BASE TABLE'”放入 where 子句中。 (3认同)
  • 谢谢,它可以在Amazon Redshift上使用,而\ dt(可接受的答案)不能使用。 (2认同)
  • 这是最普遍有用的答案。information_schema在SQL标准中定义,并且在符合标准的大多数数据库中都可用 (2认同)

Rad*_*icz 45

或者information_schema可以使用pg_tables:

select * from pg_tables where schemaname='public';
Run Code Online (Sandbox Code Playgroud)

  • 请注意,如果您只希望表名是结果查询,那么它是`SELECT tablename FROM pg_tables WHERE schemaname ='public';` (3认同)

lin*_*nog 11

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

SELECT table_schema||'.'||table_name AS full_rel_name
  FROM information_schema.tables
 WHERE table_schema = 'yourschemaname';
Run Code Online (Sandbox Code Playgroud)

  • 要排除视图,请将“AND table_type = 'BASE TABLE'”放入 where 子句中。 (2认同)

小智 7

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

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

$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
Run Code Online (Sandbox Code Playgroud)