Postgres supports various commands and queries to list all columns of a table in your Postgres database, such as the “\d” or “\d+” commands, “information_schema”, etc. You can also use pgAdmin to access and display all columns of any Postgres table. This post demonstrates how to list all columns of a specific table using CLI (SQL Shell) and GUI (pgAdmin).
The content of this Postgres post is organized as follows:
- Method 1: Using “\d” or “\d+” Command
- Method 2: Using “information_schema”
- Method 3: Using pg_Admin
Method 1: Using “\d” or “\d+” Command
The “\d” command is used to describe the tables of a database. While the “\d+” command describes the Postgres tables in detail. You can use the “\d” or “\d+” command followed by the table name to get the list of all columns of the specific table. For instance, the below command will show the column names of a user-defined table named “staff_info”:
\d staff_info;
The output shows that the “\d” command retrieves the column names of the “staff_info” table.
Method 2: Using information_schema
Alternatively, you can use the “information_schema” with the help of the “SELECT” statement to get the column names of a specific table:
SELECT column_name, data_type FROM information_schema.columns WHERE table_schema = 'public' AND table_name = 'staff_info';
The above snippet shows that the “information_schema” retrieves all the columns of the “staff_info” table.
Method 3: Using pg_Admin
You can also list all columns of a specific table using GUI/pgAdmin. To do that, open the pgAdmin, provide the login information, navigate to the “Databases” tab and click on the database of your choice to expand it:
Now click on “Schemas” and select the schema of your choice; by default, your tables are stored in the “public” schema. So, click on the “public” schema and then click on the “Tables” to see the available tables:
Now, click on the targeted table, and then select the “columns” tab to expand it:
Finally, you can see all the column names of the selected table under the “Columns” tab.
Conclusion
In PostgreSQL, the “\d” command, “\d+” command, “information_schema”, and “pgAdmin” are used to list all columns of a table. You can use the “\d” or “\d+” command followed by the table name to get the list of all columns of the specific table along with some other necessary details. While you can use the “information_schema” to get all the information of the selected table, such as column names, data types, constraints, etc. This post explained how to list all columns of a Postgres table using practical demonstration.