SQLSTATE 22007 ERROR Class 22: Data Exception

invalid_datetime_format invalid input syntax for type timestamp: “…” — 22007

PostgreSQL error "invalid input syntax for type timestamp: "…"" (SQLSTATE 22007): what it means, common causes, and how to fix it.

PG 12, 13, 14, 15, 16, 17, 18 Official docs
Last reviewed May 2025 Grounded in source

Diagnostic Queries

Symptoms

A value being converted to timestamp/timestamptz is not a recognized date-time. PostgreSQL raises SQLSTATE 22007 (invalid_datetime_format) and quotes the bad value.

  • Common with ambiguous or non-ISO date formats.
  • Empty strings and locale-specific formats are frequent culprits.
  • The current DateStyle setting affects what is accepted.

What the server log shows

ERROR:  invalid input syntax for type timestamp: "2026-13-40 99:99"

Why PostgreSQL raises this — what the manual says

Section 8.5.1 Date/Time Input:

“Date and time input is accepted in almost any reasonable format”

The timestamp input parser interprets the string according to DateStyle and field ranges. Out-of-range fields, unparseable text, or empty strings cannot form a valid timestamp and fail with 22007.

Common causes

  • Out-of-range fields (month 13, hour 99).
  • Empty string for a missing timestamp.
  • Ambiguous day/month order conflicting with DateStyle.
  • Locale or custom formats the parser does not recognize.

How to fix it

  1. Send ISO 8601 (YYYY-MM-DD HH:MI:SS) to avoid ambiguity.
  2. Parse non-standard formats with to_timestamp(text, format).
  3. Convert empty strings to NULL; set DateStyle appropriately for the session.

Related & next steps

Reference: PostgreSQL 18 Section 8.5 “Date/Time Types”.

Was this helpful?