今天在群里,有个群友问:

请教一个问题,在pgsql中要怎么把2015-11-17 15:31:36.966+08这样的日期转换成Unix时间戳啊?

这个在PostgreSQL里,有个时间函数,只不过可能没有MySQL的unix_timestamp()这么方便,但功能是一样的。PostgreSQL的函数,灵活性比较大(灵活的言外之意,就是复杂~~~哈哈)

废话就不多说了,直接上码示例

创建一张示例表

postgres=# create table ttt (dt timestamptz);
CREATE TABLE

postgres=# insert into ttt values ('2015-11-17 15:31:36.966+08');
INSERT 0 1

SQL

postgres=# select * from ttt;
             dt
----------------------------
 2015-11-17 15:31:36.966+08
(1 row)

postgres=# select (date_part('epoch', dt)) from ttt;
   date_part
----------------
 1447745496.966
(1 row)

postgres=# select (date_part('epoch', dt))::bigint from ttt;
 date_part
------------
 1447745497
(1 row)

postgres=#

即使用函数: date_part('epoch', 时间字段(date,timestamp等))

PostgreSQL 日期/时间 函数手册

Manual