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.
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 .
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.
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:
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 ...;
Access to the schemas:
For all schemas, run
GRANT ALL ON SCHEMA ... TO ...;
Access to the tables:
For all schemas, run
GRANT ALL ON ALL TABLES IN SCHEMA ... TO ...;
Access to the sequences:
For all schemas, tun
GRANT ALL ON ALL SEQUENCES IN SCHEMA ... TO ...;
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With