将表列的数据类型从timestamp更改为bigint

Naz*_*Naz 4 sql postgresql ddl types

我正在使用postgres,我有一个表的列,其数据类型是没有时区的时间戳.

我想将数据类型更改为bigint.我试图在列中存储自1970年以来的秒数.所以大有点像1397597908756.

我正在使用python,类似于:

d = dict() # create a dictionary, has key 'timestamp'
#get data from server and store in array 
d.update(dict(timestamp=data[1]) #data[1] has the number of seconds 
Run Code Online (Sandbox Code Playgroud)

我多次触摸服务器所以存储在字典中是必不可少的.查询是:

cursor.execute("INSERT into tablename columname VALUES (%s)", (quote['timestamp'];
Run Code Online (Sandbox Code Playgroud)

此时,抛出异常:

类型为timestamp的输入语法无效:1397597908756

所以我尝试将数据类型更改timestamp without timezonebigint.我做了:

ALTER TABLE tablename ALTER COLUMN columnname
SET DATA TYPE bigint USING updated::bigint;
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

错误:无法将没有时区的类型时间戳转换为bigint

Erw*_*ter 8

ALTER TABLE tablename ALTER COLUMN updated
TYPE bigint USING EXTRACT(EPOCH FROM updated);
Run Code Online (Sandbox Code Playgroud)

每个文件:

for datetimestampvalues,当地时间1970-01-01 00:00:00以来的秒数;


Cra*_*ger 4

埃尔文的回答是正确的,我只是想解决另一个方面。

我试图在列中存储自 1970 年以来的秒数

请不要这样做。查询起来很烦人,并且与仅使用时间戳相比没有任何存储优势。

店铺timestamp without time zone。或者,最好使用它,timestamp with time zone以便根据TimeZone客户端的设置进行适当调整。

如果客户端应用程序需要纪元秒,它总是可以select extract(epoch from my_field), ...。但实际上,您的应用程序应该能够正确使用日期。

目前尚不清楚您试图通过切换到存储原始纪元秒来解决应用程序中的什么问题;在某些情况下您可能必须这样做。但就我个人而言,我不会做这样的事情,而是定义一个可更新的视图,该视图返回纪元秒并将输入的 bigint 转换为时间戳以存储在基础表中。因此应用程序会认为它有一个包含纪元秒的表,但它实际上是在使用时间戳。当然,我更愿意首先让我的应用程序正确使用时间戳,如果某些客户端接口需要,则在应用程序中转换为纪元秒。

更新:

在你的情况下,只需插入一个datetime.datetime对象。

import datetime

# Convert epoch seconds into a Python datetime.datetime object that psycopg2 will
# understand as a date and insert as a PostgreSQL timestamp value.
ts = datetime.datetime.fromtimestamp(d['timestamp'])

cursor.execute("INSERT into tablename columname VALUES (%s)", (ts,) )
Run Code Online (Sandbox Code Playgroud)