Alex Rivera | Logout

How to add column if not exists on PostgreSQL?

Asked 2012-09-26T08:20:29.840
264

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';
Edit
Report

1 Answer

-5

Simply check if the query returned a column_name.

If not, execute something like this:

ALTER TABLE x ADD COLUMN y int;

Where you put something useful for 'x' and 'y' and of course a suitable datatype where I used int.

answered 2012-09-26T08:23:34.483

Your Answer