A boolean column holds true or false. It can also be empty, NULL, which means the answer isn’t known, and NULL behaves differently from true and false when you search. Here’s how boolean and NULL work in PostgreSQL, on a small to-do list.
Every command and output on this page is real, from PostgreSQL 19 on macOS.
Syntax
column_name boolean
WHERE column_name IS NULL
WHERE column_name IS NOT TRUE
boolean: true, false, or NULL when the value is unknown. Write the values asTRUE,FALSEandNULL.IS NULL: finds rows where the value is unknown.IS NOT TRUE: finds rows that are false or unknown.
See the Boolean Type (opens in a new tab) and Comparison Functions and Operators (opens in a new tab) in the PostgreSQL documentation.
The Example
A to-do list marks each task as done, not done, or not known yet.
Step 1: Create a Test Database
Connect to your server with psql as the postgres user, as Part 2 of the PostgreSQL Tutorial shows. psql connects you to the postgres database, and its prompt is postgres=#.
Create a database for the example and connect to it:
CREATE DATABASE todo;
CREATE DATABASE
\c todo
You are now connected to database "todo" as user "postgres".
Step 2: Create the Table and Add Tasks
CREATE TABLE tasks (title text, done boolean);
CREATE TABLE
INSERT INTO tasks (title, done) VALUES ('Buy milk', true);
INSERT 0 1
INSERT INTO tasks (title, done) VALUES ('Walk the dog', false);
INSERT 0 1
INSERT INTO tasks (title, done) VALUES ('Call the bank', NULL);
INSERT 0 1
SELECT * FROM tasks;
title | done
---------------+------
Buy milk | t
Walk the dog | f
Call the bank |
(3 rows)
psql shows true as t and false as f. The NULL is left empty.
Step 3: Find the Unknown Task
= NULL looks right, but finds nothing:
SELECT * FROM tasks WHERE done = NULL;
title | done
-------+------
(0 rows)
NULL means unknown, and an unknown value isn’t equal to anything, not even another NULL. Use IS NULL:
SELECT * FROM tasks WHERE done IS NULL;
title | done
---------------+------
Call the bank |
(1 row)
Step 4: Find the Tasks That Aren’t Done
NOT done skips the unknown task, because “not unknown” is still unknown:
SELECT * FROM tasks WHERE NOT done;
title | done
--------------+------
Walk the dog | f
(1 row)
IS NOT TRUE includes it:
SELECT * FROM tasks WHERE done IS NOT TRUE;
title | done
---------------+------
Walk the dog | f
Call the bank |
(2 rows)
Step 5: Other Ways to Write True
PostgreSQL also accepts text such as 'yes', 'on' and '1' for true:
INSERT INTO tasks (title, done) VALUES ('Water the plants', 'yes');
INSERT 0 1
A boolean column is a condition on its own, so WHERE done finds the true rows:
SELECT * FROM tasks WHERE done;
title | done
------------------+------
Buy milk | t
Water the plants | t
(2 rows)
Clean Up
To remove the example, connect to another database and drop the test database:
\c postgres
You are now connected to database "postgres" as user "postgres".
DROP DATABASE todo;
DROP DATABASE
Warning: DROP DATABASE deletes the database and everything in it, and it can’t be undone.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
WHERE done = NULL | (0 rows), even when there are NULLs. | Use WHERE done IS NULL. |
WHERE NOT done to find every task that isn’t done | Tasks with NULL are left out. | Use WHERE done IS NOT TRUE. |
FAQ
Which words count as true and false?
true, yes, on and 1 mean true, and false, no, off and 0 mean false, in any case. Short forms such as t and n work too.
Can I stop a column from being NULL?
Yes. Add NOT NULL after the type when you create the table, and PostgreSQL refuses a row without a value.
Conclusion
A boolean is true, false or NULL. Find unknown values with IS NULL, never = NULL, and use IS NOT TRUE when the unknown rows belong with the false ones.
Additional Resources
- PostgreSQL 19 documentation: Boolean Type (opens in a new tab)
- PostgreSQL 19 documentation: Comparison Functions and Operators (opens in a new tab)
Related
- PostgreSQL Tutorial — Part 5: Data Types Explained
- How to Check a Column’s Data Type in PostgreSQL
- timestamp vs timestamptz in PostgreSQL
Postgres, PostgreSQL and the Slonik Logo are trademarks or registered trademarks of the PostgreSQL Community Association of Canada, and used with their permission.
0 comments