Postgresql: Check if Schema Exists?

前端 未结 10 1555
星月不相逢
星月不相逢 2021-02-01 12:24

I need to create, manage and drop schemas on the fly. If I go to create a schema that already exists, I want to (conditionally, via external means) drop and recreate it as speci

10条回答
  •  隐瞒了意图╮
    2021-02-01 12:32

    Somewhat related and perhaps of interest to others looking for conditional schema creation. I found myself using code like this in some of my creation scripts:

    DO $$
    BEGIN
    
        IF NOT EXISTS(
            SELECT schema_name
              FROM information_schema.schemata
              WHERE schema_name = 'pgcrypto'
          )
        THEN
          EXECUTE 'CREATE SCHEMA pgcrypto';
        END IF;
    
    END
    $$;
    

提交回复
热议问题