1

I have written my own script to generate a restore SQL file and one of the first tasks to do the restore is delete all the constraints that should be there. (Don't worry, I am limiting my script to ones that exist at the time of the backup.)

Here is a generic code snippet

ALTER TABLE ONLY data DROP CONSTRAINT IF EXISTS data_pkey; 

Testing it on my own development environment it works as expected. However, running it on the production server that same code throws an error:

ERROR: syntax error at or near "EXISTS" LINE 1: ALTER TABLE ONLY data DROP CONSTRAINT IF EXISTS data_pkey; 

If the IF EXISTS part is removed and the syntax is:

ALTER TABLE ONLY data DROP CONSTRAINT data_pkey; 

the script runs without a problem.

I checked postgres.org under the ALTER TABLE section and my syntax is valid.

3
  • 3
    Versions of both installations? The IF EXISTS was introduced in Postgres 9.2. Commented Feb 6, 2014 at 18:21
  • IF EXISTS has to have been introduced before 9.2 for my development environment runs 9.1.11. But I think you answered the question. Querying the version through phpPgAdmin on the production server and it is merely 8.4, not 9.1 as documentation led me to believe, Commented Feb 6, 2014 at 18:32
  • Yes, my mistake - it's 9.0 and not 9.2. Commented Feb 6, 2014 at 19:37

1 Answer 1

1

As a workaround for old versions you can either:

  • Unconditionally drop the constraint and trap the error; or

  • Check information_schema or the pg_catalog for the constraint, and only drop it if you find it already exists.

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

1 Comment

Nice anticipation of my follow-up question: Since I can not use IF EXISTS because of the version as 8.4, how do I deploy the script? :-) Thanks for the answer

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.