create view permisos as
SELECT 
	n.nspname AS schema, 
	c.relname AS entidad, 
	c.relkind AS type,
	r.rolname AS owner, 
	COALESCE(c.relacl::text, ((('{'::text || r.rolname::text) || '=arwdxt/'::text) || r.rolname::text) || '}'::text) AS permisos
FROM 
	pg_class c 
JOIN 
	pg_roles r ON r.oid = c.relowner
LEFT JOIN 
	pg_namespace n ON n.oid = c.relnamespace
WHERE 
	(c.relkind = ANY (ARRAY['r'::"char", 'v'::"char", 'S'::"char",''::"char"])) 
	AND n.nspname <> 'pg_catalog'::name 
	AND n.nspname !~ '^pg_toast'::text AND pg_table_is_visible(c.oid)
ORDER BY 
	c.relkind;


create view usuarios_permisos as
SELECT 
	permisos.schema, 
	permisos.entidad, 
	permisos.type, 
	permisos.owner, 
	regexp_replace(regexp_split_to_table("substring"(permisos.permisos, 2, length(permisos.permisos) - 2), ','::text), '/'::text || permisos.owner::text, ''::text) AS permisos
FROM 
	permisos;

create view detalles_permisos as
SELECT 
	usuarios_permisos.schema, 
	usuarios_permisos.entidad, 
	usuarios_permisos.type, 
	usuarios_permisos.owner, 
	"substring"(usuarios_permisos.permisos, 1, strpos(usuarios_permisos.permisos, '='::text) - 1) AS username, 
	regexp_split_to_table("substring"(usuarios_permisos.permisos, strpos(usuarios_permisos.permisos, '='::text) + 1, length(usuarios_permisos.permisos)), ''::text) AS permisos
FROM 
	usuarios_permisos;
