postgres=# \du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------------------+-----------
brent | | {suq}
postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
suq | 1 connection | {}
zdry | Superuser +| {}
| Password valid until infinity |
test=# \h create user
Command: CREATE USER
Description: define a new database role
Syntax:
CREATE USER name [ [ WITH ] option [ ... ] ]
where option can be:
SUPERUSER | NOSUPERUSER
| CREATEDB | NOCREATEDB
| CREATEROLE | NOCREATEROLE
| INHERIT | NOINHERIT --继承
| LOGIN | NOLOGIN
| REPLICATION | NOREPLICATION
| BYPASSRLS | NOBYPASSRLS
| CONNECTION LIMIT connlimit
| [ ENCRYPTED ] PASSWORD 'password'
| VALID UNTIL 'timestamp'
| IN ROLE role_name [, ...]
| IN GROUP role_name [, ...]
| ROLE role_name [, ...]
| ADMIN role_name [, ...]
| USER role_name [, ...]
| SYSID uid
postgres=# create user test ENCRYPTED password 'test';
CREATE ROLE
test=# create user dsg superuser;
CREATE ROLE
test=# create user dsg createdb createrole inherit password 'dsg';
CREATE ROLE
test=# \h alter user
Command: ALTER USER
Description: change a database role
Syntax:
ALTER USER role_specification [ WITH ] option [ ... ]
where option can be:
SUPERUSER | NOSUPERUSER
| CREATEDB | NOCREATEDB
| CREATEROLE | NOCREATEROLE
| INHERIT | NOINHERIT
| LOGIN | NOLOGIN
| REPLICATION | NOREPLICATION
| BYPASSRLS | NOBYPASSRLS
| CONNECTION LIMIT connlimit
| [ ENCRYPTED ] PASSWORD 'password'
| VALID UNTIL 'timestamp'
ALTER USER name RENAME TO new_name
ALTER USER { role_specification | ALL } [ IN DATABASE database_name ] SET configuration_parameter { TO | = } { value | DEFAULT }
ALTER USER { role_specification | ALL } [ IN DATABASE database_name ] SET configuration_parameter FROM CURRENT
ALTER USER { role_specification | ALL } [ IN DATABASE database_name ] RESET configuration_parameter
ALTER USER { role_specification | ALL } [ IN DATABASE database_name ] RESET ALL
where role_specification can be:
role_name
| CURRENT_USER
| SESSION_USER
postgres=# alter user test superuser;
ALTER ROLE
postgres=# alter user test nosuperuser;
ALTER ROLE
test=# alter user dsg password 'test';
ALTER ROLE
test=# alter user dsg rename to dds;
NOTICE: MD5 password cleared because of role rename
ALTER ROLE
test=# alter user test nologin;
ALTER ROLE
test=# alter user test login;
ALTER ROLE
test=# alter user test connection limit 10;
ALTER ROLE
test=# drop user dds;
DROP ROLE
test=# drop user dsg;
ERROR: role "dsg" cannot be dropped because some objects depend on it
DETAIL: owner of table zzz.kkk
privileges for schema zzz
test=# reassign owned by dsg to test;
REASSIGN OWNED
test=# revoke all on schema zzz from dsg;
REVOKE
test=# drop user dsg;
DROP ROLE
test=# \dn
List of schemas
Name | Owner
--------+----------
brent | brent
public | postgres
suq | suq
zzz | test
(4 rows)
test=# \h create schema
Command: CREATE SCHEMA
Description: define a new schema
Syntax:
CREATE SCHEMA schema_name [ AUTHORIZATION role_specification ] [ schema_element [ ... ] ]
CREATE SCHEMA AUTHORIZATION role_specification [ schema_element [ ... ] ]
CREATE SCHEMA IF NOT EXISTS schema_name [ AUTHORIZATION role_specification ]
CREATE SCHEMA IF NOT EXISTS AUTHORIZATION role_specification
where role_specification can be:
user_name
| CURRENT_USER
| SESSION_USER
test=# create schema zzz authorization test;
CREATE SCHEMA
test=# drop schema zzz;
ERROR: cannot drop schema zzz because other objects depend on it
DETAIL: table zzz.test depends on schema zzz
HINT: Use DROP ... CASCADE to drop the dependent objects too.
test=# drop schema zzz cascade;
NOTICE: drop cascades to table zzz.test
DROP SCHEMA
test=# grant create on schema zzz to brent;
GRANT
test=> grant usage on schema zzz to brent;
GRANT
test=> grant all on schema zzz to brent;
GRANT
test=> select user;
user
-------
brent
test=> select * from zzz.abc;
ERROR: permission denied for relation abc
test=# grant select,insert on zzz.abc to brent;
GRANT
(1 row)
test=> \c test brent
You are now connected to database "test" as user "brent".
test=> select * from zzz.abc;
id
----
(0 rows)
test=# set search_path=zzz;
SET
test=# \dt
List of relations
Schema | Name | Type | Owner
--------+------+-------+-------
zzz | abc | table | test
zzz | kkk | table | test
(2 rows)
test=# revoke select on zzz.abc from brent;
REVOKE
test=# grant test to brent;
GRANT ROLE
test=# revoke test from brent;
REVOKE ROLE
test=# \dp abc
Access privileges
Schema | Name | Type | Access privileges | Column privileges | Policies
--------+------+-------+-------------------+-------------------+----------
zzz | abc | table | test=arwdDxt/test+| |
| | | brent=a/test +| |
| | | uuu=arwdDxt/test | |
(1 row)
test=# \du brent
List of roles
Role name | Attributes | Member of
-----------+------------+------------
brent | | {uuu,test}
test=# select * from information_schema.role_table_grants where grantee='brent';
grantor | grantee | table_catalog | table_schema | table_name | privilege_type | is_grantable | with_hierarchy
---------+---------+---------------+--------------+------------+----------------+--------------+----------------
brent | brent | test | brent | x | INSERT | YES | NO
brent | brent | test | brent | x | SELECT | YES | YES
brent | brent | test | brent | x | UPDATE | YES | NO
brent | brent | test | brent | x | DELETE | YES | NO
brent | brent | test | brent | x | TRUNCATE | YES | NO
brent | brent | test | brent | x | REFERENCES | YES | NO
brent | brent | test | brent | x | TRIGGER | YES | NO
brent | brent | test | brent | tt | INSERT | YES | NO
brent | brent | test | brent | tt | SELECT | YES | YES
brent | brent | test | brent | tt | UPDATE | YES | NO
brent | brent | test | brent | tt | DELETE | YES | NO
brent | brent | test | brent | tt | TRUNCATE | YES | NO
brent | brent | test | brent | tt | REFERENCES | YES | NO
brent | brent | test | brent | tt | TRIGGER | YES | NO
test | brent | test | zzz | abc | INSERT | YES | NO
(15 rows)