Skip to main content
0 reactions

Boolean and NULL in PostgreSQL: true, false and unknown

Featured card for Boolean and NULL in PostgreSQL, with a terminal window listing true → t, false → f, NULL: unknown and IS NULL, not = NULL.
Boolean and NULL in PostgreSQL.

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

SyntaxSQL
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 as TRUE, FALSE and NULL.
  • 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:

postgres=#SQL
CREATE DATABASE todo;
OutputPlain text
CREATE DATABASE
postgres=#Plain text
\c todo
OutputPlain text
You are now connected to database "todo" as user "postgres".

Step 2: Create the Table and Add Tasks

todo=#SQL
CREATE TABLE tasks (title text, done boolean);
OutputPlain text
CREATE TABLE
todo=#SQL
INSERT INTO tasks (title, done) VALUES ('Buy milk', true);
OutputPlain text
INSERT 0 1
todo=#SQL
INSERT INTO tasks (title, done) VALUES ('Walk the dog', false);
OutputPlain text
INSERT 0 1
todo=#SQL
INSERT INTO tasks (title, done) VALUES ('Call the bank', NULL);
OutputPlain text
INSERT 0 1
todo=#SQL
SELECT * FROM tasks;
OutputPlain text
     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:

todo=#SQL
SELECT * FROM tasks WHERE done = NULL;
OutputPlain text
 title | done
-------+------
(0 rows)

NULL means unknown, and an unknown value isn’t equal to anything, not even another NULL. Use IS NULL:

todo=#SQL
SELECT * FROM tasks WHERE done IS NULL;
OutputPlain text
     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:

todo=#SQL
SELECT * FROM tasks WHERE NOT done;
OutputPlain text
    title     | done
--------------+------
 Walk the dog | f
(1 row)

IS NOT TRUE includes it:

todo=#SQL
SELECT * FROM tasks WHERE done IS NOT TRUE;
OutputPlain text
     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:

todo=#SQL
INSERT INTO tasks (title, done) VALUES ('Water the plants', 'yes');
OutputPlain text
INSERT 0 1

A boolean column is a condition on its own, so WHERE done finds the true rows:

todo=#SQL
SELECT * FROM tasks WHERE done;
OutputPlain text
      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:

todo=#Plain text
\c postgres
OutputPlain text
You are now connected to database "postgres" as user "postgres".
postgres=#SQL
DROP DATABASE todo;
OutputPlain text
DROP DATABASE

Warning: DROP DATABASE deletes the database and everything in it, and it can’t be undone.

Common Mistakes

MistakeWhat happensDo 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 doneTasks 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

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