How to get list of sequence names in Postgres?

前端 未结 2 2054
醉话见心
醉话见心 2021-02-14 14:07

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

In Oracle, I can use:

select sequence_name 
from user_sequences

But in Postgres w

相关标签:
2条回答
  • 2021-02-14 14:46

    You can use:

    select sequence_schema, sequence_name
    from information_schema.sequences;
    

    That will return a list of sequences accessible to the current user, not the ones owned by him.

    If you want to list sequences owned by the current user you need to join pg_class, pg_namespace and 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.

    More details in the manual:

    • https://www.postgresql.org/docs/current/static/infoschema-sequences.html
    • https://www.postgresql.org/docs/current/static/catalog-pg-class.html
    0 讨论(0)
  • 2021-02-14 14:52

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

    0 讨论(0)
提交回复
热议问题