1

Can you format a TIMESTAMP WITH TIME ZONE column in a CREATE TABLE default constraint in Postgres v10.x?

Example Code:

create_date TIMESTAMP WITH TIME ZONE
 DEFAULT to_char(current_timestamp, 'DD-MON-YYYY HH24:MI:SS')
 NOT NULL

When running this, I get the following error:

ERROR: column "create_date" is of type timestamp with time zone but default expression is of type text HINT: You will need to rewrite or cast the expression. SQL state: 42804

However, according to the Postgres Docs this type of formatting should be possible.

I've also tried casting this with the to_timestamp() method, however, I get the following error:

ERROR: function to_timestamp(timestamp with time zone, unknown) does not exist HINT: No function matches the given name and argument types. You might need to add explicit type casts. SQL state: 42883

Evan Carroll
65.7k50 gold badges258 silver badges509 bronze badges
asked Mar 14, 2018 at 3:09

1 Answer 1

2

Can you format a TIMESTAMP WITH TIME ZONE column in a CREATE TABLE default constraint in Postgres v10.x?

No because that doesn't make any sense. Timestamps are stored as timestamps. It's a special type. You format things for display. Any point in time is stored the same way regardless. Formatting time should arguable not be done in the database at all.

Think of it like this, you may need Roman Numeral representation for some columns, but even if you do the database will stored integers in their binary representation.

Now if you want to change the clients default output formatting for timestamps, you can certainly do that. From the docs

The date/time style can be selected by the user using the SET datestyle command, the DateStyle parameter in the postgresql.conf configuration file, or the PGDATESTYLE environment variable on the server or client.

answered Mar 14, 2018 at 3:14

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.