• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • MsSql
  • Mysql
  • oracle
  • MariaDB
  • DB2
  • SQLite
  • PostgreSQL
  • MongoDB
  • Redis
  • Access
  • 数据库其它
  • sybase
  • HBase
您的位置:首页 > 数据库 >PostgreSQL > PostgreSQL 角色与用户管理介绍

PostgreSQL 角色与用户管理介绍

作者: 字体:[增加 减小] 来源:互联网 时间:2017-05-11

通过本文主要向大家介绍了postgresql 超级用户,postgresql默认用户名,postgresql 切换用户,postgresql创建用户,postgresql 查看用户等相关知识,希望本文的分享对您有所帮助

一、角色与用户的区别

角色就相当于岗位:角色可以是经理,助理。
用户就是具体的人:比如陈XX经理,朱XX助理,王XX助理。
在PostgreSQL 里没有区分用户和角色的概念,"CREATE USER" 为 "CREATE ROLE" 的别名,这两个命令几乎是完全相同的,唯一的区别是"CREATE USER" 命令创建的用户默认带有LOGIN属性,而"CREATE ROLE" 命令创建的用户默认不带LOGIN属性(CREATE USER is equivalent to CREATE ROLE except that CREATE USER assumes LOGIN by default, while CREATE ROLE does not)。

1.1 创建角色与用户

CREATE ROLE 语法

CREATE ROLE name [ [ WITH ] option [ ... ] ]
where option can be:
      SUPERUSER | NOSUPERUSER
    | CREATEDB | NOCREATEDB
    | CREATEROLE | NOCREATEROLE
    | CREATEUSER | NOCREATEUSER
    | INHERIT | NOINHERIT
    | LOGIN | NOLOGIN
    | REPLICATION | NOREPLICATION
    | CONNECTION LIMIT connlimit
    | [ ENCRYPTED | UNENCRYPTED ] PASSWORD 'password'
    | VALID UNTIL 'timestamp'
    | IN ROLE role_name [, ...]
    | IN GROUP role_name [, ...]
    | ROLE role_name [, ...]
    | ADMIN role_name [, ...]
    | USER role_name [, ...]
    | SYSID uid

创建david 角色和sandy 用户
postgres=# CREATE ROLE david;  //默认不带LOGIN属性
CREATE ROLE
postgres=# CREATE USER sandy;  //默认具有LOGIN属性
CREATE ROLE
postgres=# \du
                             List of roles
 Role name |                   Attributes                   | Member of
-----------+------------------------------------------------+-----------
 david     | Cannot login                                   | {}
 postgres  | Superuser, Create role, Create DB, Replication | {}
 sandy     |                                                | {}

postgres=#
postgres=# SELECT rolname from pg_roles ;
 rolname 
----------
 postgres
 david
 sandy
(3 rows)

postgres=# SELECT usename from pg_user;         //角色david 创建时没有分配login权限,所以没有创建用户
 usename 
----------
 postgres
 sandy
(2 rows)

postgres=#
1.2 验证LOGIN属性
postgres@CS-DEV:~> psql -U david
psql: FATAL:  role "david" is not permitted to log in
postgres@CS-DEV:~> psql -U sandy
psql: FATAL:  database "sandy" does not exist
postgres@CS-DEV:~> psql -U sandy -d postgres
psql (9.1.0)
Type "help" for help.

postgres=> \dt
No relations found.
postgres=>
用户sandy 可以登录,角色david 不可以登录。
1.3 修改david 的权限,增加LOGIN权限
postgres=# ALTER ROLE david LOGIN ;
ALTER ROLE
postgres=# \du
                             List of roles
 Role name |                   Attributes                   | Member of
-----------+------------------------------------------------+-----------
 david     |                                                | {}
 postgres  | Superuser, Create role, Create DB, Replication | {}
 sandy     |                                                | {}

postgres=# SELECT rolname from pg_roles ;
 rolname 
----------
 postgres
 sandy
 david
(3 rows)

postgres=# SELECT usename from pg_user;  //给david 角色分配login权限,系统将自动创建同名用户david
 usename 
----------
 postgres
 sandy
 david
(3 rows)

postgres=#
1.4 再次验证LOGIN属性
postgres@CS-DEV:~> psql -U david -d postgres
psql (9.1.0)
Type "help" for help.

postgres=> \du
                             List of roles
 Role name |                   Attributes                   | Member of
-----------+------------------------------------------------+-----------
 david     |                                                | {}
 postgres  | Superuser, Create role, Create DB, Replication | {}
 sandy     |                                                | {}

postgres=>
david 现在也可以登录了。

二、查看角色信息

psql 终端可以用\du 或\du+ 查看,也可以查看系统表 select * from pg_roles;
postgres=> \du
                             List of roles
 Role name |                   Attributes                   | Member of
-----------+------------------------------------------------+-----------
 david     | Cannot login                                   | {}
 postgres  | Superuser, Create role, Create DB, Replication | {}
 sandy     |                   &nb

分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • PostgreSQL 角色与用户管理介绍

相关文章

  • 2017-05-11在windows下手动初始化PostgreSQL数据库教程
  • 2017-05-11PostgreSQL教程(二十):PL/pgSQL过程语言
  • 2017-05-11CentOS中运行PostgreSQL需要修改的内核参数及配置脚本分享
  • 2017-05-11PostgreSQL教程(六):函数和操作符详解(2)
  • 2017-09-08浅谈 PostgreSQL 的 timestamp 类型
  • 2017-05-11修改一行代码提升 Postgres 性能 100 倍
  • 2017-05-11windows PostgreSQL 9.1 安装详细步骤
  • 2017-05-11PostgreSQL 查看数据库,索引,表,表空间大小的示例代码
  • 2017-05-11CentOS 7下安装PostgreSQL 9.6的教程分享
  • 2017-05-11PostgreSQL教程(十三):数据库管理详解

文章分类

  • MsSql
  • Mysql
  • oracle
  • MariaDB
  • DB2
  • SQLite
  • PostgreSQL
  • MongoDB
  • Redis
  • Access
  • 数据库其它
  • sybase
  • HBase

最近更新的内容

    • PostgreSQL教程(八):索引详解
    • phpPgAdmin 配置文件参数说明中文版
    • Windows下Postgresql数据库的下载与配置方法
    • 用一整天的时间安装postgreSQL NTFS权限
    • PostgreSQL教程(一):数据表详解
    • PostgreSQL教程(二十):PL/pgSQL过程语言
    • Postgresql ALTER语句常用操作小结
    • CentOS 7下安装PostgreSQL 9.6的教程分享
    • 解决PostgreSQL服务启动后占用100% CPU卡死的问题
    • PostgreSQL中关闭死锁进程的方法

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有