[postgresql] ALTER TABLE, set null in not null column, PostgreSQL 9.1

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;

This question is related to postgresql null postgresql-9.1

The answer is


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

Execute the command in this format

ALTER TABLE tablename ALTER COLUMN columnname SET NOT NULL;

for setting the column to not null.


Execute the command in this format:

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


Examples related to postgresql

Subtracting 1 day from a timestamp date pgadmin4 : postgresql application server could not be contacted. Psql could not connect to server: No such file or directory, 5432 error? How to persist data in a dockerized postgres database using volumes input file appears to be a text format dump. Please use psql Postgres: check if array field contains value? Add timestamp column with default NOW() for new rows only Can't connect to Postgresql on port 5432 How to insert current datetime in postgresql insert query Connecting to Postgresql in a docker container from outside

Examples related to null

getElementById in React Filter values only if not null using lambda in Java8 Why use Optional.of over Optional.ofNullable? How to resolve TypeError: Cannot convert undefined or null to object Check if returned value is not null and if so assign it, in one line, with one method call How do I assign a null value to a variable in PowerShell? Using COALESCE to handle NULL values in PostgreSQL How to check a Long for null in java Check if AJAX response data is empty/blank/null/undefined/0 Best way to check for "empty or null value"

Examples related to postgresql-9.1

Postgresql SQL: How check boolean field with null and True,False Value? must appear in the GROUP BY clause or be used in an aggregate function postgres, ubuntu how to restart service on startup? get stuck on clustering after instance reboot List tables in a PostgreSQL schema PostgreSQL ERROR: canceling statement due to conflict with recovery How do I convert an integer to string as part of a PostgreSQL query? ALTER TABLE, set null in not null column, PostgreSQL 9.1 Strange PostgreSQL "value too long for type character varying(500)" PostgreSQL next value of the sequences? Change type of varchar field to integer: "cannot be cast automatically to type integer"