BOOLEAN
boolean stores true, false or unknown (NULL).
In this page:
Syntax
column_name boolean DEFAULT false
WHERE column_name IS TRUE
BOOLEAN
PostgreSQL accepts TRUE, FALSE, t, f, yes, no, on, off, 1 and 0 as boolean input and displays t and f in psql. Conditions can be used directly in WHERE, and IS TRUE, IS FALSE and IS NOT DISTINCT FROM handle NULL safely.
Booleans combine with AND, OR and NOT.
Note:
A boolean column can be true, false or NULL, so decide whether NULL is allowed.
Example: BOOLEAN
shop=# CREATE TABLE tasks (id serial PRIMARY KEY, title text, done boolean DEFAULT false);
shop=# INSERT INTO tasks (title, done) VALUES ('write', true), ('test', false), ('ship', NULL);
shop=# SELECT title, done FROM tasks;
title | done
-------+------
write | t
test | f
ship |
shop=# SELECT title FROM tasks WHERE done;
title
-------
write
shop=# SELECT title FROM tasks WHERE done IS NOT TRUE;
title
-------
test
ship
⚠️ Run this in your own terminal or Node.js environment.
Related Topics
Common Mistakes
- Storing yes/no as text
- Forgetting NULL is a third state
- Comparing with = TRUE instead of using the column directly
Chapter Summary
- Values true, false and NULL
- Many input spellings are accepted
- Use IS TRUE and IS NOT DISTINCT FROM
- Use the column directly in WHERE
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: