CREATE OR REPLACE FUNCTION YOUR_SCHEMA.fn_returnalldata() RETURNS jsonb AS $$ DECLARE result jsonb := '{}'::jsonb; table_name text; table_json jsonb; BEGIN FOR table_name IN SELECT tablename FROM pg_tables WHERE schemaname = 'YOUR_SCHEMA' LOOP EXECUTE format( ' SELECT jsonb_build_object(%L, (SELECT row_to_json(row) FROM ( SELECT * FROM YOUR_SCHEMA.%I LIMIT 1 ) row))', table_name, table_name ) INTO table_json; result := result || table_json; END LOOP; RETURN result; END; $$ LANGUAGE plpgsql; select * from YOUR_SCHEMA.fn_returnalldata()