How to add column if not exists on PostgreSQL?
问题 Question is simple. How to add column x to table y , but only when x column doesn't exist ? I found only solution here how to check if column exists. SELECT column_name FROM information_schema.columns WHERE table_name='x' and column_name='y'; 回答1: Here's a short-and-sweet version using the "DO" statement: DO $$ BEGIN BEGIN ALTER TABLE <table_name> ADD COLUMN <column_name> <column_type>; EXCEPTION WHEN duplicate_column THEN RAISE NOTICE 'column <column_name> already exists in <table_name>.';