190

I have a table with not null column, How to set a null value in this column as default?

I mean, I want to do something like this:

postgres=# ALTER TABLE person ALTER COLUMN phone SET NULL;

but it shows:

postgres=# ALTER TABLE person ALTER COLUMN phone SET NULL;
ERROR:  syntax error at or near "NULL"
LINE 1: ALTER TABLE person ALTER COLUMN phone SET NULL;
2
  • Possible duplicate of How to change a PG column to NULLABLE TRUE? Commented May 4, 2018 at 0:45
  • The request to "set null in a not null column" means to remove the "NOT NULL" CONSTRAINT on that column: "ALTER TABLE person ALTER COLUMN phone DROP NOT NULL;" Commented Feb 21, 2022 at 23:14

4 Answers 4

361
ALTER TABLE person ALTER COLUMN phone DROP NOT NULL;

More details in the manual: http://www.postgresql.org/docs/9.1/static/sql-altertable.html

Sign up to request clarification or add additional context in comments.

Comments

86

Execute the command in this format

ALTER TABLE tablename ALTER COLUMN columnname SET NOT NULL;

for setting the column to not null.

4 Comments

Column names must not be enclosed in single quotes
This helped me, even though it didn't answer the question.
This does not answer the question! I cannot understand why it has that many upvotes.
It's exactly this opposite of what is requested !
27

Execute the command in this format:

ALTER [ COLUMN ] column { SET | DROP } NOT NULL

Comments

25
First, Set :
ALTER TABLE person ALTER COLUMN phone DROP NOT NULL;

1 Comment

Came here searching for how to set the column as NOT NULL. With your answer, I could figure the solution: ALTER TABLE person ALTER COLUMN phone SET NOT NULL. Thanks!

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.