Skip to main content
0 reactions

How to Describe a Table in PostgreSQL

Featured card for How to Describe a Table in PostgreSQL, with a terminal describing the players table: player_id integer, name text, and score with a default of 0.
How to Describe a Table in PostgreSQL

To describe a table in PostgreSQL, type \d and the table’s name in psql. It lists every column with its data type, whether it can be empty and its default value. Here’s how to read that output, and what \d+ adds.

Every command and output on this page is real, from PostgreSQL 19 on macOS.

Syntax

SyntaxPlain text
\d table_name
\d+ table_name
  • \d: psql’s describe command. Type it in psql, with no semicolon.
  • table_name: the table to describe. Without a name, \d lists the tables, views and sequences in the database instead.
  • +: adds more detail about each column, and the table’s constraints and access method.

See psql (opens in a new tab) in the PostgreSQL documentation, under \d.

The Example

An arcade keeps its players in a table: an ID that counts up by itself, a name, and a score that starts at 0.

Step 1: Create a Test Database

Connect to your server with psql as the postgres user, as Part 2 of the PostgreSQL Tutorial shows. psql connects you to the postgres database, and its prompt is postgres=#.

Create a database for the example and connect to it:

postgres=#SQL
CREATE DATABASE arcade;
OutputPlain text
CREATE DATABASE
postgres=#Plain text
\c arcade
OutputPlain text
You are now connected to database "arcade" as user "postgres".

Step 2: Create the Table

arcade=#SQL
CREATE TABLE players (player_id integer GENERATED ALWAYS AS IDENTITY, name text, score integer DEFAULT 0);
OutputPlain text
CREATE TABLE

Step 3: Describe the Table

arcade=#Plain text
\d players
OutputPlain text
                          Table "public.players"
  Column   |  Type   | Collation | Nullable |           Default
-----------+---------+-----------+----------+------------------------------
 player_id | integer |           | not null | generated always as identity
 name      | text    |           |          |
 score     | integer |           |          | 0

Each row of the output is one column of the table:

  • Column and Type: the column’s name and data type.
  • Collation: the sort order for text, shown only when it differs from the database’s own. It’s empty here.
  • Nullable: not null when the column can’t be empty. An identity column never can.
  • Default: the value a new row gets when it doesn’t give one. generated always as identity means PostgreSQL numbers the rows, and score starts at 0.

Step 4: See More Detail

arcade=#Plain text
\d+ players
OutputPlain text
                                                     Table "public.players"
  Column   |  Type   | Collation | Nullable |           Default            | Storage  | Compression | Stats target | Description
-----------+---------+-----------+----------+------------------------------+----------+-------------+--------------+-------------
 player_id | integer |           | not null | generated always as identity | plain    |             |              |
 name      | text    |           |          |                              | extended |             |              |
 score     | integer |           |          | 0                            | plain    |             |              |
Not-null constraints:
    "players_player_id_not_null" NOT NULL "player_id"
Access method: heap

\d+ adds Storage, Compression, Stats target and Description for each column. Under the columns, it lists the table’s Not-null constraints and its Access method: heap, PostgreSQL’s standard way of storing a table.

Step 5: List Everything in the Database

arcade=#Plain text
\d
OutputPlain text
                  List of relations
 Schema |         Name          |   Type   |  Owner
--------+-----------------------+----------+----------
 public | players               | table    | postgres
 public | players_player_id_seq | sequence | postgres
(2 rows)

Without a name, \d lists the tables, views and sequences. players_player_id_seq is the sequence behind the identity column: it hands out the next ID. Identity vs Serial in PostgreSQL explains identity columns.

Clean Up

To remove the example, connect to another database and drop the test database:

arcade=#Plain text
\c postgres
OutputPlain text
You are now connected to database "postgres" as user "postgres".
postgres=#SQL
DROP DATABASE arcade;
OutputPlain text
DROP DATABASE

Warning: DROP DATABASE deletes the database and everything in it, and it can’t be undone.

Common Mistakes

MistakeWhat happensDo this instead
A misspelled table nameDid not find any relation named "player".Check the name: How to List Tables in PostgreSQL shows how to list the tables.

FAQ

How do I see only the column types?

How to Check a Column’s Data Type in PostgreSQL shows a SQL query that returns each column’s name and type, which also works outside psql.

Does \d work in pgAdmin?

No, it’s a psql command. In pgAdmin, open the table in the Object Explorer to see its Columns, or select the table and open the SQL tab, which shows the statement that creates it.

Conclusion

\d and a table’s name show its columns, types, nullability and defaults. \d+ adds storage details and the table’s constraints, and \d on its own lists everything in the database.

Additional Resources

Postgres, PostgreSQL and the Slonik Logo are trademarks or registered trademarks of the PostgreSQL Community Association of Canada, and used with their permission.

0 comments