Mohamed El Mahallawy
Mohamed El Mahallawy

Reputation: 13852

Introspect the types returned from a SQL query

How can I introspect the types returned from a SQL query. I can introspect the table to get the columns and types, but during run time, there may be fields that are not in the db (for example, the concat method). Here is an example:

SELECT id, email, number_of_logins, concat(first_name, last_name) as full_name from users

Is there a way to find the types?

Upvotes: 0

Views: 426

Answers (2)

Laurenz Albe
Laurenz Albe

Reputation: 246493

With recent versions of psql, you can simply use \gdesc rather than ; to end your query:

SELECT 42 AS x \gdesc 

 Column |  Type   
--------+---------
 x      | integer
(1 row)

Upvotes: 1

Nathan Getachew
Nathan Getachew

Reputation: 789

You can check using pg_catalog.format_type(concat(first_name, last_name)) also try pg_typeof(col)

SELECT id, email, number_of_logins, pg_catalog.format_type(concat(first_name, last_name)) as full_name from users

Upvotes: 3

Related Questions