PostgreSQL supports all types of date, time, and timestamp data types, and operations on them.
Date/Time Types
Name
Storage Size
Description
Low Value
High Value
Resolution
timestamp [ (p) ] [ without time zone ]
8 bytes
both date and time (no time zone)
4713 BC
294276 AD
1 microsecond
timestamp [ (p) ] with time zone
8 bytes
both date and time, with time zone
4713 BC
294276 AD
1 microsecond
date
4 bytes
date (no time of day)
4713 BC
5874897 AD
1 day
time [ (p) ] [ without time zone ]
8 bytes
time of day (no date)
00:00:00
24:00:00
1 microsecond
time [ (p) ] with time zone
12 bytes
time of day (no date), with time zone
00:00:00+1559
24:00:00-1559
1 microsecond
interval [ fields ] [ (p) ]
16 bytes
time interval
-178000000 years
178000000 years
1 microsecond
Date and time input is accepted in almost any reasonable format, including ISO 8601, SQL-compatible, traditional POSTGRES, and others. If there is any ambiguity, then the DateStyle parameter can be set to a required format (MDY or DMY, or YMD).
Date Input
Parameter DateStyle dictates the input date format. Check the current DateStyle format.
show datestyle;
It dictates that the input date should be in format month-date-year format or any of its close variations but in the same order. Trying to input the date in some other order will give an error. Let’s try these commands:
show datestyle;
SET datestyle TO "ISO, DMY";
show datestyle;
select '20/12/2016'::date;
select '12/20/2016'::date;
There are other options as well for SET datestyle command.
SET DATESTYLE TO Postgres, MDY;
SET DATESTYLE TO SQL;
SET DATESTYLE TO German;
Database Level
Parameter Datestyle can be changed at the individual database level inside a cluster using ALTER DATABASE command. But, to bring the change into effect, the whole cluster has to be restarted.
Don’t forget to unset PGDATESTYLE as it overrides the database/instance level value of parameter DateStyle.
\c appdb
alter database appdb set datestyle TO "ISO, DMY";
\q
pg_ctlcluster 13 mars stop
pg_ctlcluster 13 mars start
unset PGDATESTYLE
psql -p 5433
select '20/12/2016'::date;
select '12/20/2016'::date;
Instance Level
Parameter Datestyle can be changed at the instance level by changing the parameter in ‘postgresql.conf’ file followed by a restart of the cluster.
show datestyle;
select now();
alter system set datestyle='German';pg_ctlcluster 13 mars stop
pg_ctlcluster 13 mars start
unset PGDATESTYLE
psql -p 5433
select '20/12/2016'::date;
select '12/20/2016'::date;
admin
Share post:
PostgreSQL supports all types of date, time, and timestamp data types, and operations on them.
Date/Time Types
timestamp [ (
p
) ] [ without time zone ]timestamp [ (
p
) ] with time zonedate
time [ (
p
) ] [ without time zone ]time [ (
p
) ] with time zoneinterval [
fields
] [ (p
) ]Date and time input is accepted in almost any reasonable format, including ISO 8601, SQL-compatible, traditional POSTGRES, and others. If there is any ambiguity, then the DateStyle parameter can be set to a required format (MDY or DMY, or YMD).
Date Input
Parameter DateStyle dictates the input date format. Check the current DateStyle format.
It dictates that the input date should be in format month-date-year format or any of its close variations but in the same order. Trying to input the date in some other order will give an error. Let’s try these commands:
Parameter DateStyle can be changed at the session level, database level, or instance level.
Session Level
At the session level, it can be changed by setting the environment variable PGDATESTYLE or SET command.
There are other options as well for SET datestyle command.
Database Level
Parameter Datestyle can be changed at the individual database level inside a cluster using ALTER DATABASE command. But, to bring the change into effect, the whole cluster has to be restarted.
Instance Level
Parameter Datestyle can be changed at the instance level by changing the parameter in ‘postgresql.conf’ file followed by a restart of the cluster.
Time Input
Time can be input in any of the general formats.
Timestamp Input
Timestamp can be input in any of the general formats.
Normal strings can be converted to date or systimestamp using in-built functions, and vice versa. Let’s see those in action:
Template Patterns for Date/Time Formatting
HH
HH12
HH24
MI
SS
MS
US
SSSS
AM
,am
,PM
orpm
A.M.
,a.m.
,P.M.
orp.m.
Y,YYY
YYYY
YYY
YY
Y
IYYY
IYY
IY
I
BC
,bc
,AD
orad
B.C.
,b.c.
,A.D.
ora.d.
MONTH
Month
month
MON
Mon
mon
MM
DAY
Day
day
DY
Dy
dy
DDD
IDDD
DD
D
1
) to Saturday (7
)ID
1
) to Sunday (7
)W
WW
IW
CC
J
Q
RM
rm
TZ
to_char
)tz
to_char
)OF
to_char
)Converting string to date/timestamp
Put above statements into a file date.sql and run on ‘psql’ prompt. The output would be as below:
Converting date/timestamp to string
Title 1
Detail 1
Sub title 1
Sub title 2
Sub Detail 1
Title 2
ggggggg
dadadsasda
Title 3
Detail 3
Title 4
Detail 4