How to get the current Unix timestamp from PostgreSQL?

In this article, we will see how to get the current Unix timestamp from PostgreSQL?

The unix time stamp is a way to track time as a running total of seconds. This count starts at the Unix Epoch on January 1st, 1970 at UTC.

Getting the Unix timestamp from a postgresql timestamptz like now() is simple by using either of below queries.

Syntax:

select extract(epoch from now()); SELECT extract(epoch from now() at time zone 'utc');

Examples:

r2schools=# SELECT now(),extract(epoch from now()); now | date_part -------------------------------+------------------- 2020-10-30 06:49:20.490551-07 | 1604065760.490551 (1 row) r2schools=# SELECT now(), extract(epoch from now() at time zone 'utc'); now | date_part -------------------------------+------------------- 2020-10-30 06:49:26.154581-07 | 1604065766.154581 (1 row)