Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. List all tables from a specific database

List all tables from a specific database

Scheduled Pinned Locked Moved Database
databasequestionpostgresql
13 Posts 5 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • _ Offline
    _ Offline
    _Flaviu
    wrote on last edited by
    #1

    How can I list all tables from a specific database in PostGre SQL ? Lets say I have 3 databases, and I have several tables in every one each of them. How can I retrieve the table names for every database I have, using SQL command ? With following command:

    SELECT datname FROM pg_database WHERE datistemplate = false

    I got:
    db_name
    post_flaviu
    postgres
    test

    Now, how can I find all tables under eevry db from above ?

    P K S 3 Replies Last reply
    0
    • _ _Flaviu

      How can I list all tables from a specific database in PostGre SQL ? Lets say I have 3 databases, and I have several tables in every one each of them. How can I retrieve the table names for every database I have, using SQL command ? With following command:

      SELECT datname FROM pg_database WHERE datistemplate = false

      I got:
      db_name
      post_flaviu
      postgres
      test

      Now, how can I find all tables under eevry db from above ?

      P Offline
      P Offline
      Peter_in_2780
      wrote on last edited by
      #2

      Feeding "postgresql list tables in database" into your search engine will produce a zillion useful answers.

      Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012

      _ 1 Reply Last reply
      0
      • P Peter_in_2780

        Feeding "postgresql list tables in database" into your search engine will produce a zillion useful answers.

        Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012

        _ Offline
        _ Offline
        _Flaviu
        wrote on last edited by
        #3

        Yes, I did. But I haven't found any SQL to tell me the tables for a specific database only.

        1 Reply Last reply
        0
        • _ _Flaviu

          How can I list all tables from a specific database in PostGre SQL ? Lets say I have 3 databases, and I have several tables in every one each of them. How can I retrieve the table names for every database I have, using SQL command ? With following command:

          SELECT datname FROM pg_database WHERE datistemplate = false

          I got:
          db_name
          post_flaviu
          postgres
          test

          Now, how can I find all tables under eevry db from above ?

          K Offline
          K Offline
          k5054
          wrote on last edited by
          #4

          You can get Postgres to tell you how to do that using the -E (display hidden queries) option to psql. So for example

          $ psql -E -l
          ********* QUERY **********
          SELECT d.datname as "Name",
          pg_catalog.pg_get_userbyid(d.datdba) as "Owner",
          pg_catalog.pg_encoding_to_char(d.encoding) as "Encoding",
          d.datcollate as "Collate",
          d.datctype as "Ctype",
          pg_catalog.array_to_string(d.datacl, E'\n') AS "Access privileges"
          FROM pg_catalog.pg_database d
          ORDER BY 1;
          **************************

                                        List of databases
          

          Name | Owner | Encoding | Collate | Ctype | Access privileges
          -----------+----------+-----------+-------------+-------------+-----------------------
          template0 | postgres | UTF8 | en_CA.UTF-8 | en_CA.UTF-8 | =c/postgres +
          | | | | | postgres=CTc/postgres
          template1 | postgres | UTF8 | en_CA.UTF-8 | en_CA.UTF-8 | =c/postgres +
          | | | | | postgres=CTc/postgres
          test | ebacon | UTF8 | en_CA.UTF-8 | en_CA.UTF-8 |
          (3 rows)

          Likewise for the \dt command inside psql. Then all you need to do is to figure out how to combine both queries into one. Keep Calm and Carry On

          _ 1 Reply Last reply
          0
          • K k5054

            You can get Postgres to tell you how to do that using the -E (display hidden queries) option to psql. So for example

            $ psql -E -l
            ********* QUERY **********
            SELECT d.datname as "Name",
            pg_catalog.pg_get_userbyid(d.datdba) as "Owner",
            pg_catalog.pg_encoding_to_char(d.encoding) as "Encoding",
            d.datcollate as "Collate",
            d.datctype as "Ctype",
            pg_catalog.array_to_string(d.datacl, E'\n') AS "Access privileges"
            FROM pg_catalog.pg_database d
            ORDER BY 1;
            **************************

                                          List of databases
            

            Name | Owner | Encoding | Collate | Ctype | Access privileges
            -----------+----------+-----------+-------------+-------------+-----------------------
            template0 | postgres | UTF8 | en_CA.UTF-8 | en_CA.UTF-8 | =c/postgres +
            | | | | | postgres=CTc/postgres
            template1 | postgres | UTF8 | en_CA.UTF-8 | en_CA.UTF-8 | =c/postgres +
            | | | | | postgres=CTc/postgres
            test | ebacon | UTF8 | en_CA.UTF-8 | en_CA.UTF-8 |
            (3 rows)

            Likewise for the \dt command inside psql. Then all you need to do is to figure out how to combine both queries into one. Keep Calm and Carry On

            _ Offline
            _ Offline
            _Flaviu
            wrote on last edited by
            #5

            Thank you. That query retrieve all databases. And for all tables from a database only ?

            K 1 Reply Last reply
            0
            • _ _Flaviu

              Thank you. That query retrieve all databases. And for all tables from a database only ?

              K Offline
              K Offline
              k5054
              wrote on last edited by
              #6

              try psql -E test -c "\dt". You can then take that output and combine with the previous one to get a listing of all tables across all databases. Keep Calm and Carry On

              _ 1 Reply Last reply
              0
              • K k5054

                try psql -E test -c "\dt". You can then take that output and combine with the previous one to get a listing of all tables across all databases. Keep Calm and Carry On

                _ Offline
                _ Offline
                _Flaviu
                wrote on last edited by
                #7

                "get a listing of all tables across all databases" That I already did. But doesn't help me, because I need the tables below a specific database, not all tables from all databases.

                L 1 Reply Last reply
                0
                • _ _Flaviu

                  "get a listing of all tables across all databases" That I already did. But doesn't help me, because I need the tables below a specific database, not all tables from all databases.

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  PostgreSQL Show Tables[^]

                  _ 1 Reply Last reply
                  0
                  • L Lost User

                    PostgreSQL Show Tables[^]

                    _ Offline
                    _ Offline
                    _Flaviu
                    wrote on last edited by
                    #9

                    Thank you Richard. I have tried before, and now, the SQL from that page:

                    SELECT * FROM pg_catalog.pg_tables WHERE schemaname != 'pg_catalog' AND schemaname != 'information_schema';

                    And as result I got all tables, from ALL databases. Untitled6 — ImgBB[^]

                    L 1 Reply Last reply
                    0
                    • _ _Flaviu

                      Thank you Richard. I have tried before, and now, the SQL from that page:

                      SELECT * FROM pg_catalog.pg_tables WHERE schemaname != 'pg_catalog' AND schemaname != 'information_schema';

                      And as result I got all tables, from ALL databases. Untitled6 — ImgBB[^]

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #10

                      But that is exactly what that command is supposed to do. You need to follow the instructions from the top of the page.

                      _ 1 Reply Last reply
                      0
                      • L Lost User

                        But that is exactly what that command is supposed to do. You need to follow the instructions from the top of the page.

                        _ Offline
                        _ Offline
                        _Flaviu
                        wrote on last edited by
                        #11

                        I am pretty close. And what SQL command should I use to select the database, and after that to call SELECT tablename FROM pg_catalog.pg_tables WHERE schemaname != 'pg_catalog' AND schemaname != 'information_schema' ? I have tried:

                        USE database my_database

                        but seem to not work:

                        ERROR: syntax error at or near "USE";
                        Error while executing the query

                        You know, I need to all this programatically, using SQL commands, not using Postgre tools.

                        L 1 Reply Last reply
                        0
                        • _ _Flaviu

                          I am pretty close. And what SQL command should I use to select the database, and after that to call SELECT tablename FROM pg_catalog.pg_tables WHERE schemaname != 'pg_catalog' AND schemaname != 'information_schema' ? I have tried:

                          USE database my_database

                          but seem to not work:

                          ERROR: syntax error at or near "USE";
                          Error while executing the query

                          You know, I need to all this programatically, using SQL commands, not using Postgre tools.

                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #12

                          Sorry, I do not have a system available to test this. However, I do not see any instruction concerning a USE statement in the tutorial on that page.

                          1 Reply Last reply
                          0
                          • _ _Flaviu

                            How can I list all tables from a specific database in PostGre SQL ? Lets say I have 3 databases, and I have several tables in every one each of them. How can I retrieve the table names for every database I have, using SQL command ? With following command:

                            SELECT datname FROM pg_database WHERE datistemplate = false

                            I got:
                            db_name
                            post_flaviu
                            postgres
                            test

                            Now, how can I find all tables under eevry db from above ?

                            S Offline
                            S Offline
                            Sakshi Jain 2022
                            wrote on last edited by
                            #13

                            1. Using SQL Query To show the list of tables with the corresponding schema name, run this statement:

                            SELECT * FROM information_schema.tables;

                            or in a particular schema:

                            SELECT * FROM information_schema.tables WHERE table_schema = 'schema_name';

                            2. Using psql To list all tables: In all schemas:

                            \dt *.*

                            In a particular schema:

                            \dt schema_name.*

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups