On 06/06/2014 06:19 AM, Alberto Olivares wrote:
Hello,

I want to insert data into a column "timestamp without time zone" data
type. The problem is I am receiving the data in UNIX time.

How can I create a trigger to transform the time from UNIX to timestamp
without time zone every time a new record is inserted into my database?

to_timesstamp will take the epoch, which I assume is what you are talking about, and turn it into a timestamp with time zone. Don't worry about the timezone.

test=> select to_timestamp(extract(epoch from now()));
         to_timestamp
-------------------------------
 2014-06-06 06:27:20.484509-07
(1 row)

test=> \d timestamp_test

Table "public.timestamp_test"

Column | Type | Modifiers

--------+-----------------------------+-----------

id | integer |

ts | timestamp without time zone |

 ts_z   | timestamp with time zone    |


Inserting a timestamp with time zone into a field that is timestamp without timezone will strip the timezone automatically.

test=> insert into timestamp_test values (1, to_timestamp(extract(epoch from now())), to_timestamp(extract(epoch from now())));
INSERT 0 1

test=> select * from timestamp_test ;
 id |             ts             |             ts_z
----+----------------------------+-------------------------------
  1 | 2014-06-06 06:30:58.080158 | 2014-06-06 06:30:58.080158-07
(1 row)

So you just need to wrap the above in a function and call it from your trigger.



Thank you in advance.

Regards,
Alberto.


*Alberto Olivares Colas
*Technical Consultant
Snowflake Software



--
Adrian Klaver
adrian.kla...@aklaver.com


--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

Reply via email to