Thread: check for table existence before dropping

check for table existence before dropping

From
"Constantin"
Date:
Hi

I am trying to to add some SQL code before a CREATE TABLE statement
that checks whether a table exists before dropping it.

This doesnt seem to work:

IF EXISTS( SELECT * FROM pg_class WHERE relname = 'cust' )
THEN
    DROP TABLE cust
    DROP SEQUENCE cust_cust_id_seq
END IF;

CREATE TABLE cust
(
    cust_id        SERIAL    PRIMARY KEY,
    name        TEXT        NOT NULL
);

Any ideas how to do this with postgresql?
Many thanks :)