Today I Learned

PosgreSQL "DROP DATABASE.." RTFM

I have had over 200 databases to move from server to a new one. When a testing “import” succeeded I was ready for a final-final production move.

But I had to clean up the server first and remove all existing databases from “the testing” stage, I even created a SQL function:

DO $$ DECLARE
    db RECORD;
BEGIN
    FOR db IN (SELECT datname FROM pg_database where datdba != 10 AND datdba != 16385 LOOP
        EXECUTE 'DROP DATABASE IF EXISTS ' || quote_ident(db.tablename) ;
    END LOOP;
END $$;

but it did not work as PostgreSQL does not allow to drop a database from a function 😂

Finally, I’ve used a shell script to trigger DROP DATABASE.