【发布时间】:2013-03-16 15:59:43
【问题描述】:
当我在 psql 中执行\dt 时,我只会得到当前架构中的表列表(默认为public)。
如何获取所有架构或特定架构中的所有表的列表?
【问题讨论】:
标签: postgresql postgresql-9.1 psql
当我在 psql 中执行\dt 时,我只会得到当前架构中的表列表(默认为public)。
如何获取所有架构或特定架构中的所有表的列表?
【问题讨论】:
标签: postgresql postgresql-9.1 psql
在所有模式中:
=> \dt *.*
在特定架构中:
=> \dt public.*
可以使用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
高级用户可以使用诸如字符类之类的正则表达式表示法,例如 [0-9] 来匹配任何数字。所有正则表达式特殊字符都按照第 9.7.3 节中的规定工作,除了
.被用作上面提到的分隔符,*被转换为正则表达式符号.*,?这是翻译成.和$是字面匹配的。您可以根据需要模拟这些模式字符,方法是为.编写?,为R*编写(R+|),或为R?编写(R|)。$不需要作为正则表达式字符,因为模式必须匹配整个名称,这与正则表达式的通常解释不同(换句话说,$会自动附加到您的模式中)。如果您不希望模式被锚定,请在开头和/或结尾写*。请注意,在双引号内,所有正则表达式特殊字符都失去了它们的特殊含义,并按字面意思匹配。此外,正则表达式特殊字符在运算符名称模式中按字面意思匹配(即\do的参数)。
【讨论】:
\dt 等同于\dt public.*,对吗?
\dt public.user_info, public.user_scope?
\dt public.a; \dt public.b; 更容易。
search_path 中的任何内容,而that 默认为"$user", public.*。因此,set search_path=s; \dt 将列出架构 s 中的所有表。
您可以从information_schema中选择表格
SELECT * FROM information_schema.tables
WHERE table_schema = 'public'
【讨论】:
AND table_type = 'BASE TABLE' 放在 where 子句中。
除了information_schema,还可以使用pg_tables:
select * from pg_tables where schemaname='public';
【讨论】:
SELECT tablename FROM pg_tables WHERE schemaname = 'public';
information_schema 没有列出来自public 架构的项目的权限问题,但pg_tables 方法运行良好。非常感谢!
对于那些将来遇到这种情况的人:
如果您想查看多个模式的关系列表:
$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
【讨论】:
如果您有兴趣列出 特定 架构中的所有表,我发现 this answer 相关:
SELECT table_schema||'.'||table_name AS full_rel_name
FROM information_schema.tables
WHERE table_schema = 'yourschemaname';
【讨论】:
AND table_type = 'BASE TABLE' 放在 where 子句中。