当我在psql中执行
我如何获取所有模式或特定模式中的所有表的列表?
\dt
命令时,我只能得到当前模式(默认为public
)中表的列表。我如何获取所有模式或特定模式中的所有表的列表?
\dt
命令时,我只能得到当前模式(默认为public
)中表的列表。在所有的模式中:
=> \dt *.*
=> \dt public.*
使用正则表达式有一些限制,但是在IT技术中是可行的。详细信息请参考PostgreSQL官方文档
\dt (public|s).(s|t)
List of relations
Schema | Name | Type | Owner
--------+------+-------+-------
public | s | table | cpn
public | t | table | cpn
s | t | table | cpn
.
为分隔符的特殊字符和*
被转换为正则表达式符号 .*
、?
被转换为.
、$
在此处的含义与通常的正则表达式解释不同,因为模式必须完全匹配名称,所以不需要作为正则表达式字符(换句话说,$将自动附加到您的模式中)。如果需要,请通过写?
来表示.
,通过(R + |)
来表示R *
,或者通过(R |)
来表示R?
从而模拟这些模式字符。如果您不希望模式是锚定的,请在开头和/或结尾处写*
。请注意,在双引号内,所有正则表达式特殊字符都失去其特殊含义,而被直接匹配。在操作符名称模式中(即\do
的参数),正则表达式特殊字符也被直接匹配。您可以从information_schema
中选择表。
SELECT * FROM information_schema.tables
WHERE table_schema = 'public'
select table_schema, table_name from information_schema.tables where table_name like '%whatever%';
如果您需要知道表位于哪个模式中。不确定您是否可以使用 \dt 进行此操作。 - Josh BrownAND table_type = 'BASE TABLE'
放到 where 子句中。 - SzieberthAdam除了information_schema
之外,还可以使用pg_tables
:
select * from pg_tables where schemaname='public';
SELECT tablename FROM pg_tables WHERE schemaname = 'public';
。 - Grant Humphriesinformation_schema
未列出 public
模式中的项目,但是 pg_tables
方法很好地工作。非常感谢! - John Crawford对于未来遇到此问题的人:
如果您想查看几个模式的关系列表:
$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
SELECT table_schema||'.'||table_name AS full_rel_name
FROM information_schema.tables
WHERE table_schema = 'yourschemaname';
AND table_type = 'BASE TABLE'
添加到 where 子句中。 - SzieberthAdamselect * from pg_tables where schemaname='your_own_schema_name';
\dt public.user_info, public.user_scope
这样? - James M. Lay\dt public.a; \dt public.b;
写在同一行更容易些。 - James M. Laysearch_path
中的任何内容,并且该默认值为"$user", public.*
。因此,set search_path=s; \dt
将列出模式s
中的所有表。 - Lukas Juhrich