1

How can I drop fk_bar if its table tbl_foo exists in Postgres and if the constraint itself exists?

I tried

ALTER TABLE IF EXISTS tbl_foo DROP CONSTRAINT IF EXISTS fk_bar;

But that gave me an error when tbl_foo does not exist. It executes successfully if the table does exist.

3
  • 1
    Works for me. Which Postgres version are you using? (use select version() to find out) Commented Nov 20, 2017 at 22:48
  • PostgreSQL 9.0.13 Commented Nov 20, 2017 at 22:51
  • Actually -- I have another server with 9.2.x and it works there Commented Nov 20, 2017 at 22:54

1 Answer 1

2

https://www.postgresql.org/docs/9.2/static/sql-altertable.html

ALTER TABLE [ IF EXISTS ] [ ONLY ] name [ * ]

https://www.postgresql.org/docs/9.1/static/sql-altertable.html

ALTER TABLE [ ONLY ] name [ * ]

before 9.2 no such syntaxis:

ALTER TABLE IF EXISTS

https://www.postgresql.org/docs/9.2/static/release-9-2.html

Add IF EXISTS options to some ALTER commands (Pavel Stehule)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.