Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL UNIX_TIMESTAMP for PostgreSQL

Basically I am trying to convert a date field from my database into a number of seconds old. I used UNIX_TIMESTAMP on our old MySQL database and am looking for an equivalent function in PostgreSQL.

Thanks in advance

like image 208
Spencer Avatar asked May 02 '11 13:05

Spencer


People also ask

What is Unix_timestamp in MySQL?

UNIX_TIMESTAMP() : This function in MySQL helps to return a Unix timestamp. We can define a Unix timestamp as the number of seconds that have passed since '1970-01-01 00:00:00'UTC. Even if you pass the current date/time or another specified date/time, the function will return a Unix timestamp based on that.

How do I get the current time in PostgreSQL?

The PostgreSQL function CURRENT_TIME returns the current time and time zone offset of the machine on which PostgreSQL is running. It is given as a value in the 'hh:mm:ss. nnnnnn+/-tz' format.

What is epoch time in PostgreSQL?

Posted on 19th October 2022. YES, you can convert EPOCH to Timestamp by merely switching to the present Timestamp in PostgreSQL DBMS. EPOCH time is nothing but the number of seconds from 00:00:00 UTC on 1 January 1970. Till date, without adding the extra leap year days, this is considered.

What is the datatype for time in PostgreSQL?

PostgreSQL provides user with TIME data type that is used to handle time values. It requires 8 bytes of storage and can have precision up to 6 digits. It can range from 00:00:00 to 24:00:00 .


2 Answers

SELECT extract(epoch FROM your_datetime_column)
FROM your_table

More details in the manual:

http://www.postgresql.org/docs/current/static/functions-datetime.html#FUNCTIONS-DATETIME-EXTRACT

like image 125
a_horse_with_no_name Avatar answered Sep 23 '22 12:09

a_horse_with_no_name


This:

select CURRENT_TIMESTAMP - '1970-01-01';

Will give you an interval type. You can get seconds out of that interval result if you need it.

like image 37
Pablo Santa Cruz Avatar answered Sep 22 '22 12:09

Pablo Santa Cruz