title: How to drop all schemas in PostgreSQL with SQL query?
body: <p>I need to drop all the schemas in my database except <code>public</code>, <code>information_schema</code> and those <code>LIKE 'pg_%'</code>.</p>
  
  <p>Here's what I've found: (this variant doesn't seem to work)</p>
  
  <pre><code>   CREATE OR REPLACE FUNCTION drop_all () 
     RETURNS VOID  AS
     $$
     DECLARE rec RECORD; 
     BEGIN
         -- Get all the schemas
          FOR rec IN
          SELECT DISTINCT schemaname
           FROM pg_catalog.pg_tables
           -- You can exclude the schema which you don't want to drop by adding another condition here
           WHERE schemaname NOT LIKE 'pg_%' AND schemaname != 'public' 
           AND schemaname != 'information_schema'
             LOOP
               EXECUTE 'DROP SCHEMA ' || rec.schemaname || ' CASCADE'; 
             END LOOP; 
             RETURN; 
     END;
     $$ LANGUAGE plpgsql;
  
     SELECT drop_all();
  </code></pre>
  
  <p>Another variant: (this one <strong><em>probably</em></strong> works but still crashes my app)</p>
  
  <pre><code>SELECT string_agg(format('DROP SCHEMA %I CASCADE;', nspname), E'\n')
  FROM   pg_namespace WHERE nspname != 'public'
  AND nspname NOT LIKE 'pg_%'
  AND nspname != 'information_schema';
  </code></pre>
  
  <p>So, I wanted to make sure that the queries are correct or, hopefully, find another variant of the query.</p>
  
  <p>Could anybody help me with finding another query for doing that?</p>