【问题标题】:PostgreSQL: manipulating the type of a PRIMARY KEY columnPostgreSQL:操作 PRIMARY KEY 列的类型
【发布时间】:2020-10-26 12:51:12
【问题描述】:

这是我的表的结构:

 \d trajectories
                       Table "postgres.trajectories"
   Column   |           Type           | Collation | Nullable | Default 
------------+--------------------------+-----------+----------+---------
 user_id    | integer                  |           |          | 
 session_id | bigint                   |           | not null | 
 timestamp  | timestamp with time zone |           | not null | 
 lat        | double precision         |           | not null | 
 lon        | double precision         |           | not null | 
 alt        | double precision         |           |          | 
Indexes:
    "trajectories_pkey" PRIMARY KEY, btree (session_id, "timestamp")
    "trajec_idx" btree (user_id, "timestamp")

样本数据:

SELECT * FROM trajectories LIMIT 5;
user_id |    session_id     |       timestamp        |    lat    |    lon     | alt 
---------+-------------------+------------------------+-----------+------------+-----
      85 | 84020081204232933 | 2008-12-05 07:27:03+00 | 39.934484 | 116.430599 | 200
      85 | 84020081204232933 | 2008-12-05 07:27:08+00 | 39.934486 | 116.430635 | 199
      85 | 84020081204232933 | 2008-12-05 07:27:13+00 | 39.934493 | 116.430689 | 199
      85 | 84020081204232933 | 2008-12-05 07:27:18+00 | 39.934468 | 116.430648 | 199
      85 | 84020081204232933 | 2008-12-05 07:27:23+00 | 39.934467 | 116.430614 | 199
(5 rows)

我想将timestamp 列转换为epoch,并且不再采用这种datetime 格式。

【问题讨论】:

标签: postgresql datetime epoch sqldatatypes


【解决方案1】:

这是个坏主意。最好在数据库中有时间戳。

不管怎样,这里是:

ALTER TABLE trajectories
   ALTER timestamp TYPE double precision USING extract(epoch FROM timestamp);

【讨论】:

    猜你喜欢
    • 2018-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多