ALTER TABLE, set null in not null column, PostgreSQL 9.1

PostgresqlNullPostgresql 9.1

Postgresql Problem Overview


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;

Postgresql Solutions


Solution 1 - Postgresql

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

Solution 2 - Postgresql

Execute the command in this format

ALTER TABLE tablename ALTER COLUMN columnname SET NOT NULL;

for setting the column to not null.

Solution 3 - Postgresql

Execute the command in this format:

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

Solution 4 - Postgresql

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBrian BrownView Question on Stackoverflow
Solution 1 - Postgresqla_horse_with_no_nameView Answer on Stackoverflow
Solution 2 - PostgresqlRigin OommenView Answer on Stackoverflow
Solution 3 - PostgresqlSunny ChawlaView Answer on Stackoverflow
Solution 4 - Postgresqljainvikram444View Answer on Stackoverflow