5

I have table t1 and column c1 of type boolean NULL. All values in the column are NULL. However PostgreSQL won't let me change the type of the column this way:

ALTER TABLE "t1"
ALTER "c1" TYPE timestamp;

* ERROR: column "c1" cannot be cast to type timestamp without time zone

How can I change the type? Thank you

2 Answers 2

10

If all the values in your columns are indeed null, simple add USING NULL: there's no difficult conversion to make.

ALTER TABLE "t1"
ALTER "c1" TYPE timestamp USING NULL;
Sign up to request clarification or add additional context in comments.

1 Comment

Pretty sure this will merrily throw away non-null data if there is any, so do be careful.
0

I think you just have to give it a time zone. This worked for me:

ALTER TABLE t1
    ALTER COLUMN c1 SET DATA TYPE timestamp with time zone
    USING
        timestamp with time zone 'epoch';

1 Comment

Your approach (a) doesn't use the required column type (timestamptz instead of timestamp, it's not necessarily a bad thing, on the contrary, but that's not the same as in the question) and (b) fills all the values to "epoch", which is "1-Jan-1970 00:00" instead of being null. (Try SELECT timestamp with time zone 'epoch';: that's what USING is going to use.)

Your Answer

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.