Unlike other databases, PostgreSQL returns a TEXT type by default every time you run a select with a free string. Example:


in this case above the field FIELD will be returned with type TEXT

Does anyone know if it is possible to change some parameter in PostgreSQL so that it returns a CHAR or VARCHAR type in the above case?

Firebird and Oracle return a CHAR type.

I know this would be solved with a CAST however for this I would have to replicate the same for all SQL's of the system besides having to make using CAST a standard routine of developing new SQL's.

Anyway, does anyone have any idea of ​​anything that might have changed? I'm studying the files pg_cast.h and pg_type.h

  • This might help you stackoverflow.com/questions/13676816/postgres-data-type-cast
    – DEarTh
    May 17, 2018 at 13:53
  • 1
    Why do you need a varchar instead of text?
    – McNets
    May 17, 2018 at 15:13
  • Because TEXT types are not displayed in grids of our system. Casting commands would be a solution, but it would take a lot of time to analyze all the commands in our system. In addition to increasing the chances of bugs occurring. Our system is programmed to be compatible with 3 different database types May 18, 2018 at 12:14
  • We were able to change the postgresql function return to varchar(ex.: ltrim, rtrim, sub_str ..) through pg_proc.h. But with free strings we still have not made any progress. May 18, 2018 at 12:17
  • with a as ( select 'hello'::varchar as b from table ) select a.b,pg_typeof(a.b) from a; casting seems to work... maybe the problem is in the library. - can you post example code?
    – Jasen
    May 19, 2018 at 2:33

1 Answer 1


Maybe you just need to cast it.


or using an alternative pg-specific casting syntax.

 SELECT 'Text'::varchar as FIELD FROM TABLE;

If you select a string constant and don't cast it to something it has type unknown. but I'm guessing your connection library converts this to the preferred postgres string type 'text'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.