42501: INSUFFICIENT PRIVILEGE ERROR while querying in Postgresql

后端 未结 4 1371
旧巷少年郎
旧巷少年郎 2021-01-17 09:41

I am trying to query a database table in postgresql, but every time I run the below query it gives me the INSUFFICIENT PRIVILEGE error. What possibly could be the reason for

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-17 10:17

    You need to make sure that the user with which you are connecting with also has the "USAGE" access on the schema you are trying to access with the user. I have recently faced an error where I got the dump restored into a database and then had some users to whom I was only supposed to provide the read-only access. I have followed the following steps -

    CREATE ROLE myapp_readonly;
    GRANT CONNECT ON DATABASE {database} TO myapp_readonly;
    GRANT USAGE ON SCHEMA {schema} TO myapp_readonly;
    GRANT SELECT ON TABLE {schema}.{table_name} TO myapp_readonly;
    GRANT myapp_readonly TO {usre};
    

    After performing these steps when I tried to access the table, had received the following error -

    SQL Error [42501]: ERROR: permission denied for schema {schema}
    

    In my case, my users were available already and the schemas and the database were restored recently. After I have provided the "USAGE" access to the schema to the user the error was resolved.

提交回复
热议问题