Did it help? If yes - maybe you can help me?
On 30th of July, Andrew Dunstan committed patch:
Add IF NOT EXISTS processing to ALTER TABLE ADD COLUMN Fabrízio de Royes Mello, reviewed by Payal Singh, Alvaro Herrera and Michael Paquier.
Nice. I always like when there is new “IF EXISTS" or “IF NOT EXISTS", because it makes my life as dba simpler.
This time, we got the addition for adding columns. Up to 9.5, if you wanted to make a sql script that adds column – it would fail if the column already existed:
$ alter table test add column z text; ALTER TABLE $ alter table test add column z text; ERROR: column "z" of relation "test" already exists
This error could be handled by savepoint, or DO command, with error catching, but it's not really nice. Now, however, we can:
$ alter table test add column if not exists z text; ALTER TABLE $ alter table test add column if not exists z text; NOTICE: column "z" of relation "test" already exists, skipping ALTER TABLE
Please note that on the 2nd run I didn't get ERROR, just NOTICE – and the result was a success. Simple, but definitely welcome, thanks.