How to drop column if it exists in PostgreSQL 9+?
You just need to add IF EXIST
to your DROP COLUMN
statement:
ALTER TABLE tableName
DROP COLUMN IF EXISTS columnName;
You can also try via IF EXISTS Method which work great while we are using migration
DO $$
BEGIN
IF EXISTS(
SELECT column_name FROM information_schema.columns WHERE table_name = tableName AND column_name = columnName)
THEN
ALTER TABLE tableName DROP COLUMN columnName;
END IF;
END $$;