patternsqlMinor
PostgresSQL date/time field out of range on seconds-since-epoch
Viewed 0 times
fieldepochpostgressqlrangesecondstimedatesinceout
Problem
I am importing a csv file into a PostgreSQL database. Most of the file imports without any trouble until I get this message
But this seems to be a valid timestamp for 01/12/2015 @ 6:45pm (UTC) according to https://www.unixtimestamp.com/index.php
So why is it out of range?
ERROR: date/time field value out of range: "1421088300"
HINT: Perhaps you need a different "datestyle" setting.
CONTEXT: COPY accidents, line 6356158, column datetime: "1421088300"But this seems to be a valid timestamp for 01/12/2015 @ 6:45pm (UTC) according to https://www.unixtimestamp.com/index.php
So why is it out of range?
Solution
Seconds since epoch timestamps
Your "timestamp" is in seconds-since-epoch. Per dezso, use
Bad complex idea, and dragons.
Check the input formats. They cover casts from strings. That's what
This is basically the same as another example on that chart.
Converting to
So if you want to convert from seconds-since-epoch, you can cheat by using the internal
What's happening here is that
If this is a new table you could actually type that column as
Your "timestamp" is in seconds-since-epoch. Per dezso, use
to_timestamp(). I missed that when I checked \dfS+Bad complex idea, and dragons.
Check the input formats. They cover casts from strings. That's what
COPY is doing under the hood. The only method that even remotely looks like a long number is ISO 8601. If you look at that example though you'll see it's not a seconds-since-epochExample | Description
19990108 | ISO 8601; January 8, 1999 in any modeThis is basically the same as another example on that chart.
Example | Description
1999-01-08 | ISO 8601; January 8, 1999 in any modeConverting to
timestamp with abstime as an intermediary formatSo if you want to convert from seconds-since-epoch, you can cheat by using the internal
abstime since there is no available cast directly to timestamp from a string of seconds-since-epoch.SELECT 1421088300::abstime::timestamp;
timestamp
---------------------
2015-01-12 12:45:00
(1 row)What's happening here is that
abstime is binary coercable with integer. You can see that in \dC+. I checked \dfS+ for functions to get from integer to timestamp and found none. There is a cast though from integer to abstime (which is stored as an integer), and from abstime to timestamp.If this is a new table you could actually type that column as
abstime. It should load perfectly fine. And then you can ALTER TABLE. here is an example, except I'm not running COPY (but it should work all the same).CREATE TABLE foo(bar)
AS VALUES
(1421088300::abstime);
TABLE foo;
bar
------------------------
2015-01-12 12:45:00-06
(1 row)
ALTER TABLE foo
ALTER bar
TYPE timestamp;
TABLE foo;
bar
---------------------
2015-01-12 12:45:00
(1 row)
\d foo;
Table "public.foo"
Column | Type | Modifiers
--------+-----------------------------+-----------
bar | timestamp without time zone |Code Snippets
Example | Description
19990108 | ISO 8601; January 8, 1999 in any modeExample | Description
1999-01-08 | ISO 8601; January 8, 1999 in any modeSELECT 1421088300::abstime::timestamp;
timestamp
---------------------
2015-01-12 12:45:00
(1 row)CREATE TABLE foo(bar)
AS VALUES
(1421088300::abstime);
TABLE foo;
bar
------------------------
2015-01-12 12:45:00-06
(1 row)
ALTER TABLE foo
ALTER bar
TYPE timestamp;
TABLE foo;
bar
---------------------
2015-01-12 12:45:00
(1 row)
\d foo;
Table "public.foo"
Column | Type | Modifiers
--------+-----------------------------+-----------
bar | timestamp without time zone |Context
StackExchange Database Administrators Q#185869, answer score: 8
Revisions (0)
No revisions yet.