Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

postgres 9.6 set give user privileges to database

So this should be easy but I cannot figure this out. I am using postgres 9.6 I have a postgres user and a database. I want to give that user the ability to do ANYTHING on a given database only. I have tried the following commands which don't work:

GRANT ALL PRIVILEGES ON DATABASE mydatabase TO myuser;
ALTER DATABASE mydatabase OWNER TO myuser;

The documentation seems pretty clear that ALL PRIVILEGES should include update, insert etc, but then when I log in the user can't actually do anything. I have also seen examples such as :

GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO user_name;

But im not sure how to restrict that to one database? Any help is greatly appreciates.

like image 790
noone392 Avatar asked Jan 18 '18 21:01

noone392


People also ask

How do I change user privileges in PostgreSQL?

The basic format of ALTER USER includes the name of the user (or ROLE ) followed by a series of options to inform PostgreSQL which permissive alterations to make: =# ALTER USER role_specification WITH OPTION1 OPTION2 OPTION3; These options range from CREATEDB , CREATEROLE , CREATEUSER , and even SUPERUSER .

How do I fix Postgres permission denied?

Grant privileges to a new user We resolve this permission denied error using the command. GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO new_user; The new_user was then able to read data from the table. Similarly, we can also resolve the permission denied error by setting DEFAULT privileges to the user.


1 Answers

In PostgreSQL the only way to allow users access to all objects in a database with a single statement is to give them superuser privileges. But you should not do that.

Proceed in stages:

  1. Access to the database:

    By default, everybody has access to all databases anyway, and you'd configure that in pg_hba.conf. If you have removed the permissions for PUBLIC on the database, use

    GRANT ALL ON DATABASE ... TO ...;
    
  2. Access to the schemas:

    For all schemas, run

    GRANT ALL ON SCHEMA ... TO ...;
    
  3. Access to the tables:

    For all schemas, run

    GRANT ALL ON ALL TABLES IN SCHEMA ... TO ...;
    
  4. Access to the sequences:

    For all schemas, tun

    GRANT ALL ON ALL SEQUENCES IN SCHEMA ... TO ...;
    
  5. Access to the functions:

    By default, functions are created with EXECUTE privileges for PUBLIC. If that has been removed, grant access with

    GRANT ALL ON ALL FUNCTIONS IN SCHEMA ... TO ...;
    

There are other, less frequently used things like large objects or foreign servers. You'd have to grant privileges for those as well. Look at the documentation for GRANT for the details.

like image 128
Laurenz Albe Avatar answered Nov 02 '22 23:11

Laurenz Albe