PostgreSQL: Show tables in PostgreSQL

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

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

相关标签:
24条回答
  • 2020-11-28 17:30
    1. First login as postgres user:

      sudo su - postgres

    2. connect to the required db: psql -d databaseName

    3. \dt would return the list of all table in the database you're connected to.

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

    Login as superuser:

    sudo -u postgres psql
    

    You can list all databases and users by \l command, (list other commands by \?).

    Now if you want to see other databases you can change user/database by \c command like \c template1, \c postgres postgres and use \d, \dt or \dS to see tables/views/etc.

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

    Note that \dt alone will list tables in the public schema of the database you're using. I like to keep my tables in separate schemas, so the accepted answer didn't work for me.

    To list all tables within a specific schema, I needed to:

    1) Connect to the desired database:

    psql mydb
    

    2) Specify the schema name I want to see tables for after the \dt command, like this:

    \dt myschema.*
    

    This shows me the results I'm interested in:

                   List of relations
     Schema   |       Name      | Type  |  Owner   
    ----------+-----------------+-------+----------
     myschema | users           | table | postgres
     myschema | activity        | table | postgres
     myschema | roles           | table | postgres
    
    0 讨论(0)
  • 2020-11-28 17:32

    The most straightforward way to list all tables at command line is, for my taste :

    psql -a -U <user> -p <port> -h <server> -c "\dt"
    

    For a given database just add the database name :

    psql -a -U <user> -p <port> -h <server> -c "\dt" <database_name>
    

    It works on both Linux and Windows.

    0 讨论(0)
  • 2020-11-28 17:32
    1. In PostgreSQL command-line interface after login, type the following command to connect with the desired database.

          \c [database_name]
      

    Then you will see this message You are now connected to database "[database_name]"

    1. Type the following command to list all the tables.

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

    From the psql command line interface,

    First, choose your database

    \c database_name
    

    Then, this shows all tables in the current schema:

    \dt
    

    Programmatically (or from the psql interface too, of course):

    SELECT * FROM pg_catalog.pg_tables;
    

    The system tables live in the pg_catalog database.

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