Question

I want to do something like this:

alter table foos
alter column bar type bool using bar::boolean;

bar is currently a text column. Some experimentation shows that postgres will do things like convert a string "t" to boolean true. In my case this is great, but I'd like to read the full behavior before using this. I can't seem to find where this documentation is.

Was it helpful?

Solution

The behavior for boolean

typically you have to go look up every type and to see how it can be cast or look for a function to do the casting

here is a link

Postgresql also supports Create Cast

Information on the pg_cast

OTHER TIPS

This is in the PostgreSQL manual section on the boolean data type:

Boolean constants can be represented in SQL queries by the SQL key words TRUE, FALSE, and NULL.

The datatype input function for type boolean accepts these string representations for the “true” state:

true
yes
on
1

and these representations for the “false” state:

false
no
off
0

Unique prefixes of these strings are also accepted, for example t or n. Leading or trailing whitespace is ignored, and case does not matter.

The datatype output function for type boolean always emits either t or f […]

Licensed under: CC-BY-SA with attribution
Not affiliated with dba.stackexchange
scroll top