3 Ways to Check a Column’s Data Type in PostgreSQL

Here are three ways to get the data type of a given column in MariaDB.

The \d Command

In psql, the \d command shows information about tables, views, materialised views, index, sequences, or foreign tables.

We can use this command to check the data type of the columns in a given table:

\d public.actor

Result:

                                             Table "public.actor"
+-------------+-----------------------------+-----------+----------+-----------------------------------------+
|   Column    |            Type             | Collation | Nullable |                 Default                 |
+-------------+-----------------------------+-----------+----------+-----------------------------------------+
| actor_id    | integer                     |           | not null | nextval('actor_actor_id_seq'::regclass) |
| first_name  | character varying(45)       |           | not null |                                         |
| last_name   | character varying(45)       |           | not null |                                         |
| last_update | timestamp without time zone |           | not null | now()                                   |
+-------------+-----------------------------+-----------+----------+-----------------------------------------+
Indexes:
    "actor_pkey" PRIMARY KEY, btree (actor_id)
    "idx_actor_last_name" btree (last_name)
Referenced by:
    TABLE "film_actor" CONSTRAINT "film_actor_actor_id_fkey" FOREIGN KEY (actor_id) REFERENCES actor(actor_id) ON UPDATE CASCADE ON DELETE RESTRICT
Triggers:
    last_updated BEFORE UPDATE ON actor FOR EACH ROW EXECUTE FUNCTION last_updated()

We can append a plus sign (+) to reveal extended information:

\d+ public.actor

Result:

                                                                 Table "public.actor"
+-------------+-----------------------------+-----------+----------+-----------------------------------------+----------+--------------+-------------+
|   Column    |            Type             | Collation | Nullable |                 Default                 | Storage  | Stats target | Description |
+-------------+-----------------------------+-----------+----------+-----------------------------------------+----------+--------------+-------------+
| actor_id    | integer                     |           | not null | nextval('actor_actor_id_seq'::regclass) | plain    |              |             |
| first_name  | character varying(45)       |           | not null |                                         | extended |              |             |
| last_name   | character varying(45)       |           | not null |                                         | extended |              |             |
| last_update | timestamp without time zone |           | not null | now()                                   | plain    |              |             |
+-------------+-----------------------------+-----------+----------+-----------------------------------------+----------+--------------+-------------+
Indexes:
    "actor_pkey" PRIMARY KEY, btree (actor_id)
    "idx_actor_last_name" btree (last_name)
Referenced by:
    TABLE "film_actor" CONSTRAINT "film_actor_actor_id_fkey" FOREIGN KEY (actor_id) REFERENCES actor(actor_id) ON UPDATE CASCADE ON DELETE RESTRICT
Triggers:
    last_updated BEFORE UPDATE ON actor FOR EACH ROW EXECUTE FUNCTION last_updated()
Access method: heap

The information_schema.columns View

The information_schema.columns view contains information about columns:

SELECT
    column_name,
    data_type,
    character_maximum_length AS max_length,
    character_octet_length AS octet_length
FROM
    information_schema.columns
WHERE
    table_schema = 'public' AND 
    table_name = 'actor' AND
    column_name = 'first_name';

Result:

+-------------+-------------------+------------+--------------+
| column_name |     data_type     | max_length | octet_length |
+-------------+-------------------+------------+--------------+
| first_name  | character varying |         45 |          180 |
+-------------+-------------------+------------+--------------+

The pg_typeof() Function

The pg_typeof() function returns the OID of the data type of the value that is passed to it.

We can therefore us it to get the data type of a column by passing the column to the pg_typeof() function while querying the table:

SELECT pg_typeof(first_name)
FROM public.actor
LIMIT 1;

Result:

+-------------------+
|     pg_typeof     |
+-------------------+
| character varying |
+-------------------+

In PostgreSQL, character varying is the name for varchar (actually, varchar is the alias for character varying).