简体   繁体   中英

How do I grant certain privileges on a database to a user?

I have a Postgres database with no tables. The database is owned by a user.

When I enter psql then \\dt in command line, here's what I get:

   Name    |    Owner    | Encoding |   Collate   |    Ctype    |   Access privileges   
-----------+-------------+----------+-------------+-------------+-----------------------
 somedb    | someuser    | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 

I want to give someuser the power to select, insert, update, delete, truncate, and create tables on somedb . When I run GRANT CREATE SELECT INSERT UPDATE DELETE TRUNCATE PRIVILEGES ON DATABASE somedb TO someuser; in the command line, I get this error:

LINE 1: GRANT CREATE SELECT INSERT UPDATE DELETE TRUNCATE PRIVILEGES...

What must I change to grant these privileges to someuser ?

You need to add comma between each privileges:

GRANT { { CREATE | TEMPORARY | TEMP } [,...] | ALL [ PRIVILEGES ] }
    ON DATABASE nombase [, ...]
    TO { nomutilisateur | GROUP nomgroupe | PUBLIC } [, ...] [ WITH
GRANT OPTION ]

But as a_horse_with_no_name said, these privileges can't be set at the database level.

Documentation

Tables are not created in "a database" - only in a schema.

If you want that user to create tables only in a specific schema then you need to grant usage on that schema:

grant usage, create on schema public to someuser;

You don't need to grant privileges for not yet created tables of that user. The user that creates a table is the owner of that table. The owner of a table can do anything with it. There is no need to grant the owner additional privileges.

If you want to allow the user to create new schemas, you need to grant the create privilege on the database:

grant create on database somedb to someuser;

If you want to allow the user to select any existing table in a schema, you need to explicitly grant that:

grant select,insert,update,delete on all tables in schema public to someuser;

If you want that user to also be able to do that for new tables that are not created by that user, you need to change the default privileges:

alter default privileges
   in schema public 
   grant select,insert,update,delete on tables
   to someuser;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM