9951 explained code solutions for 126 technologies


postgresqlHow do I use boolean values in PostgreSQL?


Boolean values are used in PostgreSQL to store true/false values. They are stored using the boolean data type.

Example code

CREATE TABLE test (
    id serial PRIMARY KEY,
    is_active boolean NOT NULL DEFAULT false
);

This code creates a table called test with an id column of type serial and a is_active column of type boolean. The is_active column is set to false by default.

To insert data into the table, use the INSERT command:

INSERT INTO test (is_active) VALUES (true);

This code inserts a row into the test table with the is_active column set to true.

To query the table, use the SELECT command:

SELECT * FROM test WHERE is_active = true;

This code returns all rows from the test table where the is_active column is set to true.

Helpful links

Edit this code on GitHub