PostgreSQL 18 reference · Date and Time Functions
PostgreSQL DATE_BIN() Function
Round a timestamp down into fixed-size buckets, such as 15-minute slots, counted from an origin.
PostgreSQL 18 Since 14
Syntax
date_bin(stride, source, origin)
Parameters
| Parameter | Description |
|---|---|
stride |
The bucket size, as an interval. |
source |
The timestamp. |
origin |
Where buckets start. |
Return value
A timestamp or timestamptz.
Examples
SELECT date_bin('15 minutes', TIMESTAMP '2026-03-09 10:37', TIMESTAMP '2026-01-01') AS slot;
Output
slot --------------------- 2026-03-09 10:30:00 (1 row)
Run this on your computer — see “Get Started” for setup.
SELECT date_bin('7 days', order_date::timestamp, TIMESTAMP '2026-01-05')::date AS week, count(*) AS orders
FROM orders
GROUP BY week
ORDER BY week
LIMIT 3;
Output
week | orders ------------+-------- 2026-01-05 | 1 2026-01-12 | 1 2026-02-02 | 1 (3 rows)
Run this on your computer — see “Get Started” for setup.
Version history
| Version | Change |
|---|---|
| PostgreSQL 14 | Added |
Related
DATE_TRUNC()· Truncate a timestamp or interval to a unit such as 'month', 'week' or 'hour', for grouping by period.- SQL Date Functions · tutorial