Permission denied for relation in PostgreSQL
Asked Answered
A

13

451

I tried to run simple SQL command:

SELECT * FROM site_adzone;

And I got this error:

ERROR: permission denied for relation site_adzone

What could be the problem here?

I tried also to do select for other tables and got same issue. I also tried to do this:

GRANT ALL PRIVILEGES ON DATABASE jerry TO tom;

But I got this response from console:

WARNING: no privileges were granted for "jerry"

Does anyone have any idea what can be wrong?

Arcade answered 20/3, 2013 at 10:0 Comment(5)
I am not sure how to update permissions so I can read/write in DBArcade
You need to grant the necessary privileges: postgresql.org/docs/current/static/ddl-priv.html and postgresql.org/docs/current/static/sql-grant.htmlAcanthocephalan
Welcome to SO! For this question, you may get more help over at dba.stackexchange.com, though you may find just as many snide comments :)Penetrating
Excuse me. This is the second very programming-related postgresql question I've seen closed as off-topic tonight! The last one had 67,000 views, this one 30,000 views. We should have a popularity clause: Any non-subjective question with > 15,000 views = on topic.N
This question is not off topic! It is, however, a duplication of #13497852Mandiemandingo
S
550

GRANT on the database is not what you need. Grant on the tables directly.

Granting privileges on the database mostly is used to grant or revoke connect privileges. This allows you to specify who may do stuff in the database if they have sufficient other permissions.

You want instead:

 GRANT ALL PRIVILEGES ON TABLE side_adzone TO jerry;

This will take care of this issue.

Saturnian answered 20/3, 2013 at 11:41 Comment(15)
I tried this grant all privileges on table site_adzone to jerry; and got this response ERROR: permission denied for relation site_adzoneArcade
run it as a superuser, like postgres.Saturnian
Can this be shortcut somehow? GRANT ALL PRIVILEGES ON ALL TABLES ?Keheley
@Shadur GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO jerry;Tehuantepec
@RonE is that restricted to the current database, though?Keheley
Thank you, this really help. How about schema?Alps
@zmiftah for schema you need > GRANT ALL PRIVILEGES ON SCHEMA nameSchema TO user;Asymptotic
@Ron E, add your comment as answer to this question pleaseMargarethe
Would like re-iterate the question asked by @Shadur "Is that restricted to the current database?"Aureaaureate
Okay got my answer, may be it obvious to everyone else but not to me. I executed the command and got response GRANT but it did not allow me run any query. After a lot of trial and error I realized I have to connect to the database as superuser first and then grant permission. So answer to @Shadur 's question is YES it is definitely restricted to the current database.Aureaaureate
You might also need this (replace TABLES with SEQUENCES): GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO jerry;Casiecasilda
I am using inspectdb command to generate models. The aforementioned error has gone but a new error has popped up - Unable to inspect table 'table_name' # The error was: list index out of rangeSuburbanize
As written on an answer bellow you need to connecto the the right database inside the psql terminal first: \connect databasename;Pieter
I got Error in query: ERROR: schema "db_name" does not exist after I ran GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA db_name TO user.Allowance
@Aureaaureate on top of current database, it is restricted to current tables as well! i.e. if you are adding a new table you have to run this again. Found using some trial and error only :)Gorrian
O
359

Posting Ron E answer for grant privileges on all tables as it might be useful to others.

GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO jerry;
Odontoblast answered 20/3, 2013 at 10:0 Comment(3)
You may also need to execute a similar command for ALL SEQUENCES and ALL FUNCTIONS.Boletus
Just for those who were wondering: ALL TABLES also includes views, so there is no separate ALL VIEWS command :-)Transcaucasia
stupid question, but how come simply GRANT ALL PRIVILEGES ON SCHEMA public TO jerry; does not give select access on a table inside the schema?Humiliating
U
135

Connect to the right database first, then run:

GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO jerry;
Underpainting answered 1/1, 2017 at 20:0 Comment(3)
connect to right database makes a HUGE dufference :) \connect databasename;Kerbing
THIS. I was connected to "postgres" all along. Thank You!Dispirit
Yes connecting to the DB made the differenceTorry
F
98
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public to jerry;
GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public to jerry;
GRANT ALL PRIVILEGES ON ALL FUNCTIONS IN SCHEMA public to jerry;
Freewheel answered 18/7, 2018 at 10:36 Comment(1)
Did exactly this, but still get permission denied when trying to select from tables in schema public as user jerry ...WTF?Lizarraga
L
52

1st and important step is connect to your db:

psql -d yourDBName

2 step, grant privileges

GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO userName;
Lionel answered 2/5, 2018 at 13:5 Comment(0)
B
31

To grant permissions to all of the existing tables in the schema use:

GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA <schema> TO <role>

To specify default permissions that will be applied to future tables use:

ALTER DEFAULT PRIVILEGES IN SCHEMA <schema> 
  GRANT <privileges> ON TABLES TO <role>;

e.g.

ALTER DEFAULT PRIVILEGES IN SCHEMA public 
  GRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO admin;

If you use SERIAL or BIGSERIAL columns then you will probably want to do the same for SEQUENCES, or else your INSERT will fail (Postgres 10's IDENTITY doesn't suffer from that problem, and is recommended over the SERIAL types), i.e.

ALTER DEFAULT PRIVILEGES IN SCHEMA <schema> GRANT ALL ON SEQUENCES TO <role>;

See also my answer to PostgreSQL Permissions for Web App for more details and a reusable script.

Ref:

GRANT

ALTER DEFAULT PRIVILEGES

Begat answered 9/10, 2017 at 18:31 Comment(2)
as well as references, truncate, triggerEudoca
This needs to be way higher up, this is what did the trick for me, the usual grant was being executed, but due to no tables being there yet it did not take any effect. Perhaps this is also something version specific.Illustrate
K
18

This frequently happens when you create a table as user postgres and then try to access it as an ordinary user. In this case it is best to log in as the postgres user and change the ownership of the table with the command:

alter table <TABLE> owner to <USER>;
Kameko answered 22/5, 2018 at 4:13 Comment(1)
Very nice! It's painful to know this, but this is what I had ended up doing. :D (facepalm)Oaxaca
A
8

Make sure you log into psql as the owner of the tables. to find out who own the tables use \dt

psql -h CONNECTION_STRING DBNAME -U OWNER_OF_THE_TABLES

then you can run the GRANTS

Attalie answered 15/5, 2018 at 17:30 Comment(0)
L
8

You should:

  1. Connect to the database using DBeaver and user as postgres
  2. On the left tab open your database
  3. Open Roles tab/dropdown
  4. Select your user
  5. On the right tab press 'Permissions tab'
  6. Press your schema tab
  7. Press tables tab/dropdown
  8. Select all tables
  9. Select all required permissions checkboxes (or press Grant All)
  10. Press Save
Ladyinwaiting answered 19/3, 2020 at 8:3 Comment(0)
Z
3

For PostgreSQL. On bash terminal, run this:

psql db_name -c "GRANT ALL ON ALL TABLES IN SCHEMA public to db_user;"
psql db_name -c "GRANT ALL ON ALL SEQUENCES IN SCHEMA public to db_user;"
psql db_name -c "GRANT ALL ON ALL FUNCTIONS IN SCHEMA public to db_user;"
Zales answered 18/11, 2022 at 11:0 Comment(0)
E
2

As you are looking for select permissions, I would suggest you to grant only select rather than all privileges. You can do this by:

GRANT SELECT ON <table> TO <role>;
Eijkman answered 8/1, 2020 at 15:33 Comment(0)
M
1

I ran into this after switching a user to another user that also needed to have the same rights, I kept getting the error: "must be owner of relation xx"

fix was to simply give all rights from old user to new user:

postgres-# Grant <old user> to <new user>;

Monkey answered 20/8, 2021 at 19:2 Comment(0)
P
0

For example, you can grant the user john on person table with GRANT statement as shown below:

GRANT ALL PRIVILEGES ON TABLE person TO john;

Or, you can grant the user john on all tables in public schema with GRANT statement as shown below:

GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO john;

Or, you can alter person table's owner to the user john on person table with ALTER TABLE statement as shown below:

ALTER TABLE person OWNER TO john;

*Memos:

  • You can omit PRIVILEGES which is optional.

  • You must log in with any superusers(e.g., postgres).

Be careful, even if you grant the user john on public schema with GRANT statement as shown below:

GRANT ALL PRIVILEGES ON SCHEMA public TO john;

Or, even if you grant the user john on apple database with GRANT statement as shown below:

GRANT ALL PRIVILEGES ON DATABASE apple TO john;

Or, even if you alter public schema's owner to the user john with ALTER SCHEMA statement as shown below:

ALTER SCHEMA public OWNER TO john;

Or, even if you alter apple database's owner to the user john with ALTER DATABASE statement as shown below:

ALTER DATABASE apple OWNER TO john;

Then, you cannot solve the error as shown below:

apple=> SELECT * FROM person;
ERROR:  permission denied for table person
Posse answered 20/1 at 23:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.