postgresql
  1. postgresql-describe-table

DESCRIBE TABLE - (PostgreSQL Table)

In PostgreSQL, the DESCRIBE TABLE command is used to display the structure of a table. It is used to retrieve information about the columns, data types, and constraints defined in a table. In this tutorial, we'll discuss the syntax, example, output, explanation, use, important points, and summary of DESCRIBE TABLE command in PostgreSQL.

Syntax

\d table_name
  • table_name: The name of the table that you want to describe.

Example

Let's see an example of using DESCRIBE TABLE command.

\d employees

The output of this query would be:

                                  Table "public.employees"
   Column   |  Type   | Collation | Nullable |              Default
------------+---------+-----------+----------+-----------------------------------
 id         | bigint  |           | not null | nextval('employees_id_seq'::regclass)
 first_name | text    |           | not null |
 last_name  | text    |           | not null |
 email      | text    |           | not null |
 created_at | timestamp without time zone |           | not null |
 updated_at | timestamp without time zone |           |
Indexes:
    "employees_pkey" PRIMARY KEY, btree (id)

Explanation

In this example, we used the DESCRIBE TABLE command to display the structure of the employees table. The output shows the list of columns defined in the table, along with their data types, whether they allow NULL values, and their default values (if any). It also shows the indexes defined on the table.

Use

The DESCRIBE TABLE command is used to retrieve information about the columns, data types, and indexes defined in a table. It is useful when you are working with an unfamiliar table or trying to understand the structure of a database.

Important Points

  • In PostgreSQL, the DESCRIBE TABLE command is equivalent to the \d command or SHOW CREATE TABLE command in other databases.
  • You can use the WITH OIDS option to display the object IDs (OIDs) of the columns in the table.
  • If you want to display the structure of a view, you can use the \d command followed by the name of the view.

Summary

In this tutorial, we discussed the DESCRIBE TABLE command in PostgreSQL. We covered the syntax, example, output, explanation, use, and important points of the DESCRIBE TABLE command. With this knowledge, you can now use the DESCRIBE TABLE command to retrieve information about the columns, data types, and indexes defined in a table in your PostgreSQL database.

Published on: