444

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?

Dale K
  • 25,246
  • 15
  • 42
  • 71
Bob
  • 8,392
  • 12
  • 55
  • 96
  • I am not sure how to update permissions so I can read/write in DB – Bob Mar 20 '13 at 10:23
  • 1
    You need to grant the necessary privileges: http://www.postgresql.org/docs/current/static/ddl-priv.html and http://www.postgresql.org/docs/current/static/sql-grant.html –  Mar 20 '13 at 10:32
  • 3
    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 :) – Jared Beck Mar 28 '13 at 04:09
  • 28
    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. – Theodore R. Smith Dec 12 '14 at 05:51
  • 1
    This question is not off topic! It is, however, a duplication of http://stackoverflow.com/questions/13497352/error-permission-denied-for-relation-tablename-on-postgres-while-trying-a-selec – wheaties Feb 17 '15 at 20:05

12 Answers12

539

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.

Chris Travers
  • 25,424
  • 6
  • 65
  • 182
  • I tried this grant all privileges on table site_adzone to jerry; and got this response ERROR: permission denied for relation site_adzone – Bob Mar 20 '13 at 12:19
  • 12
    run it as a superuser, like postgres. – Chris Travers Mar 20 '13 at 12:21
  • 23
    Can this be shortcut somehow? GRANT ALL PRIVILEGES ON ALL TABLES ? – Shadur Apr 08 '14 at 11:17
  • 240
    @Shadur GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO jerry; – Ron E Jun 09 '14 at 02:29
  • 12
    @RonE is that restricted to the current database, though? – Shadur Jun 09 '14 at 07:04
  • Thank you, this really help. How about schema? – Zein Miftah Feb 27 '15 at 04:14
  • 8
    @zmiftah for schema you need > GRANT ALL PRIVILEGES ON SCHEMA nameSchema TO user; – Pierozi Sep 04 '15 at 09:17
  • @Ron E, add your comment as answer to this question please – Oleg Abrazhaev Feb 16 '16 at 08:57
  • 1
    Would like re-iterate the question asked by @Shadur "Is that restricted to the current database?" – jazaman May 31 '16 at 04:03
  • 6
    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. – jazaman May 31 '16 at 06:25
  • 4
    You might also need this (replace `TABLES` with `SEQUENCES`): `GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO jerry;` – Nick Jun 16 '16 at 17:09
  • 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 range – JDP Sep 17 '16 at 04:14
  • 1
    As written on an answer bellow you need to connecto the the right database inside the psql terminal first: `\connect databasename;` – Ole Henrik Skogstrøm Feb 14 '19 at 14:09
  • 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`. – Paul Dec 10 '19 at 21:44
  • @jazaman 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 :) – Mahesh Thumar Mar 15 '20 at 04:50
348

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;
sag
  • 5,333
  • 8
  • 54
  • 91
  • 36
    You may also need to execute a similar command for `ALL SEQUENCES` and `ALL FUNCTIONS`. – Pistos Jun 27 '17 at 05:37
  • 16
    Just for those who were wondering: `ALL TABLES` also includes views, so there is no separate `ALL VIEWS` command :-) – André Gasser Apr 03 '18 at 06:24
  • 3
    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? – lollerskates Apr 19 '21 at 16:21
132

Connect to the right database first, then run:

GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO jerry;
BrDaHa
  • 5,138
  • 5
  • 32
  • 47
user2757813
  • 1,329
  • 1
  • 8
  • 3
96
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;
Meirza
  • 1,318
  • 10
  • 15
  • Did exactly this, but still get permission denied when trying to select from tables in schema `public` as user `jerry` ...WTF? – syr Nov 30 '22 at 11:48
51

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;
zond
  • 1,473
  • 1
  • 21
  • 34
30

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

isapir
  • 21,295
  • 13
  • 115
  • 116
  • as well as `references, truncate, trigger` – Josh Hibschman Feb 15 '22 at 22:52
  • 1
    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. – BuzZin' May 02 '22 at 07:53
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>;
salvador
  • 107
  • 4
Bruce
  • 449
  • 4
  • 6
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

Brian McCall
  • 1,831
  • 1
  • 18
  • 33
7

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
happydmitry
  • 111
  • 2
  • 5
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>;
Sergi Ramón
  • 185
  • 1
  • 4
2

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;"
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>;

Jens Timmerman
  • 9,316
  • 1
  • 42
  • 48