PostgreSQL 17.12Lab-verified · 2026-12-01

PostgreSQL 22008: date/time field value out of range

The text looks like a date or time, but one field (month, day or hour) is outside its valid range.

PostgreSQL 17.12
22008: date/time field value out of range: "30/11/2026"

Jump to a cause

  1. 01dd/mm/yyyy text read as month/day (DateStyle MDY)
  2. 02A day that does not exist, such as November 31
  3. 03More than 24 hours stored in a time column
  4. 04Casting Unix epoch seconds text with ::timestamp
  5. 05Month and day swapped in the to_date format

Lab-verifiedReproduced and fixed on PostgreSQL 17.12 · 2026-12-01 · container image postgres:17

The message quotes the value: date/time field value out of range: "30/11/2026". Unlike 22007, the format is recognized; one of the numbers is impossible.

When the problem is the day/month order, PostgreSQL adds HINT: Perhaps you need a different "datestyle" setting. The default DateStyle in the postgres:17 image is ISO, MDY.

01dd/mm/yyyy text read as month/day (DateStyle MDY)

SHOW datestyle returns ISO, MDY, so '30/11/2026' is read as month 30, which does not exist.

Setup
Setuprun as lab_a
CREATE TABLE orders (id int PRIMARY KEY, order_date date);
OUTPUT
CREATE TABLE
Reproducerun as lab_a
INSERT INTO orders VALUES (1, '30/11/2026');
✕ ERROR
ERROR:  22008: date/time field value out of range: "30/11/2026"
LINE 1: INSERT INTO orders VALUES (1, '30/11/2026');
                                      ^
HINT:  Perhaps you need a different "datestyle" setting.
Checkrun as lab_a
SHOW datestyle;
OUTPUT
 DateStyle
-----------
 ISO, MDY
(1 row)

State the format explicitly with to_date('30/11/2026', 'DD/MM/YYYY'). It is stored as 2026-11-30 regardless of DateStyle. Changing DateStyle was not tested here.

After the fixrun as lab_a
INSERT INTO orders VALUES (1, to_date('30/11/2026', 'DD/MM/YYYY'));
SELECT * FROM orders;
✓ SUCCESS
INSERT 0 1
 id | order_date
----+------------
  1 | 2026-11-30
(1 row)

02A day that does not exist, such as November 31

A month-end date is built by appending '-31' to '2026-11'. November has 30 days, so '2026-11-31'::date fails.

Setup
Setuprun as lab_a
CREATE TABLE closings (ym text PRIMARY KEY, close_date date);
OUTPUT
CREATE TABLE
Reproducerun as lab_a
INSERT INTO closings VALUES ('2026-11', ('2026-11' || '-31')::date);
✕ ERROR
ERROR:  22008: date/time field value out of range: "2026-11-31"
Checkrun as lab_a
SELECT (date_trunc('month', date '2026-11-01') + interval '1 month - 1 day')::date AS month_end;
OUTPUT
 month_end
------------
 2026-11-30
(1 row)

Compute the month end: (date_trunc('month', d) + interval '1 month - 1 day')::date returns 2026-11-30.

After the fixrun as lab_a
INSERT INTO closings VALUES ('2026-11', (date_trunc('month', ('2026-11' || '-01')::date) + interval '1 month - 1 day')::date);
SELECT * FROM closings;
✓ SUCCESS
INSERT 0 1
   ym    | close_date
---------+------------
 2026-11 | 2026-11-30
(1 row)

03More than 24 hours stored in a time column

The column holds total hours worked as time, but time is a time of day, so '25:30' is out of range.

Setup
Setuprun as lab_a
CREATE TABLE work_log (emp_id int, work_day date, worked time);
OUTPUT
CREATE TABLE
Reproducerun as lab_a
INSERT INTO work_log VALUES (7, '2026-11-30', '25:30');
✕ ERROR
ERROR:  22008: date/time field value out of range: "25:30"
LINE 1: INSERT INTO work_log VALUES (7, '2026-11-30', '25:30');
                                                      ^
Fixrun as lab_a
ALTER TABLE work_log ALTER COLUMN worked TYPE interval;
OUTPUT
ALTER TABLE

Use interval for durations: after ALTER COLUMN worked TYPE interval, '25:30' is stored as 25:30:00 and extract(epoch …) / 3600 gives 25.5 hours.

