How can I list all postgres rules using SQL?
Asked Answered
M

1

6

I have a postgres DB with SQL-only access (I mean I can connect using psql). I need to disable some rules, do maintenance and then enable them back. I'd like to assure that after all those operations all the rules are enabled. How could I list them using an SQL query?

Margarettamargarette answered 20/11, 2013 at 14:50 Comment(1)
depends on what Rules you're wanting to check but this may help for tables: https://mcmap.net/q/44964/-postgresql-quot-describe-table-quot or maybe: select * from pg_rules; would get the rules.Cynicism
A
13

Something like this should get you started:

select n.nspname as rule_schema, 
       c.relname as rule_table, 
       case r.ev_type  
         when '1' then 'SELECT' 
         when '2' then 'UPDATE' 
         when '3' then 'INSERT' 
         when '4' then 'DELETE' 
         else 'UNKNOWN' 
       end as rule_event
from pg_rewrite r  
  join pg_class c on r.ev_class = c.oid 
  left join pg_namespace n on n.oid = c.relnamespace 
  left join pg_description d on r.oid = d.objoid 
Australoid answered 20/11, 2013 at 14:53 Comment(1)
adding , rulename as rule_name after end as rule_event was also helpful for mePrismoid

© 2022 - 2024 — McMap. All rights reserved.