4 PostgreSQL provides the standard SQL type boolean; see Table 8.19. The
5 boolean type can have several states: “true”, “false”, and a third
6 state, “unknown”, which is represented by the SQL null value.
8 Table 8.19. Boolean Data Type
9 Name Storage Size Description
10 boolean 1 byte state of true or false
12 Boolean constants can be represented in SQL queries by the SQL key
13 words TRUE, FALSE, and NULL.
15 The datatype input function for type boolean accepts these string
16 representations for the “true” state:
22 and these representations for the “false” state:
28 Unique prefixes of these strings are also accepted, for example t or n.
29 Leading or trailing whitespace is ignored, and case does not matter.
31 The datatype output function for type boolean always emits either t or
32 f, as shown in Example 8.2.
34 Example 8.2. Using the boolean Type
35 CREATE TABLE test1 (a boolean, b text);
36 INSERT INTO test1 VALUES (TRUE, 'sic est');
37 INSERT INTO test1 VALUES (FALSE, 'non est');
44 SELECT * FROM test1 WHERE a;
49 The key words TRUE and FALSE are the preferred (SQL-compliant) method
50 for writing Boolean constants in SQL queries. But you can also use the
51 string representations by following the generic string-literal constant
52 syntax described in Section 4.1.2.7, for example 'yes'::boolean.
54 Note that the parser automatically understands that TRUE and FALSE are
55 of type boolean, but this is not so for NULL because that can have any
56 type. So in some contexts you might have to cast NULL to boolean
57 explicitly, for example NULL::boolean. Conversely, the cast can be
58 omitted from a string-literal Boolean value in contexts where the
59 parser can deduce that the literal must be of type boolean.