postgresql 如何在 Postgres 中获取序列名称列表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38194364/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 02:19:48  来源:igfitidea点击:

How to get list of sequence names in Postgres?

postgresql

提问by liu246437

I want to get the list of sequence names in Postgres.

我想获取 Postgres 中的序列名称列表。

In Oracle, I can use:

在 Oracle 中,我可以使用:

select sequence_name 
from user_sequences

But in Postgres when I use that statement it always throws the error: not found user_sequences.

但是在 Postgres 中,当我使用该语句时,它总是抛出错误:未找到 user_sequences

How can I do this in Postgres?

我怎样才能在 Postgres 中做到这一点?

回答by a_horse_with_no_name

You can use:

您可以使用:

select sequence_schema, sequence_name
from information_schema.sequences;

That will return a list of sequences accessibleto the current user, not the ones ownedby him.

这将返回当前用户可以访问的序列列表,而不是他拥有的序列。

If you want to list sequences ownedby the current user you need to join pg_class, pg_namespaceand pg_user:

如果要列出当前用户拥有的序列,则需要加入pg_classpg_namespace并且pg_user

select n.nspname as sequence_schema, 
       c.relname as sequence_name,
       u.usename as owner
from pg_class c 
  join pg_namespace n on n.oid = c.relnamespace
  join pg_user u on u.usesysid = c.relowner
where c.relkind = 'S'
  and u.usename = current_user;

In Postgres a user can own objects (e.g. sequences) in multiple schemas, not just "his own", so you also need to check in which schema the sequence is created.

在 Postgres 中,用户可以拥有多个模式中的对象(例如序列),而不仅仅是“他自己的”,因此您还需要检查序列是在哪个模式中创建的。

More details in the manual:

手册中的更多详细信息:

回答by DunnoHowToCode

SELECT c.relname FROM pg_class c WHERE c.relkind = 'S';
Or trying running psql as psql -U username -Efollowed by \ds. This will show you the query that was been used to generate the result as well.

SELECT c.relname FROM pg_class c WHERE c.relkind = 'S';
或者尝试运行 psqlpsql -U username -E后跟\ds. 这也将显示用于生成结果的查询。