The types timestamp
or timestamptz
optionally take a precision modifier p
: timestamp(p)
.
To round to full seconds, set the default to:
now()::timestamp(0)
or:
now()::timestamptz(0)
Standard SQL functions CURRENT_TIMESTAMP
(returns timestamptz
) or LOCALTIMESTAMP
(returns timestamp
) allow the same precision modifier:
CURRENT_TIMESTAMP(0)
LOCALTIMESTAMP(0)
That's a bit shorter than calling date_trunc()
- which truncates fractional seconds (may be what you really want!)
date_trunc('second', now())
Store timestamps as timestamptz
(or timestamp
), not as character type.
Finally, to make sure that ...
newly added entries also have the format: YYYY-MM-DD HH24:MI:SS
you could define your column as type timestamptz(0)
. This covers all values entered into that column, not just the default. But the rounding may introduce timestamps up to half a second in the future. If that can be an issue in any way, rather use date_trunc()
.
See @Clodoaldo's answer for instructions on to_char()
and how to ALTER TABLE
.
This related answer for in-depth information on timestamps and time zone handling: