446

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?

5
  • I am not sure how to update permissions so I can read/write in DB
    – Bob
    Mar 20, 2013 at 10:23
  • 1
    You need to grant the necessary privileges: postgresql.org/docs/current/static/ddl-priv.html and postgresql.org/docs/current/static/sql-grant.html
    – user330315
    Mar 20, 2013 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, 2013 at 4: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. Dec 12, 2014 at 5:51
  • 1
    This question is not off topic! It is, however, a duplication of stackoverflow.com/questions/13497352/…
    – wheaties
    Feb 17, 2015 at 20:05

12 Answers 12

542

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.

15
  • 12
    run it as a superuser, like postgres. Mar 20, 2013 at 12:21
  • 23
    Can this be shortcut somehow? GRANT ALL PRIVILEGES ON ALL TABLES ?
    – Shadur
    Apr 8, 2014 at 11:17
  • 240
    @Shadur GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO jerry;
    – Ron E
    Jun 9, 2014 at 2:29
  • 12
    @RonE is that restricted to the current database, though?
    – Shadur
    Jun 9, 2014 at 7:04
  • 8
    @zmiftah for schema you need > GRANT ALL PRIVILEGES ON SCHEMA nameSchema TO user;
    – Pierozi
    Sep 4, 2015 at 9:17
350

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;
3
  • 36
    You may also need to execute a similar command for ALL SEQUENCES and ALL FUNCTIONS.
    – Pistos
    Jun 27, 2017 at 5:37
  • 16
    Just for those who were wondering: ALL TABLES also includes views, so there is no separate ALL VIEWS command :-) Apr 3, 2018 at 6: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? Apr 19, 2021 at 16:21
132

Connect to the right database first, then run:

GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO jerry;
3
  • 51
    connect to right database makes a HUGE dufference :) \connect databasename; Jan 22, 2018 at 10:54
  • 7
    THIS. I was connected to "postgres" all along. Thank You!
    – Andrew G.
    Feb 5, 2018 at 4:28
  • 3
    Yes connecting to the DB made the difference
    – inostia
    Apr 15, 2019 at 18:07
97
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;
1
  • Did exactly this, but still get permission denied when trying to select from tables in schema public as user jerry ...WTF?
    – syr
    Nov 30, 2022 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;
0
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

2
  • as well as references, truncate, trigger Feb 15, 2022 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 2, 2022 at 7: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>;
1
  • Very nice! It's painful to know this, but this is what I had ended up doing. :D (facepalm)
    – ankush981
    Dec 27, 2021 at 13:53
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

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.