Search code examples
postgresqlpsql

Describe table with sorted columns


I want to describe postgres table using psql. I read this article and now I can use this structure

\d+ table_name

With this function I could display all columns and indexes related to table. But is it possible to show all table columns with specified order ? For Example I want to display columns sorted by name in desc order


Solution

  • You can use:

    SELECT * FROM information_schema.columns 
    WHERE table_name = 'your_table' 
    ORDER BY column_name DESC;
    

    As the documentation says:

    The information schema consists of a set of views that contain information about the objects defined in the current database.

    In this columns table you will find more information as

    • ordinal position of the column within the table
    • column default
    • data type
    • if the column is nullable
    • scale e precision for numeric fields
    • limit of length for character fields
    • ....

    Here for a complete list