Skip to main content
0 reactions

How to Check a Column’s Data Type in PostgreSQL

Featured card for How to Check a Column’s Data Type in PostgreSQL, with a terminal window listing the psql describe command for plants, name: text, height_cm: integer and planted_on: date.
How to check a column’s data type in PostgreSQL.

To check a column’s data type in PostgreSQL, use psql’s \d command with the table’s name. In any other tool, ask the information_schema.columns view with a short query. Here are both, on a small example table.

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

Syntax

Syntax (psql)Plain text
\d table_name
Syntax (SQL)SQL
SELECT column_name, data_type FROM information_schema.columns WHERE table_name = 'table_name';
  • \d table_name: a psql command that describes a table, including each column’s type.
  • information_schema.columns: a view that lists the columns of every table you can see. It’s defined by the SQL standard, so the query works in any SQL tool.
  • table_name in the query is text in single quotes, written in lowercase, the way PostgreSQL stores names.

See psql (opens in a new tab) and The columns view (opens in a new tab) in the PostgreSQL documentation.

The Example

A garden keeps its plants in a table: a name, a height in centimetres and the day each was planted.

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 garden;
OutputPlain text
CREATE DATABASE
postgres=#Plain text
\c garden
OutputPlain text
You are now connected to database "garden" as user "postgres".

Create the example table:

garden=#SQL
CREATE TABLE plants (name text, height_cm integer, planted_on date);
OutputPlain text
CREATE TABLE

Step 2: Check the Types with \d

garden=#Plain text
\d plants
OutputPlain text
                 Table "public.plants"
   Column   |  Type   | Collation | Nullable | Default
------------+---------+-----------+----------+---------
 name       | text    |           |          |
 height_cm  | integer |           |          |
 planted_on | date    |           |          |

The Type column shows each column’s data type: text, integer and date.

Step 3: Check the Types with SQL

garden=#SQL
SELECT column_name, data_type FROM information_schema.columns WHERE table_name = 'plants';
OutputPlain text
 column_name | data_type
-------------+-----------
 height_cm   | integer
 planted_on  | date
 name        | text
(3 rows)

The same types, one row per column. The rows can come back in any order: without ORDER BY, the documentation (opens in a new tab) says rows come in whatever order the system finds fastest.

Clean Up

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

garden=#Plain text
\c postgres
OutputPlain text
You are now connected to database "postgres" as user "postgres".
postgres=#SQL
DROP DATABASE garden;
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
Writing the table name with capitals in the querySELECT column_name, data_type FROM information_schema.columns WHERE table_name = 'Plants'; returns (0 rows).Write it in lowercase, as PostgreSQL stores it: 'plants'.
Typing \d in a tool other than psqlIt’s a psql command, not SQL, so other tools don’t know it.Use the information_schema.columns query there.

FAQ

Is \d part of SQL?

No. psql handles every command that starts with a backslash itself. The information_schema query is SQL, so it also works in pgAdmin’s Query Tool.

Conclusion

In psql, \d and the table’s name show every column’s type. Anywhere else, query information_schema.columns with the table’s name in lowercase.

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