Site icon JD Bots

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)

\d table_name

Syntax (SQL)

SELECT column_name, data_type FROM information_schema.columns WHERE table_name = 'table_name';

See psql and The columns view 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=#

CREATE DATABASE garden;

Output

CREATE DATABASE

postgres=#

\c garden

Output

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

Create the example table:

garden=#

CREATE TABLE plants (name text, height_cm integer, planted_on date);

Output

CREATE TABLE

Step 2: Check the Types with \d

garden=#

\d plants

Output

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

SELECT column_name, data_type FROM information_schema.columns WHERE table_name = 'plants';

Output

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

\c postgres

Output

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

postgres=#

DROP DATABASE garden;

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

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