7

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() ?

2 Answers 2

8

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))
Sign up to request clarification or add additional context in comments.

5 Comments

to_timestamp basically converts unix timestamp to readable PostgreSQL timestamp. I don't need all. I want specific things from unix timestamp. For example I want only the Year from timestamp. In MySQL is done like FROM_UNIXTIME(field,"%YY")
@user1551373: then use the extract function to get that from the resulting timestamp.
@user1551373 extract is SQL standard by the way, and should work just fine in MySQL too. to_standard doesn't seem to be though it's supported in Oracle and some other databases. You might want to consider a writing a wrapper function if your code needs to be portable.
@CraigRinger: what do you mean with to_standard?
@a_horse_with_no_name Argh, to_timestamp . Sorry.
4
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

2 Comments

It's worth adding a brief note of explanation about what you're doing and a link to the docs. See @a_horse_with_no_name's answer. Your answer will probably get flagged by the low quality question moderator's system and deleted as a code-only answer with better answers present.
You can leave date(...) out. to_char(to_timestamp(1195374767),'YYYY-MM-DD')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.