pg_grant is a Python module for parsing, querying, and updating PostgreSQL
privileges.
Installation
$pipinstallpg_grant[sqlalchemy]
Without the SQLAlchemy extra, pg_grant can only parse access privileges.
Example
>>>frompg_grantimportparse_acl_item>>>parse_acl_item('bob=arw*/alice')Privileges(grantee='bob',grantor='alice',privs=['SELECT','INSERT'],privswgo=['UPDATE'])>>>fromsqlalchemyimportcreate_engine>>>frompg_grant.queryimportget_table_acl>>>engine=create_engine('postgresql://...')>>>get_table_acl(engine,'table2')SchemaRelationInfo(oid=138067,name='table2',owner='alice',acl=['alice=arwdDxt/alice','bob=arwdDxt/alice'],schema='public')>>>frompg_grantimportPgObjectType>>>frompg_grant.sqlimportrevoke>>>stmt=revoke(['SELECT'],PgObjectType.TABLE,'table2','bob')>>>str(stmt)'REVOKE SELECT ON TABLE table2 FROM bob'>>>engine.execute(stmt)