Sub topic 1
The Date and Time Types and When to Use Each
PostgreSQL has several temporal types. The correct choice depends on whether you need a calendar date, a wall-clock time, an absolute instant, or a duration. This chapter assumes you know basic SELECT, WHERE, GROUP BY, and CREATE TABLE, and that you have a general idea of UTC and UTC offsets.
Prerequisites
You should be able to run SQL in psql or another PostgreSQL client. You should also understand that an offset such as +02 means “two hours ahead of UTC.”
The main temporal types
| Type | Stores | Use it for |
|---|---|---|
| `date` | A calendar date | Birthdays, due dates, holidays |
| `time` | A time of day without a date or time zone | Store opening time, daily schedule |
| `timestamp` | Date and clock time without a time zone | A local wall-clock value whose zone is known separately |
| `timestamptz` | An instant in time | Events, log entries, appointments |
| `interval` | A duration or calendar quantity | “3 hours,” “2 months,” or “1 day” |
date is appropriate when the time of day does not matter:
CREATE TABLE person (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
name text NOT NULL,
birthday date NOT NULL,
reminder_at time
);A time value is a clock reading, such as 09:30:00. It does not identify an instant because it has no date or location. Use it for recurring local schedules, not for an event that must be ordered globally.
timestamp without time zone, usually written simply as timestamp, stores a date and clock time but no time-zone interpretation. For example, 2024-06-01 09:00:00 could mean 9 a.m. in Paris or New York. The database cannot know which one.
Use timestamp when the value is intentionally local and the application stores the location elsewhere. A store might have a local opening time, while a meeting that occurs at one definite instant should normally use timestamptz.
Why `timestamptz` is usually right for events
Despite its name, timestamptz does not preserve the input time-zone name or offset. PostgreSQL converts an input value to an absolute instant, conceptually using UTC. When it returns the value, PostgreSQL displays it in the current session's TimeZone.
CREATE TABLE event (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
name text NOT NULL,
starts_at timestamptz NOT NULL
);
INSERT INTO event (name, starts_at)
VALUES ('launch', '2024-06-01 09:00:00+02');
SET TimeZone = 'UTC';
SELECT starts_at FROM event;
-- 2024-06-01 07:00:00+00Changing the session time zone changes the display, not the stored instant. This behavior makes timestamptz suitable for audit records, payments, messages, and scheduled events.
Common selection errors
Do not use timestamp for a global event merely because its input looks readable. Without a zone, PostgreSQL cannot distinguish two identical local clock readings in different places.
Do not use timestamptz for a birthday. A birthday is a calendar date, and converting it between zones can produce an unexpected neighboring date if you attach an arbitrary time.
Use interval for durations, but remember that a month and a day are calendar units. They do not always represent a fixed number of seconds.
Exercise
Which type should store the instant when a customer placed an order, and which type should store the customer's birthday?
Show answer
Use timestamptz for the order instant and date for the birthday.