Site icon JD Bots

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

Syntax

\d table_name
\d+ table_name

See psql 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=#

CREATE DATABASE arcade;

Output

CREATE DATABASE

postgres=#

\c arcade

Output

You are now connected to database "arcade" as user "postgres".

Step 2: Create the Table

arcade=#

CREATE TABLE players (player_id integer GENERATED ALWAYS AS IDENTITY, name text, score integer DEFAULT 0);

Output

CREATE TABLE

Step 3: Describe the Table

arcade=#

\d players

Output

                          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:

Step 4: See More Detail

arcade=#

\d+ players

Output

                                                     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=#

\d

Output

                  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=#

\c postgres

Output

You are now connected to database "postgres" as user "postgres".

postgres=#

DROP DATABASE arcade;

Output

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

Related

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

Exit mobile version