2

I need to write a PostgreSQL script that can be executed repeatedly.

I want to add a column for my table, and check if the table and column exist, and add comment.

So I write this:

ALTER TABLE IF EXISTS tbl_user ADD COLUMN IF NOT EXISTS age INT COMMENT 'age';

but I got an error . It seems that "COMMENT" is not allowed here.

Is there a way to add comment in one statement when adding column?

1

1 Answer 1

2

No, you have to add the comment in a separate statement:

ALTER TABLE IF EXISTS tbl_user ADD COLUMN IF NOT EXISTS age INT;

COMMENT ON COLUMN tbl_user.age IS 'age';
1
  • if you need both to happen at the exact same time you can run them inside a transaction.
    – Jasen
    Commented Aug 8, 2023 at 4:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.