PostgreSQL: how to convert from Unix epoch to date?

SqlPostgresqlDateType ConversionEpoch

Sql Problem Overview


The statement gives me the date and time.

How could I modify the statement so that it returns only the date (and not the time)?

SELECT to_timestamp( TRUNC( CAST( epoch_ms AS bigint ) / 1000 ) );

Sql Solutions


Solution 1 - Sql

You use to_timestamp function and then cast the timestamp to date

 select to_timestamp(epoch_column)::date;

More details:

/* Current time */
 select now();  -- returns timestamp

/* Epoch from current time;
   Epoch is number of seconds since 1970-01-01 00:00:00+00 */
 select extract(epoch from now()); 

/* Get back time from epoch */
 -- Option 1 - use to_timestamp function
 select to_timestamp( extract(epoch from now()));
 -- Option 2 - add seconds to 'epoch'
 select timestamp with time zone 'epoch' 
         + extract(epoch from now()) * interval '1 second';

/* Cast timestamp to date */
 -- Based on Option 1
 select to_timestamp(extract(epoch from now()))::date;
 -- Based on Option 2
 select (timestamp with time zone 'epoch' 
          + extract(epoch from now()) * interval '1 second')::date; 

In your case:

 select to_timestamp(epoch_ms / 1000)::date;

PostgreSQL Docs

Solution 2 - Sql

select to_timestamp(cast(epoch_ms/1000 as bigint))::date

worked for me

Solution 3 - Sql

On Postgres 10:

SELECT to_timestamp(CAST(epoch_ms as bigint)/1000)

Solution 4 - Sql

The solution above not working for the latest version on PostgreSQL. I found this way to convert epoch time being stored in number and int column type is on PostgreSQL 13:

SELECT TIMESTAMP 'epoch' + (<table>.field::int) * INTERVAL '1 second' as started_on from <table>;

For more detail explanation, you can see here https://www.yodiw.com/convert-epoch-time-to-timestamp-in-postgresql/#more-214

Solution 5 - Sql

This works for me fine:

SELECT t.*,
   to_timestamp(cast(t.prev_fire_time/1000 as bigint)) as prev_fire_time,
   to_timestamp(cast(t.next_fire_time/1000 as bigint)) as next_fire_time,
   to_timestamp(cast(t.start_time/1000 as bigint)) as start_time
FROM public.qrtz_triggers t;

Solution 6 - Sql

Seconds since epoch with GNU date:

$ date +%s.%N
1627059870.945134901

This works with PostgreSQL 11:

# select to_timestamp (1627059870.945134901);
         to_timestamp          
-------------------------------
 2021-07-23 19:04:30.945135+02
(1 row)

# select to_timestamp (1627059870.945134901)::date;
 to_timestamp 
--------------
 2021-07-23
(1 row)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionsid_comView Question on Stackoverflow
Solution 1 - SqlTomas GreifView Answer on Stackoverflow
Solution 2 - SqlSinuView Answer on Stackoverflow
Solution 3 - SqlStefano ColettaView Answer on Stackoverflow
Solution 4 - SqlyodiView Answer on Stackoverflow
Solution 5 - SqlYegorView Answer on Stackoverflow
Solution 6 - SqlcevingView Answer on Stackoverflow