PostgreSQL – Convert Unix Timestamp to SQL Timestamp

datedatetimepostgresqltimestamp

I have passed Date.now() and other plain unix timestamps like 1534360109480 into sql and ran them through this.

"@converted_date" = to_timestamp("@date") AT TIME ZONE 'UTC';

And it consistently returns 50591-11-28 22:32:38.

I do not understand how the year could possibly be 50591 and the date/time is not accurate beyond that anyways.

What am I doing wrong?

Best Answer

to_timestamp accepts the epoch value ("Unix time") with second precision, while your value seems to have millisecond precision. You need to do something like

to_timestamp(@date/1000) AT TIME ZONE 'UTC';