如何在 Postgresql 中列出当前用户拥有的所有模式中的所有表?

Pet*_*ves 27 postgresql postgresql-9.1

我可以使用列出所有模式中的所有表

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

但这也列出了远远超过我关心的表的系统表。我想要我在公共模式和我定义的任何模式中创建的所有表(可能还有视图)。

我希望找到一种方法来做到这一点,而不必在我创建它们时将模式显式添加到搜索路径中,如下所述:

/sf/answers/903144861/

编辑:

根据接受的答案,我创建了以下视图:

create view my_tables as 
select table_catalog, table_schema, table_name, table_type 
from information_schema.tables 
where table_schema not in ('pg_catalog', 'information_schema');
Run Code Online (Sandbox Code Playgroud)

现在下面的命令给了我我想要的:

select * from my_tables;
Run Code Online (Sandbox Code Playgroud)

a_h*_*ame 39

这将列出当前用户有权访问的所有表,而不仅仅是当前用户拥有的表:

select *
from information_schema.tables
where table_schema not in ('pg_catalog', 'information_schema')
and table_schema not like 'pg_toast%'
Run Code Online (Sandbox Code Playgroud)

(虽然我不完全确定not like 'pg_toast%'实际上需要它。)

我你真的需要所有者信息,你可能需要使用pg_class和相关的表。

编辑:这是包含所有者信息的查询:

select nsp.nspname as object_schema,
       cls.relname as object_name, 
       rol.rolname as owner, 
       case cls.relkind
         when 'r' then 'TABLE'
         when 'm' then 'MATERIALIZED_VIEW'
         when 'i' then 'INDEX'
         when 'S' then 'SEQUENCE'
         when 'v' then 'VIEW'
         when 'c' then 'TYPE'
         else cls.relkind::text
       end as object_type
from pg_class cls
  join pg_roles rol on rol.oid = cls.relowner
  join pg_namespace nsp on nsp.oid = cls.relnamespace
where nsp.nspname not in ('information_schema', 'pg_catalog')
  and nsp.nspname not like 'pg_toast%'
  and rol.rolname = current_user  --- remove this if you want to see all objects
order by nsp.nspname, cls.relname;
Run Code Online (Sandbox Code Playgroud)


Sah*_*sci 21

对这个问题的简短回答是:

SELECT *
FROM pg_tables t
WHERE t.tableowner = current_user;
Run Code Online (Sandbox Code Playgroud)