• 技术文章 >数据库 >PostgreSQL

    postgresql数据库怎么删除用户

    silencementsilencement2020-03-13 20:57:33原创5479

    本文将介绍PostgreSQL 下如何快速删除一个用户(role)。

    一般情况下直接执行 drop role xxx; 就可以把这个用户删除。但是很多时候会因为用户有依赖而报错。

    推荐:postgresql教程

    权限依赖

    postgres=# create role test with login;
    CREATE ROLE
    postgres=# grant all on database postgres to test;
    GRANT
    postgres=# drop role test;
    ERROR:  role "test" cannot be dropped because some objects depend on it
    DETAIL:  privileges for database postgres

    可以看出,因为我们把数据库postgres 的权限赋予了test 用户,所以直接删除的时候会报错。面对这种情况,我们需要先将role 的权限

    所有的权限全部revoke 掉,如下:

    postgres=# revoke all on database postgres from test;
    REVOKE
    postgres=# drop role test;
    DROP ROLE

    注意:需要把该用户在所有数据库具有权限的所有数据库对象的(表,视图,SEQUENCE)权限全部回收,才能删除该用户。

    对象依赖

    postgres=# create role test with login;
    CREATE ROLE
    postgres=# \c - test
    You are now connected to database "postgres" as user "test".
    postgres=> create table test (id int);
    CREATE TABLE
    postgres=# \c - postgres
    You are now connected to database "postgres" as user "postgres".
    postgres=# drop role test;
    ERROR:  role "test" cannot be dropped because some objects depend on it
    DETAIL:  owner of table test

    可以看出,因为test 用户是test 表的owner,所以删除的时候报错owner of table test。如果不需要保留该对象,则需要先把该依赖对象

    删除。如果需要保留该对象,则应该在删除之前先把owner 赋予别人,如下:

    postgres=# alter table test OWNER  TO postgres;
    ALTER TABLE
    postgres=# drop role test;
    DROP ROLE

    注意:需要把该用户在所有数据库具有owner 权限的所有数据库对象(表,视图,SEQUENCE)删除或者执行alter xx owner to,才能

    删除该用户。

    更多技术请关注Python视频教程

    专题推荐:数据库
    品易云
    上一篇:postgresql异常怎么重启 下一篇:postgresql打不开怎么办

    相关文章推荐

    • postgresql表空间是什么• postgresql中怎么抛出异常• 怎么确定postgresql是否安装

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网