Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FROM_UNIX alternate in PostgreSQL

Guys I have stored unix timestamps in my MySQL database, where I was reading them using the MySQL function FROM_UNIX().

Now I'm migrating the database from MySQL to PostgreSQL. In PostgreSQL, how I can read unix timestamps just like I was doing in MySQL with FROM_UNIX() ?

like image 206
user1551373 Avatar asked Aug 30 '12 10:08

user1551373


2 Answers

Taken from the manual:

to_timestamp(double precision) convert Unix epoch to time stamp

If you need parts of the created timestamp, use the extract function

select extract(year from to_timestamp(1284352323))
like image 68
a_horse_with_no_name Avatar answered Oct 14 '22 12:10

a_horse_with_no_name


SELECT to_char(date(to_timestamp(1195374767)),'YYYY-MM-DD');
  • to_timestamp - convert to Postgresql timestamp no unix timestamp
  • date convert to date type
  • to_char format output

http://www.postgresql.org/docs/8.1/static/functions-formatting.html

extract can do the same thing but not in one step

like image 44
baklarz2048 Avatar answered Oct 14 '22 13:10

baklarz2048