13 Boolean and Dates
PostgreSQL’s boolean type stores true, false, or NULL. For dates and times you have a small family of types: date for a calendar day, time for a time of day, timestamp for date-and-time without a time zone, and timestamptz (timestamp with time zone) for an instant displayed in the session time zone (the original zone/offset is not retained). For almost all application use, timestamptz is what you want.
What you’ll learn
- The
booleantype and how it accepts many inputs (true,'t','yes',1) - The four core date/time types and when each makes sense
- Why
timestamptzis preferred overtimestamp current_date,current_timestamp,now(),localtime- Date arithmetic with
interval
Example
The boolean type accepts a generous set of input strings: true, false, t, f, yes, no, y, n, 1, 0, on, off. The output is always shown as t or f in psql. Comparisons (=, <>, etc.) produce booleans; you can store the result of a comparison in a boolean column.
timestamp without time zone (just timestamp) stores a wall-clock value with no notion of where on Earth it was. If you insert 2024-01-15 10:00:00, that is exactly what you get back, regardless of who reads it. timestamp with time zone (timestamptz) stores the same instant in UTC internally and presents it in the session’s time zone. Two sessions in different time zones will see the same timestamptz as different wall-clock times, but the underlying instant is the same.
For nearly every “when did this event happen?” question — order placed, log written, user created — timestamptz is the right answer. Plain timestamp is for things like “the recipe says bake at 10am local time, in whatever local time the user is in” — rare.
Run the example
To run:
Common pitfalls
- Storing event times as
timestampinstead oftimestamptz: a year later, you cannot tell whether they were UTC, server-local, or user-local. Alwaystimestamptz. - Comparing a
dateortimestamp without time zoneto atimestamptzuses the session time zone to interpret the value without a zone. Be explicit when it matters. now()returns the time of the transaction start, not the wall-clock now. Inside a long transaction, repeated calls return the same value. Useclock_timestamp()for true wall-clock time.interval '1 month'plus a date gives a timestamp in the next month — sometimes with surprises (January 31 + 1 month = February 28/29).
Set your session’s time zone with SET TIME ZONE 'UTC';. In production, run the database in UTC and convert in the application layer when displaying to users.
Run SELECT current_timestamp, now(), clock_timestamp(); — current_timestamp and now() are identical; clock_timestamp() can already differ within the same statement. Then BEGIN; SELECT now(); SELECT pg_sleep(1); SELECT now(); COMMIT; and observe.
Downloads
SQL file: boolean-and-dates.sql · All lesson files · Setup guide