- You can't check table existence like this. Use pg_class system table to check if table exists
- You have to increment i inside your function, or you'll end up with infinite loop
create or replace function test (numoftables int) returns void as $$
declare
i integer;
begin
i:=0;
while i< numoftables loop
if not exists (select * from pg_class where relname = 'table' || i::text)
then
execute '
create table table' || i::text || '(
column1 int,
column2 int,
column3 text,
column4 text,
column5 text);
';
end if;
i := i + 1;
end loop;
end;
$$
language 'plpgsql';
I've tried not changed your logic or syntax much, so it's still your function.