Today I Learned

hashrocket A Hashrocket project

Checking The Type Of A Value

The pg_typeof() function allows you to determine the data type of anything in Postgres.

> select pg_typeof(1);
 pg_typeof
-----------
 integer
(1 row)

> select pg_typeof(true);
 pg_typeof
-----------
 boolean
(1 row)

If you try it on an arbitrary string, it is unable to disambiguate which string type (e.g. text vs varchar).

> select pg_typeof('hello');
 pg_typeof
-----------
 unknown
(1 row)

You just have to be a bit more specific.

> select pg_typeof('hello'::varchar);
     pg_typeof
-------------------
 character varying
(1 row)

source

See More #sql TILs
Looking for help? Hashrocket developers believe that data quality is as important as code quality. We enjoy all the challenges of relational databases, from finding the fastest index, to structuring data to fit the needs of an application. We're eager to share our experiences; check out PG Casts, our series of free PostgreSQL screencasts.