Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgres timestamp to unix time in milliseconds as a bigint

Tags:

postgresql

How can I get the following snippet to work in postgres:

ALTER TABLE mytable
ADD COLUMN create_time_utc bigint not null
DEFAULT (now() at time zone 'utc');

I want the new column create_time_utc to be the unix time in milliseconds (i.e number of milliseconds since Unix epoch January 1 1970).

I know I need to convert the postgres timestamp to a bigint, but I'm not sure how to do that.

like image 588
sungiant Avatar asked Sep 22 '14 15:09

sungiant


People also ask

What is the time format in PostgreSQL?

The default is the ISO format. (The SQL standard requires the use of the ISO 8601 format.

What is timestamp with timezone in PostgreSQL?

In PostgreSQL 2 temporal data types namely timestamp and timestamptz where one is without timezone and the later is with timezone respectively, are supported to store Time and Date to a column. Both timestamp and timestamptz uses 8 bytes for storing timestamp values.

What is timestamp without timezone in PostgreSQL?

The TIMESTAMP (also known as TIMESTAMP WITHOUT TIME ZONE ) and TIMESTAMPTZ (also known as TIMESTAMP WITH TIME ZONE ) types stored as a 64-bit integer as a microsecond offset since 1970-01-01 in CRDB and as a 64-bit integer microsecond offset since 2000-01-01 in PostgreSQL (by default).

How do I find the timestamp in PostgreSQL?

The PostgreSQL function LOCALTIMESTAMP returns the current date and time (of the machine running that instance of PostgreSQL) as a timestamp value. It uses the 'YYYY-MM-DD hh:mm:ss. nnnnnnn' format, where: YYYY is a 4-digit year.


1 Answers

extract(epoch

alter table mytable
add column create_time_utc bigint not null
default (extract(epoch from now()) * 1000);

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

like image 93
Clodoaldo Neto Avatar answered Sep 20 '22 15:09

Clodoaldo Neto