如何仅以毫秒为单位从PostgreSQL获取时间戳列?


29

timestamp without time zone default now()在PostgreSQL数据库中有一列“创建”的类型。

如果我选择“ colums”,则默认情况下它的格式很好看:

SELECT created FROM mytable;

         created
---------------------------
2011-05-17 10:40:28.876944

但是我只想以毫秒为单位获取时间戳(以Long为单位)。像这样:

从mytable中选择myformat(创建);

     created
-----------------
2432432343876944

我怎样才能在几毫秒内从PostgreSQL获取时间戳列?


对杰克的回应:

我确实得到了与您(-3600)相同的区别,但是如果我使用的话,timestamp with time zone我会看到“错误”或区别是因为'1970-01-01'获得了时区+01

create table my_table_2(created timestamp with time zone);
CREATE TABLE
insert into my_table_2 (created) values (now()), ('1970-01-01');
INSERT 0 2
select created, extract(epoch from created) from my_table_2;
            created            |    date_part
-------------------------------+------------------
 2011-05-18 11:03:16.909338+02 | 1305709396.90934
 1970-01-01 00:00:00+01        |            -3600
(2 rows)

区别是错误吗?我现在可能是因为“夏令时”吗?


to_timestamp()在插入时间戳0和1时也很有趣。

insert into my_table_2 (created) values (to_timestamp(0));
INSERT 0 1

insert into my_table_2 (created) values (to_timestamp(1));
INSERT 0 1
select created, extract(epoch from created) from my_table_2;
            created            |    date_part
-------------------------------+------------------
 2011-05-18 11:03:16.909338+02 | 1305709396.90934
 1970-01-01 00:00:00+01        |            -3600
 1970-01-01 01:00:00+01        |                0
 1970-01-01 01:00:01+01        |                1

Answers:


35

使用EXTRACT和UNIX时间戳

SELECT EXTRACT(EPOCH FROM TIMESTAMP '2011-05-17 10:40:28.876944') * 1000;

会给

1305621628876.94

乘以1000将其转换为毫秒。然后,您可以将其转换为所需的任何内容(十进制将是一个不错的选择)。不要忘记记住时区。JackPDouglas在他这样一个例子答案。这是他的回答的摘录(created是您的时间戳列),说明了如何使用时区:

SELECT EXTRACT(EPOCH FROM created AT TIME ZONE 'UTC') FROM my_table;

5

- 编辑 -

我发现这基本上是错误的。请参阅如何从PostgreSQL获取当前的unix时间戳?为我的困惑之源...

-结束编辑-

发布为答案,因为它不会作为评论。

测试平台:

create role stack;
grant stack to dba;
create schema authorization stack;
set role stack;

create table my_table(created timestamp);
insert into my_table(created) values(now()),('1970-01-01');
\d my_table
              Table "stack.my_table"
 Column  |            Type             | Modifiers
---------+-----------------------------+-----------
 created | timestamp without time zone |

查询:

select created, extract(epoch from created) from my_table;

          created          |    date_part
---------------------------+------------------
 2011-05-17 13:18:48.03266 | 1305634728.03266
 1970-01-01 00:00:00       |            -3600


select created, extract(epoch from date_trunc('milliseconds', created)) 
from my_table;

          created          |    date_part
---------------------------+------------------
 2011-05-17 13:18:48.03266 | 1305634728.03266
 1970-01-01 00:00:00       |            -3600


select created, extract(epoch from created at time zone 'UTC') from my_table;

          created          |    date_part
---------------------------+------------------
 2011-05-17 13:18:48.03266 | 1305638328.03266
 1970-01-01 00:00:00       |                0

date_part第三个查询中的注释是:130563 83 28.03266-3600。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.