How do I select the date as a readable string from epoch time in milliseconds?
Some like: SELECT *, to_date(time_in_milli_sec) FROM mytable
Per PostgreSQL docs:
SELECT *, to_timestamp(time_in_milli_sec / 1000) FROM mytable
to_timestamp() returns timestamptz and depends on the current timezone setting. I added clarification to my answer.SELECT timestamp 'epoch' + time_in_millisec * interval '1 ms'
FROM mytable;
This returns type timestamp [without time zone], while to_timestamp() returns timestamp [without time zone], based on the timezone setting of the current session, which is a significant difference. The manual warns:
Beware that applying
to_timestampto an epoch extracted from adateortimestampvalue could produce a misleading result: the result will effectively assume that the original value had been given in UTC, which might not be the case.
Related:
timezone setting.For milliseconds
SELECT timestamp 'epoch' + proyecto.fecha_inicio * interval '1 ms'
from proyecto.proyecto
where proyecto.fecha_inicio is not null
For seconds
SELECT TIMESTAMP WITH TIME ZONE 'epoch' + 982384720 * INTERVAL '1 second';
In the manual : http://www.postgresql.org/docs/current/interactive/functions-datetime.html.
Line: .. "Here is how you can convert an epoch value back to a time stamp"..