Amazon Redshift: Drop Table If Exists

Amazon Redshift: Drop Table If Exists

Does Redshift support any statement equivalent to the following?

DROP TABLE IF EXISTS tablename

2 Answers

This is supported in the latest version of Redshift:

DROP TABLE [ IF EXISTS ] name [, ...] [ CASCADE | RESTRICT ]

IF EXISTS Clause that indicates that if the specified table doesn’t exist, the command should make no changes and return a message that the table doesn't exist, rather than terminating with an error.

This clause is useful when scripting, so the script doesn’t fail if DROP TABLE runs against a nonexistent table.

Taken from online AWS Redshift docs.

0

See next answer; this is out of date.


Support for

DROP TABLE IF EXISTS tablename;

was added in PostgreSQL 8.2. Redshift is a very heavily modified fork of 8.1 by ParAccel, and as far as I know they've backported very few changes from newer versions. It's very unlikely that it supports IF EXISTS; you probably need to do a catalog query to determine if the table exists by looking up information_schema, then deciding whether you'll create it based on the result.

1

Your Answer

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

Maya Lin-Takahashi
Author

Maya Lin-Takahashi

Maya is a hardware enthusiast who tests and reviews smart home devices, smartphones, wearables, and audio gear. She focuses on practical consumer value and build quality.