Postgresql - add boolean column to default table

Is this the correct postgresql syntax for adding a column to a table with a default value of false

 ALTER TABLE users ADD "priv_user" BIT ALTER priv_user SET DEFAULT '0' 

Thank!

+74
sql postgresql
Aug 13 2018-12-12T00:
source share
5 answers
 ALTER TABLE users ADD COLUMN "priv_user" BOOLEAN DEFAULT FALSE; 

you can also specify NOT NULL directly

 ALTER TABLE users ADD COLUMN "priv_user" BOOLEAN NOT NULL DEFAULT FALSE; 

As Craig said on the completed tables, it’s more efficient to break it down into stages:

 ALTER TABLE users ADD COLUMN priv_user BOOLEAN; UPDATE users SET priv_user = 'f'; ALTER TABLE users ALTER COLUMN priv_user SET NOT NULL; ALTER TABLE users ALTER COLUMN priv_user SET DEFAULT FALSE; 
+143
Aug 13 2018-12-12T00:
source share

If you need the actual boolean column:

 ALTER TABLE users ADD "priv_user" boolean DEFAULT false; 
+12
Aug 13 2018-12-12T00:
source share

Just for future reference, if you already have a boolean column and you just want to add a default value:

 ALTER TABLE users ALTER COLUMN priv_user SET DEFAULT false; 
+6
Aug 19 '14 at 12:40
source share

If you are using postgresql then you should use the lowercase BOOLEAN column type as boolean.

ALTER TABLE users ADD "priv_user" boolean DEFAULT false;

+4
Jan 01 '13 at 9:19
source share

In psql alter column query syntax like this

 Alter table users add column priv_user boolean default false ; 

boolean value (true-false) save in DB as (tf) value.

0
Mar 08 '17 at 5:52 on
source share



All Articles