Is this proper postgresql syntax to add a column to a table with a default value of false
ALTER TABLE users
ADD "priv_user" BIT
ALTER priv_user SET DEFAULT '0'
Thanks!
ALTER TABLE users
ADD COLUMN "priv_user" BOOLEAN DEFAULT FALSE;
you can also directly specify NOT NULL
ALTER TABLE users
ADD COLUMN "priv_user" BOOLEAN NOT NULL DEFAULT FALSE;
UPDATE: following is only true for versions before postgresql 11.
As Craig mentioned on filled tables it is more efficient to split it into steps:
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;
ALTER TABLE users ADD COLUMN priv_user BOOLEAN;, then UPDATE users SET priv_user = 'f'; and finally if you need to ALTER TABLE users ALTER COLUMN priv_user SET NOT NULL;.ALTER TABLE users ADD COLUMN priv_user BOOLEAN; ALTER TABLE users ALTER COLUMN priv_user SET DEFAULT FALSE; UPDATE users SET priv_user = 'f'; ALTER TABLE users ALTER COLUMN priv_user SET NOT NULL; Otherwise there might come another row in between statement 2 and 3 which does not have the value, which would cause ALTER TABLE users ALTER COLUMN priv_user SET NOT NULL; to fail?If you want an actual boolean column:
ALTER TABLE users ADD "priv_user" boolean DEFAULT false;
Just for future reference, if you already have a boolean column and you just want to add a default do:
ALTER TABLE users
ALTER COLUMN priv_user SET DEFAULT false;
booleancolumn?