Postgres将列从没有时区的时间改为int

Ant*_*pad 2 postgresql alter-table

我将表列TYPE从没有时区的时间转换为整数时遇到问题.那可能吗?其中整数将等于秒.

我正在使用ALTER表exampleTable ALTER COLUMN时间TYPE整数.

a_h*_*ame 6

这应该这样做:

 ALTER TABLE your_table 
     ALTER COLUMN time TYPE integer USING 0;
Run Code Online (Sandbox Code Playgroud)

ALTER COLUMN通常仅在您要保留该列中的数据时使用.在你的情况下,上述应该工作,但不会给你买任何东西.删除列然后添加类型integer和默认值为0 的新列将同样有效.

(顺便说一下:你不应该使用像time列名这样的保留字)

编辑
如果您确实想要转换现有数据,那么您可以使用:

 ALTER TABLE your_table 
     ALTER COLUMN time_column TYPE integer USING extract(epoch from time_column);
Run Code Online (Sandbox Code Playgroud)