1

I tried to update column's default value with the below queries in postgres. But seems its not working. May be I am missing something. Could you help?

ALTER TABLE tableName ADD COLUMN newColumn INTEGER DEFAULT 0;

 ALTER TABLE tableName ALTER COLUMN newColumn DROP DEFAULT;

or

 ALTER TABLE tableName ALTER COLUMN newColumn SET DEFAULT NULL;

 SELECT * FROM tableName;

Here I still find 0.

1
  • 2
    The (new) default value will only be applied when you insert new rows. It does not change anything for the existing rows Commented Sep 13, 2019 at 9:59

1 Answer 1

1

The change only applies to new records. After the modification you have to heal all the previous data with a migration like this:

UPDATE tableName SET newColumn = NULL WHERE newColumn = 0
Sign up to request clarification or add additional context in comments.

Comments

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.