I come from MySQL to PostgreSQL then, I created test
table with BOOLEAN state
column in PostgreSQL as shown below:
CREATE TABLE test (
state BOOLEAN -- Here
);
But, I couldn’t insert TRUE
with 1
and FALSE
with 0
to test
table as shown below even though the SQL queries below work in MySQL:
INSERT INTO test VALUES (1);
INSERT INTO test VALUES (0);
Then, I got the error below:
ERROR: column "state" is of type boolean but expression is of type integer
So, how to insert a boolean value to a table?
2
Answers
You can insert
TRUE
with'1'
andFALSE
with'0'
in PostgreSQL as shown below:Then,
t
which isTRUE
andf
which isFALSE
are inserted totest
table as shown below:In addtion, these SQL queries below also work to insert
TRUE
andFALSE
totest
table as shown below:TRUE
:FALSE
:The standard way to insert boolean values in PostgreSQL is to use the literal boolean values
true
orfalse
or any expression that evaluates to a boolean.For example:
Returns:
See running example in db<>fiddle.