Search code examples
pythonsqlpostgresqlamazon-redshiftnavicat

Python connected to PostgreSql Redshift database, trying to run permission sequel but unable to. How can I grant permission or run permission query?


I'm trying to run a query using Python connected to PostgreSql using psycopg2. I'm able to connect but unable to run a query that gives permission which starts with 'grant select on ...'

I have tried doing regular 'select * from column limit 1' to see if the connection is made and that I'm able to do regular queries. I've tried 'grant select on view to user' but is not working.

permissions = "grant select on view_activities to username;"

try:
    cur.execute(permissions)
    contents = "The permissions have been run"
    message = twilioCli.messages.create(body = contents, from_=twil, to = me)
except:
    contents = "The permissions have failed"
    message = twilioCli.messages.create(body = contents, from_=twil, to = me)

The expected result is receiving a text message stating 'The permissions have been run' which indicates that this query has run without an issue. However, I'm getting the second result 'The permissions have failed' instead, which shows me that the query is not being run correctly. My theory is that postgres is unable to run Python's 'grant select...' syntax.


Solution

  • It seems like some part of your code(which is maybe not added here), is executing cursor.fetchall() and cursor.fetchone(), which gives the same exception for me psycopg2.ProgrammingError: no results to fetch . Once I removed the .fetchall() or .fetchone() , my code executed without any issues.

    Also, make sure you run connection.commit() after executing the GRANT, otherwise it'll be rolled back.