Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sqlalchemy grant permission in Base.metadata.create_all

Is there a way to grant permissions when creating a new table using sqlalchemy? The only way I see to do it now is to execute an sql "GRANT ALL ON TABLE blabla to whomever;" everytime I do a Base.metadata.create_all(...)

like image 427
CrabbyPete Avatar asked Aug 10 '15 19:08

CrabbyPete


1 Answers

I use PostgreSQL database with psycopg2 backend. After create_all I execute:

import models
from psycopg2 import sql

for table in models.Base.metadata.tables.values():
    connection.connection.cursor().execute(
        sql.SQL('GRANT ALL PRIVILEGES ON {} TO {}').format(
            sql.Identifier(table.name), sql.Identifier('bob')))
like image 167
maciek Avatar answered Oct 14 '22 05:10

maciek