I have such query to list tables in current database:
SELECT c.relname
FROM pg_catalog.pg_class c
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnames
You need to use dynamic SQL for this, which in turn can only be used in a procedural language like PL/pgSQL, something like this:
do
$$
declare
stmt text;
table_rec record;
begin
for table_rec in (SELECT c.relname as tname
FROM pg_catalog.pg_class c
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE c.relkind IN ('r','')
AND n.nspname NOT IN ('pg_catalog', 'pg_toast')
AND pg_catalog.pg_table_is_visible(c.oid))
loop
execute 'drop table '||table_rec.tname||' cascade';
end loop;
end;
$$