PG date类型插入数据说明


datastyle格式为DMY或者MDY时,允许以YMD的格式插入数据;
但datastyle格式为YMD时,不允许以DMY或者MDY的格式插入数据;

postgres9.6@[local]:5432 postgres# create table t (coll date);
CREATE TABLE
Time: 10.555 ms
postgres9.6@[local]:5432 postgres# show datestyle;
 DateStyle 
-----------
 ISO, MDY
(1 row)

Time: 0.518 ms


postgres9.6@[local]:5432 postgres# insert into t values(date '2016-12-15');
INSERT 0 1
Time: 4.225 ms
postgres9.6@[local]:5432 postgres# select * from t;
    coll    
------------
 2016-12-15
(1 row)
Time: 0.646 ms
postgres9.6@[local]:5432 postgres# insert into t values(date '12-15-2016');
INSERT 0 1
Time: 2.720 ms
postgres9.6@[local]:5432 postgres# insert into t values(date '15-12-2016');
ERROR:  date/time field value out of range: "15-12-2016"
LINE 1: insert into t values(date '15-12-2016');
                                  ^
HINT:  Perhaps you need a different "datestyle" setting.
Time: 0.591 ms


postgres9.6@[local]:5432 postgres# set datestyle='DMY';
SET
Time: 0.827 ms
postgres9.6@[local]:5432 postgres# insert into t values(date '15-12-2016');
INSERT 0 1
Time: 3.200 ms
postgres9.6@[local]:5432 postgres# insert into t values(date '2016-12-15');
INSERT 0 1
Time: 2.884 ms
postgres9.6@[local]:5432 postgres# insert into t values(date '12-15-2016');
ERROR:  date/time field value out of range: "12-15-2016"
LINE 1: insert into t values(date '12-15-2016');
                                  ^
HINT:  Perhaps you need a different "datestyle" setting.
Time: 0.761 ms
postgres9.6@[local]:5432 postgres# set datestyle='YMD';
SET
Time: 0.306 ms
postgres9.6@[local]:5432 postgres# insert into t values(date '2016-12-15');
INSERT 0 1
Time: 2.611 ms
postgres9.6@[local]:5432 postgres# insert into t values(date '12-15-2016');
ERROR:  date/time field value out of range: "12-15-2016"
LINE 1: insert into t values(date '12-15-2016');
                                  ^
HINT:  Perhaps you need a different "datestyle" setting.
Time: 1.523 ms
postgres9.6@[local]:5432 postgres# insert into t values(date '15-12-2016');
ERROR:  date/time field value out of range: "15-12-2016"
LINE 1: insert into t values(date '15-12-2016');
                                  ^
HINT:  Perhaps you need a different "datestyle" setting.
Time: 1.465 ms

你可能感兴趣的:(Highgo,DB,PostgreSQL)