PostgreSQL – Keep Trailing Zeros on Timestamp SELECT

date formatpostgresqltimestamp

This is the issue:
I am reading rows with a timestamp column from a table and I need to make sure that the database returns the timestamp with a fixed width. But I am encountering instances where the PostgreSQL database returns the timestamp without the trailing zeros.

The table I am working on contains UUID, Varchar, Smallint and Timestamp.
I understand that I can use "to_char" as explained here, but what I am really looking for is a way tell PostgreSQL to always return the Timestamp in a given format, regardless of zeros.

Here is an example of the output from the Timestamp column I get, which contains a trimmed Timestamp:

"2014-09-26 17:17:45.999"
"2014-11-26 17:17:46"
"2015-03-26 17:17:46.001"
"2015-04-26 17:17:46.002"
"2015-05-26 17:17:46.003"

This will naturally cause a problem for me since the function that is reading from the database expects to get a timestamp in a given format.

Is there a way to make the PostgreSQL always return a timestamp with a fixed width on SELECT * FROM [table] queries?

Below you see how the fields are created:

"created_UTC" timestamp NOT NULL DEFAULT (now() AT TIME ZONE 'UTC'),
"last_modified_UTC" timestamp NOT NULL DEFAULT (now() AT TIME ZONE 'UTC')

Best Answer

It seems to me that you need to set the Timestamp column to CURRENT_TIMESTAMP(3). This would set the column to return the time to the three digit precision you are looking for.