Check if a user-defined type already exists in PostgreSQL

后端 未结 10 1608
离开以前
离开以前 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条回答
  •  清歌不尽
    2020-12-29 02:37

    This plays well with schemas, and avoids exception handling:

    DO $$
    BEGIN
        IF NOT EXISTS (
          SELECT 1 FROM pg_type t
          LEFT JOIN pg_namespace p ON t.typnamespace=p.oid
          WHERE t.typname='my_type' AND p.nspname='my_schema'
        ) THEN
            CREATE TYPE my_schema.my_type AS (/* fields go here */);
        END IF;
    END
    $$;
    

提交回复
热议问题