Postgresql: Check if Schema Exists?

前端 未结 10 1579
星月不相逢
星月不相逢 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:31

    If you are a total purist or you want to gain some milisecs. I recommend you to make use of postgres native system catalog. One can avoid then nested loop which is caused by calling pg_catalog anyway...

    SELECT EXISTS(SELECT 1 FROM information_schema.schemata 
                  WHERE schema_name = 'name');
    

    querying information_schema

    If you querying pg_namespace directly:

    SELECT EXISTS(SELECT 1 FROM pg_namespace WHERE nspname = 'name');
    

    Planer's work is much simpler:

    enter image description here

    So your own solution was the best.

提交回复
热议问题