← Back to PostgreSQL Course | Chapter 3: Data Types | Lesson 3 of 7

BOOLEAN

boolean stores true, false or unknown (NULL).

In this page:

  1. BOOLEAN
Syntax
sql
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

bash
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
  1. Storing yes/no as text
  2. Forgetting NULL is a third state
  3. 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.