After the fixrun as lab_a
INSERT INTO work_log VALUES (7, '2026-11-30', '25:30');
SELECT emp_id, worked, extract(epoch FROM worked) / 3600 AS hours FROM work_log;
✓ SUCCESS
INSERT 0 1
 emp_id |  worked  |        hours
--------+----------+---------------------
      7 | 25:30:00 | 25.5000000000000000
(1 row)

04Casting Unix epoch seconds text with ::timestamp

A string like '1764460800' from an API or log is not a date. Casting it with ::timestamp gives 22008, and the HINT mentions datestyle, but DateStyle is not the problem.

Setup
Setuprun as lab_a
CREATE TABLE events (id int PRIMARY KEY, created_at timestamp);
CREATE TABLE events_raw (id int, created_at text);
INSERT INTO events_raw VALUES (1, '1764460800'), (2, '1764547200');
OUTPUT
CREATE TABLE
CREATE TABLE
INSERT 0 2
Reproducerun as lab_a
INSERT INTO events SELECT id, created_at::timestamp FROM events_raw;
✕ ERROR
ERROR:  22008: date/time field value out of range: "1764460800"
HINT:  Perhaps you need a different "datestyle" setting.
Checkrun as lab_a
SELECT created_at, to_timestamp(created_at::bigint) AT TIME ZONE 'UTC' AS utc_time FROM events_raw;
OUTPUT
 created_at |      utc_time
------------+---------------------
 1764460800 | 2025-11-30 00:00:00
 1764547200 | 2025-12-01 00:00:00
(2 rows)

Convert it to a number and use to_timestamp(created_at::bigint). It returns timestamptz, so here AT TIME ZONE 'UTC' was used for the timestamp column, storing 2025-11-30 00:00:00.

After the fixrun as lab_a
INSERT INTO events SELECT id, to_timestamp(created_at::bigint) AT TIME ZONE 'UTC' FROM events_raw;
SELECT * FROM events ORDER BY id;
✓ SUCCESS
INSERT 0 2
 id |     created_at
----+---------------------
  1 | 2025-11-30 00:00:00
  2 | 2025-12-01 00:00:00
(2 rows)

05Month and day swapped in the to_date format

Reading US-style '11/30/2026' with 'DD/MM/YYYY' makes 30 the month. Because the error comes from to_date, there is no HINT or LINE, only the value.

Setup
Setuprun as lab_a
CREATE TABLE shipments (id int PRIMARY KEY, ship_date date);
OUTPUT
CREATE TABLE
Reproducerun as lab_a
INSERT INTO shipments VALUES (1, to_date('11/30/2026', 'DD/MM/YYYY'));
✕ ERROR
ERROR:  22008: date/time field value out of range: "11/30/2026"
Checkrun as lab_a
SELECT to_date('11/30/2026', 'MM/DD/YYYY') AS ship_date;
OUTPUT
 ship_date
------------
 2026-11-30
(1 row)

Use the format that matches the data, 'MM/DD/YYYY', and it is stored as 2026-11-30.

After the fixrun as lab_a
INSERT INTO shipments VALUES (1, to_date('11/30/2026', 'MM/DD/YYYY'));
SELECT * FROM shipments;
✓ SUCCESS
INSERT 0 1
 id | ship_date
----+------------
  1 | 2026-11-30
(1 row)

FAQ

How do I fix date/time field value out of range in PostgreSQL?

Check the quoted value. For dd/mm/yyyy use to_date with a format, for impossible days compute the date instead of building a string, and use interval for durations over 24 hours.

What does the HINT about datestyle mean?

PostgreSQL read your day as a month because DateStyle is MDY. Using to_date with an explicit format avoids depending on that setting.

How do I get the last day of the month in PostgreSQL?

(date_trunc('month', your_date) + interval '1 month - 1 day')::date

Can a PostgreSQL time column store 25 hours?

No. time is a time of day. Store durations in an interval column, which accepts '25:30'.

How do I convert a Unix timestamp to timestamp in PostgreSQL?

'1764460800'::timestamp fails with 22008. Use to_timestamp(value::bigint), which returns timestamptz, and pick a time zone such as AT TIME ZONE 'UTC' for a timestamp column.

How we verify

Each scenario starts from fresh database users. A script runs the setup, confirms the exact error code, applies the fix and runs the statement again. The output shown is what the database printed, saved by the script.

lab_a and lab_b are ordinary roles created for the test, each owning a schema of the same name; postgres is the superuser. PostgreSQL 17.12