1 无法删除用户
ERROR:角色不能被删除,因为有对象依赖于它
LINE 1:模式 public 的权限/用户下有表存在
postgres=# \du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------------------+-----------
ljfz | | {}
postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
postgres=# drop user ljfz;
ERROR: role "ljfz" cannot be dropped because some objects depend on it
DETAIL: privileges for schema public
owner of table ta
postgres=#
postgres=# revoke usage on schema public from ljfz;
REVOKE
postgres=# drop user ljfz;
ERROR: role "ljfz" cannot be dropped because some objects depend on it
DETAIL: owner of table ta
postgres=#
postgres=# \d
List of relations
Schema | Name | Type | Owner
--------+--------+-------+----------
public | mydata | table | postgres
public | ta | table | ljfz
(2 rows)
postgres=# drop table ta;
DROP TABLE
postgres=# drop user ljfz;
DROP ROLE
postgres=#
处理:收回赋予此用户的 模式 public 的权限,并删除用户下的表,即可正常删除。
2 无法删除数据库
ERROR:其他用户正在访问数据库
LINE 1:还有一个使用数据库的会话
[postgres@pgccc ~]$ psql
psql (15.2)
Type "help" for help.
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | ICU Locale | Locale Provider | Access privileges
-----------+----------+----------+-------------+-------------+------------+-----------------+-----------------------
postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | | libc |
subdb | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | | libc | =Tc/postgres +
| | | | | | | postgres=CTc/postgres+
| | | | | | | ljfz=c/postgres
template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | | libc | =c/postgres +
| | | | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | | libc | =c/postgres +
| | | | | | | postgres=CTc/postgres
(4 rows)
postgres=# drop database subdb;
ERROR: database "subdb" is being accessed by other users
DETAIL: There is 1 other session using the database.
postgres=# \q
[postgres@pgccc ~]$ ps -axjf |grep postgres
19326 26702 26702 19326 pts/0 13158 S 0 0:00 | \_ su - postgres
19042 24317 24317 19042 pts/1 516 S 0 0:00 \_ su - postgres
24318 517 516 19042 pts/1 516 S+ 1000 0:00 \_ grep --color=auto postgres
1 13926 13926 13926 ? -1 Ss 1000 0:00 /pgccc/app/15.2/bin/postgres -D /pgccc/pgdata
13926 13927 13927 13927 ? -1 Ss 1000 0:00 \_ postgres: checkpointer
13926 13928 13928 13928 ? -1 Ss 1000 0:00 \_ postgres: background writer
13926 13930 13930 13930 ? -1 Ss 1000 0:00 \_ postgres: walwriter
13926 13931 13931 13931 ? -1 Ss 1000 0:00 \_ postgres: autovacuum launcher
13926 13932 13932 13932 ? -1 Ss 1000 0:00 \_ postgres: logical replication launcher
13926 2771 2771 2771 ? -1 Ss 1000 0:00 \_ postgres: ljfz subdb [local] idle
[postgres@pgccc ~]$ kill -9 2771
[postgres@pgccc ~]$ psql
psql (15.2)
Type "help" for help.
postgres=# drop database subdb;
DROP DATABASE
postgres=#
处理:KILL 会话相关的进程
3 无法创建表
ERROR:schema public 的权限被拒绝。即权限缺失。
LINE 1:创建表... ...
[postgres@pgccc ~]$ psql
psql (15.2)
Type "help" for help.
postgres=#
postgres=# create user ljfz;
CREATE ROLE
postgres=# \c postgres ljfz
You are now connected to database "postgres" as user "ljfz".
postgres=> create table ta (id int);
ERROR: permission denied for schema public
LINE 1: create table ta (id int);
^
postgres=> \c postgres postgres
You are now connected to database "postgres" as user "postgres".
postgres=# grant create on schema public to ljfz;
GRANT
postgres=# \c postgres ljfz
You are now connected to database "postgres" as user "ljfz".
postgres=> create table ta(id int);
CREATE TABLE
postgres=>
处理:授权用户在 schema public 上的权限 create ,即可。(提示不能 create table,当然是缺少 create )