PostgreSQL – How to Change Default Output Format for Boolean Values

postgresql

In a Postgres database, I know how to use case statements to get only TRUE or FALSE for a boolean field but was wondering whether there is a cleaner way to set this up like changing any configuration in the db.

Update: in Postgres, a the boolean representation for true is not only TRUE but also: yes, y, on, "t". So, I am inserting values with value TRUE. However, the SELECT command returns those values as "t".

Best Answer

if I understand you .. use this https://www.postgresql.org/docs/9.3/ddl-default.html

CREATE TABLE T (
b boolean not null default FALSE
);

on the other hand postgres still allows for more values that will be translated to true or false (Table of literal values for true and false according to http://www.postgresqltutorial.com/postgresql-boolean/)

True   False
true   false
‘t’    ‘f‘
‘true’ ‘false’
‘y’    ‘n’
‘yes’  ‘no’
‘1’    ‘0’

so you probably can't enforce true / false - on other hand a select will deliver true / false as you want