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. Get all tables under a database in Postgre

Get all tables under a database in Postgre

Scheduled Pinned Locked Moved Database
databasepostgresqltoolsquestion
17 Posts 6 Posters 39 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.
  • V Victor Nijegorodov

    I never used Postgre... In MS SQL Server you usually use something like:

    create database mydb3
    go
    use mydb3
    CREATE TABLE

    In postgresql you should use something similar... Example: [postgresql - How to switch databases in psql? - Stack Overflow](https://stackoverflow.com/questions/3949876/how-to-switch-databases-in-psql)

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

    Hello Victor. As far as I understood, "use ..." is not valid command in Postgre SQL ... I have tried that.

    V 1 Reply Last reply
    0
    • _ _Flaviu

      Hello Victor. As far as I understood, "use ..." is not valid command in Postgre SQL ... I have tried that.

      V Offline
      V Offline
      Victor Nijegorodov
      wrote on last edited by
      #6

      _Flaviu wrote:

      As far as I understood, "use ..." is not valid command in Postgre SQL

      I know. But did you try the

      \connect

      _ 1 Reply Last reply
      0
      • _ _Flaviu

        I need to get all database names, using SQL script only. I have tried:

        SELECT datname FROM pg_database WHERE datistemplate = false

        and seem to work. I got:

        mydb1
        mydb2
        postgres

        mybd1 and mydb2 have been created by me. So far, so good. Now, I need to find all tables under every database, using SQL script only. Is it possible ?

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

        Did you try the link I gave you at Re: List all tables from a specific database - Database Discussion Boards[^]?

        _ 1 Reply Last reply
        0
        • V Victor Nijegorodov

          _Flaviu wrote:

          As far as I understood, "use ..." is not valid command in Postgre SQL

          I know. But did you try the

          \connect

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

          That command is a Postgre SQL cmd line command, I cannot run it as SQL script, I have tried that. Thank you.

          C 1 Reply Last reply
          0
          • L Lost User

            Did you try the link I gave you at Re: List all tables from a specific database - Database Discussion Boards[^]?

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

            Yes, I have tried:

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

            but this script doesn't take into account a specific database. I don't understand how is relation between database and table, in Postgre SQL ...

            L 1 Reply Last reply
            0
            • _ _Flaviu

              Yes, I have tried:

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

              but this script doesn't take into account a specific database. I don't understand how is relation between database and table, in Postgre SQL ...

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

              That is not what the tutorial suggests. The summary states:

              Quote:

              Summary: in this tutorial, you will learn how to show tables in PostgreSQL using psql tool and pg_catalog schema.

              _ 1 Reply Last reply
              0
              • _ _Flaviu

                That command is a Postgre SQL cmd line command, I cannot run it as SQL script, I have tried that. Thank you.

                C Offline
                C Offline
                CHill60
                wrote on last edited by
                #11

                If you are not using psql then how are you running your SQL?

                _ 1 Reply Last reply
                0
                • C CHill60

                  If you are not using psql then how are you running your SQL?

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

                  Is about a C++ ODBC class which can run SQL scripts only.

                  V 1 Reply Last reply
                  0
                  • _ _Flaviu

                    Is about a C++ ODBC class which can run SQL scripts only.

                    V Offline
                    V Offline
                    Victor Nijegorodov
                    wrote on last edited by
                    #13

                    Could you try to set the desired database in the connection string ([PostgreSQL ODBC Driver (psqlODBC) Connection Strings - ConnectionStrings.com](https://www.connectionstrings.com/postgresql-odbc-driver-psqlodbc/))?

                    1 Reply Last reply
                    0
                    • L Lost User

                      That is not what the tutorial suggests. The summary states:

                      Quote:

                      Summary: in this tutorial, you will learn how to show tables in PostgreSQL using psql tool and pg_catalog schema.

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

                      I realize that all available tables are below existing connection, which is the default database. And the default database must be choosen at the connection time. But yes, all databases could be listed in any connection. Thank you for all your support.

                      M 1 Reply Last reply
                      0
                      • _ _Flaviu

                        I realize that all available tables are below existing connection, which is the default database. And the default database must be choosen at the connection time. But yes, all databases could be listed in any connection. Thank you for all your support.

                        M Offline
                        M Offline
                        Member_15436648
                        wrote on last edited by
                        #15

                        If you have problems with connection to database, you can try different tools. For example .net data provider for postgresql.

                        1 Reply Last reply
                        0
                        • V Victor Nijegorodov

                          Could it help you: [sql - List all tables in postgresql information_schema - Stack Overflow](https://stackoverflow.com/questions/2276644/list-all-tables-in-postgresql-information-schema) ?

                          T Offline
                          T Offline
                          Tomas PawSQL
                          wrote on last edited by
                          #16

                          SELECT TABLE_NAME, TABLE_NAME FROM INFORMATION_SCHEMA.TABLES T WHERE T.TABLE_CATALOG=$dbname AND T.TABLE_SCHEMA='public';

                          T 1 Reply Last reply
                          0
                          • T Tomas PawSQL

                            SELECT TABLE_NAME, TABLE_NAME FROM INFORMATION_SCHEMA.TABLES T WHERE T.TABLE_CATALOG=$dbname AND T.TABLE_SCHEMA='public';

                            T Offline
                            T Offline
                            Tomas PawSQL
                            wrote on last edited by
                            #17

                            Get create table statements for a database under a schema.

                            SELECT TABLE_NAME, 'CREATE TABLE '||TABLE_NAME|| ' ('||STRING_AGG(CONCAT( C1, C2, C3, C4, C5, C6 ),', ')||')' AS QUERY FROM ( SELECT C.TABLE_NAME, '"'||C.COLUMN_NAME||'"' || ' ' || CASE WHEN DATA_TYPE='ARRAY' THEN LTRIM(UDT_NAME,'_')||'[]' ELSE DATA_TYPE END AS C1, CASE WHEN CHARACTER_MAXIMUM_LENGTH > 0 THEN '(' || CHARACTER_MAXIMUM_LENGTH || ')' END AS C2, CASE WHEN NUMERIC_PRECISION > 0 AND NUMERIC_SCALE < 1 THEN NULL END AS C3, CASE WHEN NUMERIC_PRECISION > 0 AND NUMERIC_SCALE > 0 THEN NULL END AS C4, CASE WHEN IS_NULLABLE = 'NO' THEN ' NOT NULL' END AS C5, CASE WHEN COLUMN_DEFAULT IS NOT NULL AND COLUMN_DEFAULT NOT LIKE 'nextval%' THEN ' DEFAULT' END || ' ' || REPLACE(COLUMN_DEFAULT, '::CHARACTER VARYING', '') AS C6 FROM INFORMATION_SCHEMA.COLUMNS C, INFORMATION_SCHEMA.TABLES T WHERE C.TABLE_CATALOG='tpch' AND T.TABLE_CATALOG='tpch' AND T.TABLE_SCHEMA='public' AND C.TABLE_NAME=T.TABLE_NAME AND C.TABLE_SCHEMA='public' AND T.TABLE_TYPE IN ('BASE TABLE') ORDER BY C.TABLE_NAME, C.ORDINAL_POSITION ) AS STRING_COLUMNS GROUP BY TABLE_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