PostgreSQL 9.0.x 中不推荐使用日期时间变量类型?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8304902/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-20 23:26:02  来源:igfitidea点击:

Datetime variable type deprecated in PostgreSQL 9.0.x?

postgresqldatetimetimestamp

提问by Edwin

I'm querying an external API and want to test out data inserts on a test server before moving to production.

我正在查询外部 API 并希望在进入生产环境之前测试测试服务器上的数据插入。

Currently, I'm trying to execute the following statement in Postgres's shell:

目前,我正在尝试在 Postgres 的 shell 中执行以下语句:

CREATE TABLE referrals (client_id VARCHAR(36), device_id VARCHAR(100), conversion_time DATETIME, acquisition_cost MONEY);

but it keeps failing on DATETIME.

但它一直失败DATETIME

Since this doesn't work, would timestamp? If so, how do I modify timestamp's default behavior to take in preformatted dates and times?

既然这不起作用,那么时间戳会怎样?如果是这样,我如何修改时间戳的默认行为以采用预先格式化的日期和时间?

回答by Erwin Brandstetter

CREATE TABLE referrals (
  client_id text 
, device_id text 
, conversion_time timestamp
, acquisition_cost money
);

You don't haveto add without time zone, that is the default.

你不必without time zone这是默认

I use the type textin the example instead of varcharwith a length modifier, because that is usually the better choice. You can use varchar(n)of course, no problem.

text在示例中使用类型而不是varchar长度修饰符,因为这通常是更好的选择。你varchar(n)当然可以用,没问题。

localeand DateStylesettings influence how text input for date/ timestampvalues is interpreted. Use the functions to_date()or to_timestamp()to be largely independent of local settings:

语言环境DateStyle设置会影响date/timestamp值的文本输入的解释方式。使用功能to_date()to_timestamp()在很大程度上独立于本地设置:

SELECT to_timestamp('05 Dec 2000', 'DD Mon YYYY');

More in the chapter Data Type Formatting Functionsof the fine manual.

更多内容请参见精细手册的数据类型格式化函数一章。

回答by Cody Caughlan

I believe the data type you're looking for is timestamp without time zone, so change your DDL to:

我相信您正在寻找的数据类型是timestamp without time zone,因此将您的 DDL 更改为:

CREATE TABLE referrals (
   client_id VARCHAR(36), 
   device_id VARCHAR(100), 
   conversion_time TIMESTAMP without time zone, 
   acquisition_cost MONEY
);