age
PG 8.0+→ intervalReturns the interval between two timestamps, or between a timestamp and now(), with years and months included symbolically.
Signatures
age ( timestamp, timestamp ) → intervalage ( timestamp ) → intervalParameters
| Parameter | Type | Description |
|---|---|---|
| timestamp1 | timestamp | Minuend (later date) |
| timestamp2 | timestamp | Subtrahend (earlier date); defaults to now() if omitted |
Examples
SELECT age('2025-01-01', '2000-06-15');24 years 6 mons 16 daysSELECT age(birth_date) FROM users;Age from birth_date to todaySELECT EXTRACT(year FROM age(birth_date)) AS years_old FROM users;Integer age in yearsSELECT name, age(created_at) AS account_age FROM users ORDER BY created_at;Account ages as human-readable intervalsDirect subtraction (`ts1 - ts2`) returns an interval measured in raw days and seconds with no month or year component. This means '1 year' shows as '365 days' and loses meaningful calendar structure.
✓ Instead: Use `age(ts1, ts2)` when you need a human-readable breakdown of years, months, and days.
Use `EXTRACT(year FROM age(birth_date))` rather than `EXTRACT(year FROM now()) - EXTRACT(year FROM birth_date)`. The second approach can be off by 1 for birthdays not yet reached this year.
SELECT name, EXTRACT(year FROM age(birth_date))::int AS age FROM users ORDER BY age;Correct age in whole years