PostgreSQL Error 22009: Invalid Time Zone Displacement
In this article
Error 22009 in PostgreSQL means a time zone displacement value you supplied is outside the range the server accepts. The fix is to correct the value to a real UTC offset between -15:59:59 and +15:59:59, or to use a named time zone instead of a raw numeric offset. This guide walks through diagnosing the error, correcting the data or query, and preventing it from coming back.
What You'll Need
- Access to the PostgreSQL server or database where the error occurs (psql, pgAdmin, or your application's database client).
- The ability to run queries and, if needed, update or alter table columns.
- Knowledge of which application or query triggered the error, so you can inspect the exact value being passed.
- Optional: a test database or transaction you can roll back, so corrections are safe to try.
Step-by-Step
- Reproduce the error and capture the full message. Run the failing query or application action again and note the complete PostgreSQL error text. It usually includes a hint such as
Value "+25:00" is out of range for type time with time zoneor names the column and value. The exact value is the key clue. - Identify where the value comes from. Check the query parameters, the application code that builds the timestamp, and any data import or ETL job. The error is almost always caused by a string like
+25:00,-16:00,+99:99, or a malformed offset such as+5:0being cast totimestamptzortimetz. Note that PostgreSQL accepts offsets only within roughly -15:59:59 to +15:59:59. - Test the value directly in psql. Run a small query such as
SELECT '+25:00'::timestamptz;to confirm the error. Then try a valid offset likeSELECT '+05:30'::timestamptz;to verify the syntax works. This isolates whether the problem is the value itself or the way it is being cast. - Correct literal values in queries. Replace invalid offsets with valid ones. If the offset was meant to represent a real location, use a named time zone such as
'America/New_York'or'Asia/Kolkata'instead of a numeric offset. Named zones handle daylight saving changes and are less error-prone. - Fix application code that generates offsets. Look for formatting code that produces offsets without a sign, with minutes over 59, or with hours over 15. Common culprits are custom date formatters, CSV exports, and JavaScript libraries that emit
+0530without a colon when PostgreSQL expects+05:30. Ensure the generated string matches a valid ISO 8601 offset. - Clean existing bad data in tables. If the error occurs when reading rows, find the offending rows with a query such as
SELECT id, tz_column FROM my_table WHERE tz_column !~ '^[+-](0[0-9]|1[0-5]):[0-5][0-9]$';(adjust the column and pattern to your data). Review those rows, then update them to valid values or NULL. Always run a SELECT first and back up the table before updating. - Validate incoming data at the boundary. Add a check in your application or a database constraint so invalid offsets are rejected early with a clear message. For example, a CHECK constraint or a trigger can validate the format before it reaches a timestamp column. This prevents the error from recurring in production.
- Re-run the original operation. After correcting the value or data, run the failing query or application action again. Confirm it succeeds and that the stored timestamps are correct, including the intended offset or zone.
Common Mistakes
- Assuming the error is about the server's time zone setting. Error 22009 is about a displacement value you supplied, not about
timezoneinpostgresql.conf. Changing the server time zone will not fix a bad literal. - Using offsets like
+25:00or-16:00. Real UTC offsets never exceed about 15 hours; values beyond that are invalid by definition. - Forgetting the sign or colon. Strings such as
0530or+530are not valid PostgreSQL offsets. Use+05:30. - Updating data without a backup. Bulk updates to timestamp columns can corrupt historical records if the wrong offset is applied. Always select and review first.
- Confusing
timestampwithtimestamptz. A plaintimestampcolumn does not store an offset, so casting an offset string into it can behave differently than expected. Check the column type before changing data.
Tips
- Prefer named time zones (
America/Chicago,Europe/Berlin) over numeric offsets when the value represents a real location. They are more readable and handle daylight saving automatically. - Use ISO 8601 format consistently:
2024-06-01T12:00:00+05:30. This avoids ambiguity between date and offset parts. - Log the exact string your application sends to PostgreSQL when debugging. The error message often truncates the value, so the log is more reliable.
- When importing CSV or JSON, validate offsets before the load. A single bad row can abort an entire transaction.
- Keep a small set of test queries with valid and invalid offsets to quickly confirm behavior after upgrades or code changes.
Troubleshooting
If the error persists after correcting the obvious value, work through these checks in order. First, confirm you are editing the right code path: search the application for the exact string from the error message. Second, check for triggers or rules on the table that might cast values again. Third, inspect any views or functions that transform timestamps, since the error can originate there even if your query looks clean. Fourth, verify the client library version; some older drivers format offsets without a colon. Finally, if the error appears only for certain rows, run a targeted SELECT to find and fix those rows, and consider adding a validation constraint so future inserts are rejected with a clearer message. If you cannot change the source data, cast the value to text and clean it in SQL before casting to timestamptz, but do this only after backing up the affected data.
Frequently Asked Questions
What causes PostgreSQL error 22009?
It is raised when a time zone displacement value is outside the valid range, typically because a string like +25:00 or a malformed offset is cast to a time zone type.
What is the valid range for a time zone offset in PostgreSQL?
Offsets must fall roughly between -15:59:59 and +15:59:59. Values outside that range are rejected.
Can I use a named time zone instead of an offset?
Yes. Named zones such as Europe/London are accepted and are usually safer because they account for daylight saving.
Does changing the server time zone fix error 22009?
No. The error concerns the value you supply, not the server's configured time zone. Correct the value or the data instead.
How do I find rows with invalid offsets?
Use a SELECT with a regular expression or a try-cast pattern to list suspicious values, review them, then update or null them after backing up the table.
Will this error stop a transaction?
Yes, an error inside a transaction aborts it until you roll back or fix the statement, so correct the value before retrying.