14

I’m using pgAdmin III on Mac Yosemite.
I created a role “discount” and a database “discount”.

In the pgadmin tool, how do I give the user “discount” connect privileges (and table read/write privileges) to the database “discount”?

Currently, when I try and login at the command line I get this error:-

davea$ psql -h localhost -d discount -U discount
Password for user discount: 
psql: FATAL:  permission denied for database "discount"
DETAIL:  User does not have CONNECT privilege.
greybeard
  • 2,249
  • 8
  • 30
  • 66
Dave
  • 15,639
  • 133
  • 442
  • 830
  • 2
    You can use GRANT command to give permissions [here](https://www.postgresql.org/docs/9.0/static/sql-grant.html) is official document – Sandesh Jain Aug 26 '16 at 15:31
  • This link has nothing to do with the pgadmin tool – Dave Aug 26 '16 at 15:34
  • The question has nothing to do with pgAdmin III either, I think. You can issue the missing `GRANT` statement with pgAdmin III if you like. – Laurenz Albe Aug 27 '16 at 04:26
  • Your problem is not looks like with the pgadmin but it's related to the permissions and so I provided you the way how you can grant proper permissions to the user so the user will have permission to access the database. – Sandesh Jain Aug 27 '16 at 05:15

2 Answers2

23

Permission to current objects

To have read/write access to the tables, you need to use GRANT command in 3 levels:

  1. DATABASE
  2. SCHEMA
  3. TABLES, FUNCTIONS, SEQUENCES, etc.

First, you need CONNECT privilege on the database:

GRANT CONNECT ON DATABASE <dbname> TO <username>;

Second, you need USAGE privilege on the schema inside the database (you must connect to the database before running it):

GRANT USAGE ON SCHEMA <schemaname> TO <username>;

At last, you can give the privilege on the tables, suppose you want common DML and SELECT on all tables in the schema, and other permissions:

GRANT SELECT,INSERT,UPDATE,DELETE ON ALL TABLES IN SCHEMA <schemaname> TO <username>;
-- and the sequences, for INSERT to work
GRANT USAGE ON ALL SEQUENCES IN SCHEMA <schemaname> TO <username>;
-- and the functions
GRANT EXECUTE ON ALL FUNCTIONS IN SCHEMA <schemaname> TO <username>;

Default permission for objects created in the future

You must now notice something. Each database, and each schema, and each object (table, function, etc.) have an owner. The owner of is the user that will manage and run DDL commands on it. Generally you should run all the above commands while connected as the user which owns everything, because this user already has all permissions (you could use a SUPERUSER too, but I recommend keeping it only for DBA tasks).

The above GRANT ... ON ALL ... IN SCHEMA commands will give permissions to the objects already present in the database, but won't apply to new objects created. In order to do that, you can use ALTER DEFAULT PRIVILEGES (I'll call it ADP) command. As before, you should run that while connected as the owner, because you must keep in mind that ADP is applied only if the owner of the new object matches with the one used here (or set in FOR ROLE clause):

ALTER DEFAULT PRIVILEGES IN SCHEMA <schemaname>
    GRANT SELECT,INSERT,UPDATE,DELETE ON TABLES TO <username>;
-- and the sequences, for INSERT to work
ALTER DEFAULT PRIVILEGES IN SCHEMA <schemaname>
    GRANT USAGE ON SEQUENCES TO <username>;
-- and the functions
ALTER DEFAULT PRIVILEGES IN SCHEMA <schemaname>
    GRANT EXECUTE ON FUNCTIONS TO <username>;

You can also skip IN SCHEMA <schemaname> from above and have it applied for any schema you have or create in the future. But again, be careful, even if you do not provide FOR ROLE <rolename> that means it will apply to the current user connected, so only objects created that is owned by that <rolename> will consider the ADP command.

For a good management of permissions, I highly recommend you keep an eye on which user owns the objects. In most cases, I also recommend you keep only one owner for everything inside the database (unless you are an advanced user and know what you are doing), that way permission management is easier.

MatheusOl
  • 10,870
  • 3
  • 30
  • 28
0

Make sure to verify the database name, to which you are trying to connect. I also faced the same issue initially, only to figure out later that I was connecting to a different database.

The one having the yellow-colored symbol will be your database.