PostgreSQL: Show tables in PostgreSQL

后端 未结 24 2329
醉梦人生
醉梦人生 2020-11-28 16:58

What\'s the equivalent to show tables (from MySQL) in PostgreSQL?

相关标签:
24条回答
  • 2020-11-28 17:22

    You can use PostgreSQL's interactive terminal Psql to show tables in PostgreSQL.

    1. Start Psql

    Usually you can run the following command to enter into psql:

    psql DBNAME USERNAME
    

    For example, psql template1 postgres

    One situation you might have is: suppose you login as root, and you don't remember the database name. You can just enter first into Psql by running:

    sudo -u postgres psql
    

    In some systems, sudo command is not available, you can instead run either command below:

    psql -U postgres
    psql --username=postgres
    

    2. Show tables

    Now in Psql you could run commands such as:

    1. \? list all the commands
    2. \l list databases
    3. \conninfo display information about current connection
    4. \c [DBNAME] connect to new database, e.g., \c template1
    5. \dt list tables of the public schema
    6. \dt <schema-name>.* list tables of certain schema, e.g., \dt public.*
    7. \dt *.* list tables of all schemas
    8. Then you can run SQL statements, e.g., SELECT * FROM my_table;(Note: a statement must be terminated with semicolon ;)
    9. \q quit psql
    0 讨论(0)
  • 2020-11-28 17:22

    If you only want to see the list of tables you've created, you may only say:

    \dt

    But we also have PATTERN which will help you customize which tables to show. To show all including pg_catalog Schema, you can add *.

    \dt *

    If you do: \?

    \dt[S+] [PATTERN] list tables

    0 讨论(0)
  • 2020-11-28 17:22

    If you are using pgAdmin4 in PostgreSQL, you can use this to show the tables in your database:

    select * from information_schema.tables where table_schema='public';
    
    0 讨论(0)
  • 2020-11-28 17:23

    Login as a superuser so that you can check all the databases and their schemas:-

    sudo su - postgres
    

    Then we can get to postgresql shell by using following command:-

    psql
    

    You can now check all the databases list by using the following command:-

    \l
    

    If you would like to check the sizes of the databases as well use:-

    \l+
    

    Press q to go back.

    Once you have found your database now you can connect to that database using the following command:-

    \c database_name
    

    Once connected you can check the database tables or schema by:-

    \d
    

    Now to return back to the shell use:-

    q
    

    Now to further see the details of a certain table use:-

    \d table_name
    

    To go back to postgresql_shell press \q.

    And to return back to terminal press exit.

    0 讨论(0)
  • 2020-11-28 17:23

    To view foreign tables in psql, run \dE

    0 讨论(0)
  • 2020-11-28 17:28

    \dt will list tables, and "\pset pager off" shows them in the same window, without switching to a separate one. Love that feature to death in dbshell.

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