create or replace function clear_table_data() returns text as $$ declare select_result record; table_name varchar; begin for select_result in select relname as tablename from pg_class c where relkind = 'r' and relname not like 'pg_%' and relname not like 'sql_%' order by relname loop table_name = select_result.tablename; execute 'TRUNCATE TABLE ' || table_name; end loop; return 'success'; end; $$ language plpgsql; select * FROM clear_table_data();