How do I list all tables in all schemas owned by the current user in Postgresql?












20















I can list all tables in all schemas using



> dt *.*


but that also lists system tables that greatly outnumber my tables that I care about. I'd like all the tables (and possibly views) created by me in the public schema and any schemas I've defined.



I'm hoping to find a way to do this without having to explicitly add schemas to the search path as I create them as described here:



https://stackoverflow.com/a/12902069



EDIT:



Based on the accepted answer, I've created the following View:



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');


And now the following command gives me what I wanted:



select * from my_tables;









share|improve this question





























    20















    I can list all tables in all schemas using



    > dt *.*


    but that also lists system tables that greatly outnumber my tables that I care about. I'd like all the tables (and possibly views) created by me in the public schema and any schemas I've defined.



    I'm hoping to find a way to do this without having to explicitly add schemas to the search path as I create them as described here:



    https://stackoverflow.com/a/12902069



    EDIT:



    Based on the accepted answer, I've created the following View:



    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');


    And now the following command gives me what I wanted:



    select * from my_tables;









    share|improve this question



























      20












      20








      20


      4






      I can list all tables in all schemas using



      > dt *.*


      but that also lists system tables that greatly outnumber my tables that I care about. I'd like all the tables (and possibly views) created by me in the public schema and any schemas I've defined.



      I'm hoping to find a way to do this without having to explicitly add schemas to the search path as I create them as described here:



      https://stackoverflow.com/a/12902069



      EDIT:



      Based on the accepted answer, I've created the following View:



      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');


      And now the following command gives me what I wanted:



      select * from my_tables;









      share|improve this question
















      I can list all tables in all schemas using



      > dt *.*


      but that also lists system tables that greatly outnumber my tables that I care about. I'd like all the tables (and possibly views) created by me in the public schema and any schemas I've defined.



      I'm hoping to find a way to do this without having to explicitly add schemas to the search path as I create them as described here:



      https://stackoverflow.com/a/12902069



      EDIT:



      Based on the accepted answer, I've created the following View:



      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');


      And now the following command gives me what I wanted:



      select * from my_tables;






      postgresql postgresql-9.1






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited May 23 '17 at 12:40









      Community

      1




      1










      asked Dec 9 '12 at 17:42









      Peter GrovesPeter Groves

      355247




      355247






















          4 Answers
          4






          active

          oldest

          votes


















          27














          This will list all tables the current user has access to, not only those that are owned by the current user:



          select *
          from information_schema.tables
          where table_schema not in ('pg_catalog', 'information_schema')
          and table_schema not like 'pg_toast%'


          (I'm not entirely sure the not like 'pg_toast%' is actually needed though.)



          I you really need the owner information, you probably need to use pg_class and related tables.



          Edit: this is the query that includes the owner information:



          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;





          share|improve this answer


























          • This is good enough. I'm going to create a View called my_tables from this.

            – Peter Groves
            Dec 9 '12 at 20:24











          • Great answer, add a when 'm' then 'MATERIALIZED_VIEW' to show that new type.

            – Forbesmyester
            Apr 16 '18 at 9:26











          • While another answer is concise, this might be relevant when excluding namespaces.

            – mlt
            Feb 9 at 2:29



















          13














          The short answer to the question would be:



          SELECT *
          FROM pg_tables t
          WHERE t.tableowner = current_user;





          share|improve this answer

































            0














            What is the best alternative in PostgreSQL for User_source in Oracle?





            share








            New contributor




            vidhya sundari is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.




























              -3














              See this. All tables:



              SELECT relname FROM pg_class WHERE relname !~ '^(pg_|sql_)' AND relkind = 'r';





              share|improve this answer

























                Your Answer








                StackExchange.ready(function() {
                var channelOptions = {
                tags: "".split(" "),
                id: "182"
                };
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function() {
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled) {
                StackExchange.using("snippets", function() {
                createEditor();
                });
                }
                else {
                createEditor();
                }
                });

                function createEditor() {
                StackExchange.prepareEditor({
                heartbeatType: 'answer',
                autoActivateHeartbeat: false,
                convertImagesToLinks: false,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: null,
                bindNavPrevention: true,
                postfix: "",
                imageUploader: {
                brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                allowUrls: true
                },
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                });


                }
                });














                draft saved

                draft discarded


















                StackExchange.ready(
                function () {
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f30061%2fhow-do-i-list-all-tables-in-all-schemas-owned-by-the-current-user-in-postgresql%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                4 Answers
                4






                active

                oldest

                votes








                4 Answers
                4






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                27














                This will list all tables the current user has access to, not only those that are owned by the current user:



                select *
                from information_schema.tables
                where table_schema not in ('pg_catalog', 'information_schema')
                and table_schema not like 'pg_toast%'


                (I'm not entirely sure the not like 'pg_toast%' is actually needed though.)



                I you really need the owner information, you probably need to use pg_class and related tables.



                Edit: this is the query that includes the owner information:



                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;





                share|improve this answer


























                • This is good enough. I'm going to create a View called my_tables from this.

                  – Peter Groves
                  Dec 9 '12 at 20:24











                • Great answer, add a when 'm' then 'MATERIALIZED_VIEW' to show that new type.

                  – Forbesmyester
                  Apr 16 '18 at 9:26











                • While another answer is concise, this might be relevant when excluding namespaces.

                  – mlt
                  Feb 9 at 2:29
















                27














                This will list all tables the current user has access to, not only those that are owned by the current user:



                select *
                from information_schema.tables
                where table_schema not in ('pg_catalog', 'information_schema')
                and table_schema not like 'pg_toast%'


                (I'm not entirely sure the not like 'pg_toast%' is actually needed though.)



                I you really need the owner information, you probably need to use pg_class and related tables.



                Edit: this is the query that includes the owner information:



                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;





                share|improve this answer


























                • This is good enough. I'm going to create a View called my_tables from this.

                  – Peter Groves
                  Dec 9 '12 at 20:24











                • Great answer, add a when 'm' then 'MATERIALIZED_VIEW' to show that new type.

                  – Forbesmyester
                  Apr 16 '18 at 9:26











                • While another answer is concise, this might be relevant when excluding namespaces.

                  – mlt
                  Feb 9 at 2:29














                27












                27








                27







                This will list all tables the current user has access to, not only those that are owned by the current user:



                select *
                from information_schema.tables
                where table_schema not in ('pg_catalog', 'information_schema')
                and table_schema not like 'pg_toast%'


                (I'm not entirely sure the not like 'pg_toast%' is actually needed though.)



                I you really need the owner information, you probably need to use pg_class and related tables.



                Edit: this is the query that includes the owner information:



                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;





                share|improve this answer















                This will list all tables the current user has access to, not only those that are owned by the current user:



                select *
                from information_schema.tables
                where table_schema not in ('pg_catalog', 'information_schema')
                and table_schema not like 'pg_toast%'


                (I'm not entirely sure the not like 'pg_toast%' is actually needed though.)



                I you really need the owner information, you probably need to use pg_class and related tables.



                Edit: this is the query that includes the owner information:



                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;






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Apr 16 '18 at 9:25









                Community

                1




                1










                answered Dec 9 '12 at 18:02









                a_horse_with_no_namea_horse_with_no_name

                39.6k775112




                39.6k775112













                • This is good enough. I'm going to create a View called my_tables from this.

                  – Peter Groves
                  Dec 9 '12 at 20:24











                • Great answer, add a when 'm' then 'MATERIALIZED_VIEW' to show that new type.

                  – Forbesmyester
                  Apr 16 '18 at 9:26











                • While another answer is concise, this might be relevant when excluding namespaces.

                  – mlt
                  Feb 9 at 2:29



















                • This is good enough. I'm going to create a View called my_tables from this.

                  – Peter Groves
                  Dec 9 '12 at 20:24











                • Great answer, add a when 'm' then 'MATERIALIZED_VIEW' to show that new type.

                  – Forbesmyester
                  Apr 16 '18 at 9:26











                • While another answer is concise, this might be relevant when excluding namespaces.

                  – mlt
                  Feb 9 at 2:29

















                This is good enough. I'm going to create a View called my_tables from this.

                – Peter Groves
                Dec 9 '12 at 20:24





                This is good enough. I'm going to create a View called my_tables from this.

                – Peter Groves
                Dec 9 '12 at 20:24













                Great answer, add a when 'm' then 'MATERIALIZED_VIEW' to show that new type.

                – Forbesmyester
                Apr 16 '18 at 9:26





                Great answer, add a when 'm' then 'MATERIALIZED_VIEW' to show that new type.

                – Forbesmyester
                Apr 16 '18 at 9:26













                While another answer is concise, this might be relevant when excluding namespaces.

                – mlt
                Feb 9 at 2:29





                While another answer is concise, this might be relevant when excluding namespaces.

                – mlt
                Feb 9 at 2:29













                13














                The short answer to the question would be:



                SELECT *
                FROM pg_tables t
                WHERE t.tableowner = current_user;





                share|improve this answer






























                  13














                  The short answer to the question would be:



                  SELECT *
                  FROM pg_tables t
                  WHERE t.tableowner = current_user;





                  share|improve this answer




























                    13












                    13








                    13







                    The short answer to the question would be:



                    SELECT *
                    FROM pg_tables t
                    WHERE t.tableowner = current_user;





                    share|improve this answer















                    The short answer to the question would be:



                    SELECT *
                    FROM pg_tables t
                    WHERE t.tableowner = current_user;






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Aug 4 '15 at 13:14









                    dezso

                    22.1k116096




                    22.1k116096










                    answered Aug 4 '15 at 13:08









                    Sahap AsciSahap Asci

                    1,188412




                    1,188412























                        0














                        What is the best alternative in PostgreSQL for User_source in Oracle?





                        share








                        New contributor




                        vidhya sundari is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                        Check out our Code of Conduct.

























                          0














                          What is the best alternative in PostgreSQL for User_source in Oracle?





                          share








                          New contributor




                          vidhya sundari is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.























                            0












                            0








                            0







                            What is the best alternative in PostgreSQL for User_source in Oracle?





                            share








                            New contributor




                            vidhya sundari is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                            Check out our Code of Conduct.










                            What is the best alternative in PostgreSQL for User_source in Oracle?






                            share








                            New contributor




                            vidhya sundari is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                            Check out our Code of Conduct.








                            share


                            share






                            New contributor




                            vidhya sundari is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                            Check out our Code of Conduct.









                            answered 9 mins ago









                            vidhya sundarividhya sundari

                            1




                            1




                            New contributor




                            vidhya sundari is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                            Check out our Code of Conduct.





                            New contributor





                            vidhya sundari is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                            Check out our Code of Conduct.






                            vidhya sundari is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                            Check out our Code of Conduct.























                                -3














                                See this. All tables:



                                SELECT relname FROM pg_class WHERE relname !~ '^(pg_|sql_)' AND relkind = 'r';





                                share|improve this answer






























                                  -3














                                  See this. All tables:



                                  SELECT relname FROM pg_class WHERE relname !~ '^(pg_|sql_)' AND relkind = 'r';





                                  share|improve this answer




























                                    -3












                                    -3








                                    -3







                                    See this. All tables:



                                    SELECT relname FROM pg_class WHERE relname !~ '^(pg_|sql_)' AND relkind = 'r';





                                    share|improve this answer















                                    See this. All tables:



                                    SELECT relname FROM pg_class WHERE relname !~ '^(pg_|sql_)' AND relkind = 'r';






                                    share|improve this answer














                                    share|improve this answer



                                    share|improve this answer








                                    edited Aug 31 '16 at 5:13









                                    Paul White

                                    51.3k14278450




                                    51.3k14278450










                                    answered Aug 31 '16 at 0:20









                                    ribafsribafs

                                    1




                                    1






























                                        draft saved

                                        draft discarded




















































                                        Thanks for contributing an answer to Database Administrators Stack Exchange!


                                        • Please be sure to answer the question. Provide details and share your research!

                                        But avoid



                                        • Asking for help, clarification, or responding to other answers.

                                        • Making statements based on opinion; back them up with references or personal experience.


                                        To learn more, see our tips on writing great answers.




                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function () {
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f30061%2fhow-do-i-list-all-tables-in-all-schemas-owned-by-the-current-user-in-postgresql%23new-answer', 'question_page');
                                        }
                                        );

                                        Post as a guest















                                        Required, but never shown





















































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown

































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown







                                        Popular posts from this blog

                                        Liste der Baudenkmale in Friedland (Mecklenburg)

                                        Single-Malt-Whisky

                                        Czorneboh