Check if a user-defined type already exists in PostgreSQL

后端 未结 10 1581
离开以前
离开以前 2020-12-29 01:43

Say I have created some user-defined types in the DB,

i.e. CREATE TYPE abc ...

Is it then possible to determine if the user-defined type exists

10条回答
  •  旧时难觅i
    2020-12-29 02:39

    The simplest solution I've found so far that copes with schemas, inspired by @Cromax's answer, is this:

    DO $$ BEGIN
        CREATE TYPE my_type AS (/* fields go here */);
    EXCEPTION
        WHEN duplicate_object THEN null;
    END $$;
    

    Just what you might expect really - we just wrap the CREATE TYPE statement in an exception handler so it doesn't abort the current transaction.

提交回复
热议问题