列出PostgreSQL模式中的表

在psql中,默认情况下,`ls`只会显示当前模式的表列表。要获取所有架构或特定架构的表列表,可以使用SQL查询。在所有架构中,可以使用`SELECT * FROM pg_catalog.pg_tables WHERE schemaname != 'pg_catalog' AND schemaname != 'information_schema';`。在特定模式中,只需将条件改为schemaname='your_schema_name'。高级用户还可以利用正则表达式进行更复杂的过滤。
摘要由CSDN通过智能技术生成

本文翻译自:List tables in a PostgreSQL schema

When I do a \\dt in psql I only get a listing of tables in the current schema ( public by default). 当我在psql中执行\\dt ,我只会得到当前模式中的表列表(默认情况下为public )。

How can I get a list of all tables in all schemas or a particular schema? 如何获得所有架构或特定架构中所有表的列表?


#1楼

参考:https://stackoom.com/question/13dl2/列出PostgreSQL模式中的表


#2楼

You can select the tables from information_schema 您可以从information_schema选择表

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

#3楼

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. 高级用户可以使用正则表达式表示法,例如字符类,例如[0-9]来匹配任何数字。 All regular expression special characters work as specified in Section 9.7.3, except for . 除之外,所有正则表达式特殊字符均按9.7.3节中的规定工作. 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? ,对于R* (R+|)还是对于R? (R|) 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 (ie, the argument of \\do ). 同样,正则表达式特殊字符在运算符名称模式(即\\do的参数)中按字面值进行匹配。


#4楼

除了information_schema ,还可以使用pg_tables

select * from pg_tables where schemaname='public';

#5楼

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值