Monday, October 7, 2019

Permission Denied when Querying PostgreSQL Database

When I query a PostgreSQL database whose name is exampledb that has a table called exampletbl from PostgreSQL user exampleuser, I encountered "Permission Denied" error as follows,

exampledb=> SELECT * FROM exampletbl;
ERROR:  permission denied for relation exampletbl
exampledb=>

The solution is to grant necessary permissions to the user. In the following, I grant "All Permissions" to the user. The following is the command on the command line from a terminal window:

$ echo GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO exampleuser | \
  psql -U postgres exampledb

The key is to grant permissions to the tables in the database to the user, and we can complete this via the database admin user (postgres).

We may also need to grant the user permissions to "All Sequences" and "All Functions" as follows on the command line,

$ echo GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO exampleuser | \
  psql -U postgres exampledb
 
$ echo GRANT ALL PRIVILEGES ON ALL FUNCTIONS IN SCHEMA public TO exampleuser | \
  psql -U postgres exampledb 

No comments:

Post a Comment