Postgresql 中文操作指南
Synopsis
CREATE ROLE 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' | PASSWORD NULL
| VALID UNTIL 'timestamp'
| IN ROLE role_name [, ...]
| IN GROUP role_name [, ...]
| ROLE role_name [, ...]
| ADMIN role_name [, ...]
| USER role_name [, ...]
| SYSID uid
Description
CREATE ROLE 向 PostgreSQL 数据库集群添加了一个新角色。角色是能够拥有数据库对象和具有数据库权限的实体;角色可以被视为“用户”、“组”,或两者,具体取决于其使用方式。有关管理用户和认证的信息,请参阅 Chapter 22 和 Chapter 21 。您必须拥有 CREATEROLE 权限或成为数据库超级用户才能使用此命令。
CREATE ROLE adds a new role to a PostgreSQL database cluster. A role is an entity that can own database objects and have database privileges; a role can be considered a “user”, a “group”, or both depending on how it is used. Refer to Chapter 22 and Chapter 21 for information about managing users and authentication. You must have CREATEROLE privilege or be a database superuser to use this command.
请注意,角色是在数据库集群级别定义的,因此在集群中的所有数据库中都有效。
Note that roles are defined at the database cluster level, and so are valid in all databases in the cluster.
在创建角色期间,可以立即将新创建的角色指定为现有角色的成员,还可以指定现有角色为新创建角色的成员。有资格获得哪些初始角色成员选项的规则在下面的 IN ROLE 、 ROLE 和 ADMIN 条款中进行了说明。 GRANT 命令在创建成员资格期间具有细粒度的选项控制,并在创建新角色后修改这些选项的能力。
During role creation it is possible to immediately assign the newly created role to be a member of an existing role, and also assign existing roles to be members of the newly created role. The rules for which initial role membership options are enabled described below in the IN ROLE, ROLE, and ADMIN clauses. The GRANT command has fine-grained option control during membership creation, and the ability to modify these options after the new role is created.
Parameters
-
name
-
The name of the new role.
-
-
SUPERUSER__NOSUPERUSER
-
These clauses determine whether the new role is a “superuser”, who can override all access restrictions within the database. Superuser status is dangerous and should be used only when really needed. You must yourself be a superuser to create a new superuser. If not specified, NOSUPERUSER is the default.
-
-
CREATEDB__NOCREATEDB
-
These clauses define a role’s ability to create databases. If CREATEDB is specified, the role being defined will be allowed to create new databases. Specifying NOCREATEDB will deny a role the ability to create databases. If not specified, NOCREATEDB is the default. Only superuser roles or roles with CREATEDB can specify CREATEDB.
-
-
CREATEROLE__NOCREATEROLE
-
These clauses determine whether a role will be permitted to create, alter, drop, comment on, and change the security label for other roles. See role creation for more details about what capabilities are conferred by this privilege. If not specified, NOCREATEROLE is the default.
-
-
INHERIT__NOINHERIT
-
This affects the membership inheritance status when this role is added as a member of another role, both in this and future commands. Specifically, it controls the inheritance status of memberships added with this command using the IN ROLE clause, and in later commands using the ROLE clause. It is also used as the default inheritance status when adding this role as a member using the GRANT command. If not specified, INHERIT is the default.
-
In PostgreSQL versions before 16, inheritance was a role-level attribute that controlled all runtime membership checks for that role.
-
-
LOGIN__NOLOGIN
-
These clauses determine whether a role is allowed to log in; that is, whether the role can be given as the initial session authorization name during client connection. A role having the LOGIN attribute can be thought of as a user. Roles without this attribute are useful for managing database privileges, but are not users in the usual sense of the word. If not specified, NOLOGIN is the default, except when CREATE ROLE is invoked through its alternative spelling CREATE USER.
-
-
REPLICATION__NOREPLICATION
-
These clauses determine whether a role is a replication role. A role must have this attribute (or be a superuser) in order to be able to connect to the server in replication mode (physical or logical replication) and in order to be able to create or drop replication slots. A role having the REPLICATION attribute is a very highly privileged role, and should only be used on roles actually used for replication. If not specified, NOREPLICATION is the default. Only superuser roles or roles with REPLICATION can specify REPLICATION.
-
-
BYPASSRLS__NOBYPASSRLS
-
These clauses determine whether a role bypasses every row-level security (RLS) policy. NOBYPASSRLS is the default. Only superuser roles or roles with BYPASSRLS can specify BYPASSRLS.
-
Note that pg_dump will set row_security to OFF by default, to ensure all contents of a table are dumped out. If the user running pg_dump does not have appropriate permissions, an error will be returned. However, superusers and the owner of the table being dumped always bypass RLS.
-
-
CONNECTION LIMIT connlimit
-
If role can log in, this specifies how many concurrent connections the role can make. -1 (the default) means no limit. Note that only normal connections are counted towards this limit. Neither prepared transactions nor background worker connections are counted towards this limit.
-
-
[ ENCRYPTED ] PASSWORD 'password'PASSWORD NULL
-
Sets the role’s password. (A password is only of use for roles having the LOGIN attribute, but you can nonetheless define one for roles without it.) If you do not plan to use password authentication you can omit this option. If no password is specified, the password will be set to null and password authentication will always fail for that user. A null password can optionally be written explicitly as PASSWORD NULL.
-
The password is always stored encrypted in the system catalogs. The ENCRYPTED keyword has no effect, but is accepted for backwards compatibility. The method of encryption is determined by the configuration parameter password_encryption. If the presented password string is already in MD5-encrypted or SCRAM-encrypted format, then it is stored as-is regardless of password_encryption (since the system cannot decrypt the specified encrypted password string, to encrypt it in a different format). This allows reloading of encrypted passwords during dump/restore.
-
-
VALID UNTIL 'timestamp'
-
The VALID UNTIL clause sets a date and time after which the role’s password is no longer valid. If this clause is omitted the password will be valid for all time.
-
-
IN ROLE role_name
-
The IN ROLE clause causes the new role to be automatically added as a member of the specified existing roles. The new membership will have the SET option enabled and the ADMIN option disabled. The INHERIT option will be enabled unless the NOINHERIT option is specified.
-
-
IN GROUP role_name
-
IN GROUP is an obsolete spelling of IN ROLE.
-
-
ROLE role_name
-
The ROLE clause causes one or more specified existing roles to be automatically added as members, with the SET option enabled. This in effect makes the new role a “group”. Roles named in this clause with the role-level INHERIT attribute will have the INHERIT option enabled in the new membership. New memberships will have the ADMIN option disabled.
-
-
ADMIN role_name
-
The ADMIN clause has the same effect as ROLE, but the named roles are added as members of the new role with ADMIN enabled, giving them the right to grant membership in the new role to others.
-
-
USER role_name
-
The USER clause is an obsolete spelling of the ROLE clause.
-
-
SYSID uid
-
The SYSID clause is ignored, but is accepted for backwards compatibility.
-
Note
指定空字符串还将把密码设为空,但这种情况在 PostgreSQL 版本 10 之前不是这样。在早期版本中,可以使用空字符串,也可以不使用,具体取决于身份验证方法和具体版本,并且 libpq 在任何情况下都拒绝使用它。为避免歧义,应避免指定空字符串。
Specifying an empty string will also set the password to null, but that was not the case before PostgreSQL version 10. In earlier versions, an empty string could be used, or not, depending on the authentication method and the exact version, and libpq would refuse to use it in any case. To avoid the ambiguity, specifying an empty string should be avoided.
Notes
使用 ALTER ROLE 更改角色的属性,使用 DROP ROLE 删除角色。 CREATE ROLE 指定的所有属性都可以由 ALTER ROLE 命令在后面进行修改。
Use ALTER ROLE to change the attributes of a role, and DROP ROLE to remove a role. All the attributes specified by CREATE ROLE can be modified by later ALTER ROLE commands.
The preferred way to add and remove members of roles that are being used as groups is to use GRANT and REVOKE.
VALID UNTIL 子句只为密码定义一个到期时间,而不是为角色本身定义。尤其是,在使用基于非密码身份验证方法登录时不实施到期时间。
The VALID UNTIL clause defines an expiration time for a password only, not for the role per se. In particular, the expiration time is not enforced when logging in using a non-password-based authentication method.
此处定义的角色属性是不可继承的,即成为具有 CREATEDB 属性的角色的成员不会允许成员创建新数据库,即使成员资格授予即具有 INHERIT 选项。当然,如果成员资格授予具有 SET 选项,则成员角色将能够 SET ROLE 至 createdb 角色,然后创建新数据库。
The role attributes defined here are non-inheritable, i.e., being a member of a role with, e.g., CREATEDB will not allow the member to create new databases even if the membership grant has the INHERIT option. Of course, if the membership grant has the SET option the member role would be able to SET ROLE to the createdb role and then create a new database.
IN ROLE 、 ROLE 和 ADMIN 子句创建的成员资格授予将执行此命令的角色指定为授予者。
The membership grants created by the IN ROLE, ROLE, and ADMIN clauses have the role executing this command as the grantor.
出于向后兼容性的原因, INHERIT 特性被设为默认值:在 PostgreSQL 的先前的版本中,用户始终可以访问其作为成员加入的所有组的特权。然而, NOINHERIT 提供了更符合 SQL 标准中指定语义的匹配。
The INHERIT attribute is the default for reasons of backwards compatibility: in prior releases of PostgreSQL, users always had access to all privileges of groups they were members of. However, NOINHERIT provides a closer match to the semantics specified in the SQL standard.
PostgreSQL 包含一个程序 createuser ,其与 CREATE ROLE 具有相同的功能(事实上它调用此命令),但可以从命令 shell 中运行。
PostgreSQL includes a program createuser that has the same functionality as CREATE ROLE (in fact, it calls this command) but can be run from the command shell.
CONNECTION LIMIT 选项仅大致强制实施;如果两个新会话在大约同时启动,而角色只剩下一个连接“时隙”,那么两者都可能失败。此外,对超级用户从不实施限制。
The CONNECTION LIMIT option is only enforced approximately; if two new sessions start at about the same time when just one connection “slot” remains for the role, it is possible that both will fail. Also, the limit is never enforced for superusers.
使用此命令指定未加密密码时必须小心。密码将以明文方式传输到服务器,并且它也可能记录在客户端的命令历史记录或服务器日志中。然而, createuser 命令以加密方式传输密码。此外, psql 包含一个命令 \password ,该命令可用于在稍后安全地更改密码。
Caution must be exercised when specifying an unencrypted password with this command. The password will be transmitted to the server in cleartext, and it might also be logged in the client’s command history or the server log. The command createuser, however, transmits the password encrypted. Also, psql contains a command \password that can be used to safely change the password later.
Examples
创建一个可以登录的角色,但不要给它一个密码:
Create a role that can log in, but don’t give it a password:
CREATE ROLE jonathan LOGIN;
创建一个带有密码的角色:
Create a role with a password:
CREATE USER davide WITH PASSWORD 'jw8s0F4';
( CREATE USER 与 CREATE ROLE 相同,但它暗示 LOGIN 。)
(CREATE USER is the same as CREATE ROLE except that it implies LOGIN.)
用密码创建一个角色,该密码在2004年底之前有效。在2005年的第一秒过去的之后,该密码就将无效。
Create a role with a password that is valid until the end of 2004. After one second has ticked in 2005, the password is no longer valid.
CREATE ROLE miriam WITH LOGIN PASSWORD 'jw8s0F4' VALID UNTIL '2005-01-01';
创建一个能够创建数据库和管理角色的角色:
Create a role that can create databases and manage roles:
CREATE ROLE admin WITH CREATEDB CREATEROLE;
Compatibility
CREATE ROLE 语句在SQL标准中,但标准只要求语法。
The CREATE ROLE statement is in the SQL standard, but the standard only requires the syntax
CREATE ROLE name [ WITH ADMIN role_name ]
多个初始管理员和 CREATE ROLE 的其他选项是PostgreSQL扩展。
Multiple initial administrators, and all the other options of CREATE ROLE, are PostgreSQL extensions.
SQL标准定义了用户和角色的概念,但它把它们视为不同的概念并将定义用户的全部命令留给各数据库实现者决定。在PostgreSQL中,我们选择将用户和角色统一为单一实体。因此,角色有着比标准中更多的可选属性。
The SQL standard defines the concepts of users and roles, but it regards them as distinct concepts and leaves all commands defining users to be specified by each database implementation. In PostgreSQL we have chosen to unify users and roles into a single kind of entity. Roles therefore have many more optional attributes than they do in the standard.
SQL标准指定的的行为最接近的是将SQL标准用户作为带有 NOINHERIT 选项的PostgreSQL角色和将SQL标准角色作为带有 INHERIT 选项的PostgreSQL角色来创建。
The behavior specified by the SQL standard is most closely approximated creating SQL-standard users as PostgreSQL roles with the NOINHERIT option, and SQL-standard roles as PostgreSQL roles with the INHERIT option.