ok
Direktori : /opt/alt/postgresql11/usr/share/doc/alt-postgresql11-9.2.24/html/ |
Current File : //opt/alt/postgresql11/usr/share/doc/alt-postgresql11-9.2.24/html/sql-createrole.html |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >CREATE ROLE</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK REV="MADE" HREF="mailto:pgsql-docs@postgresql.org"><LINK REL="HOME" TITLE="PostgreSQL 9.2.24 Documentation" HREF="index.html"><LINK REL="UP" TITLE="SQL Commands" HREF="sql-commands.html"><LINK REL="PREVIOUS" TITLE="CREATE OPERATOR FAMILY" HREF="sql-createopfamily.html"><LINK REL="NEXT" TITLE="CREATE RULE" HREF="sql-createrule.html"><LINK REL="STYLESHEET" TYPE="text/css" HREF="stylesheet.css"><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-8859-1"><META NAME="creation" CONTENT="2017-11-06T22:43:11"></HEAD ><BODY CLASS="REFENTRY" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="5" ALIGN="center" VALIGN="bottom" ><A HREF="index.html" >PostgreSQL 9.2.24 Documentation</A ></TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="top" ><A TITLE="CREATE OPERATOR FAMILY" HREF="sql-createopfamily.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="10%" ALIGN="left" VALIGN="top" ><A HREF="sql-commands.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="60%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="20%" ALIGN="right" VALIGN="top" ><A TITLE="CREATE RULE" HREF="sql-createrule.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="SQL-CREATEROLE" ></A >CREATE ROLE</H1 ><DIV CLASS="REFNAMEDIV" ><A NAME="AEN69708" ></A ><H2 >Name</H2 >CREATE ROLE -- define a new database role</DIV ><DIV CLASS="REFSYNOPSISDIV" ><A NAME="AEN69713" ></A ><H2 >Synopsis</H2 ><PRE CLASS="SYNOPSIS" >CREATE ROLE <TT CLASS="REPLACEABLE" ><I >name</I ></TT > [ [ WITH ] <TT CLASS="REPLACEABLE" ><I >option</I ></TT > [ ... ] ] <SPAN CLASS="phrase" ><SPAN CLASS="PHRASE" >where <TT CLASS="REPLACEABLE" ><I >option</I ></TT > can be:</SPAN ></SPAN > SUPERUSER | NOSUPERUSER | CREATEDB | NOCREATEDB | CREATEROLE | NOCREATEROLE | CREATEUSER | NOCREATEUSER | INHERIT | NOINHERIT | LOGIN | NOLOGIN | REPLICATION | NOREPLICATION | CONNECTION LIMIT <TT CLASS="REPLACEABLE" ><I >connlimit</I ></TT > | [ ENCRYPTED | UNENCRYPTED ] PASSWORD '<TT CLASS="REPLACEABLE" ><I >password</I ></TT >' | VALID UNTIL '<TT CLASS="REPLACEABLE" ><I >timestamp</I ></TT >' | IN ROLE <TT CLASS="REPLACEABLE" ><I >role_name</I ></TT > [, ...] | IN GROUP <TT CLASS="REPLACEABLE" ><I >role_name</I ></TT > [, ...] | ROLE <TT CLASS="REPLACEABLE" ><I >role_name</I ></TT > [, ...] | ADMIN <TT CLASS="REPLACEABLE" ><I >role_name</I ></TT > [, ...] | USER <TT CLASS="REPLACEABLE" ><I >role_name</I ></TT > [, ...] | SYSID <TT CLASS="REPLACEABLE" ><I >uid</I ></TT ></PRE ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN69728" ></A ><H2 >Description</H2 ><P > <TT CLASS="COMMAND" >CREATE ROLE</TT > adds a new role to a <SPAN CLASS="PRODUCTNAME" >PostgreSQL</SPAN > database cluster. A role is an entity that can own database objects and have database privileges; a role can be considered a <SPAN CLASS="QUOTE" >"user"</SPAN >, a <SPAN CLASS="QUOTE" >"group"</SPAN >, or both depending on how it is used. Refer to <A HREF="user-manag.html" >Chapter 20</A > and <A HREF="client-authentication.html" >Chapter 19</A > for information about managing users and authentication. You must have <TT CLASS="LITERAL" >CREATEROLE</TT > privilege or be a database superuser to use this command. </P ><P > Note that roles are defined at the database cluster level, and so are valid in all databases in the cluster. </P ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN69739" ></A ><H2 >Parameters</H2 ><P ></P ><DIV CLASS="VARIABLELIST" ><DL ><DT ><TT CLASS="REPLACEABLE" ><I >name</I ></TT ></DT ><DD ><P > The name of the new role. </P ></DD ><DT ><TT CLASS="LITERAL" >SUPERUSER</TT ><BR><TT CLASS="LITERAL" >NOSUPERUSER</TT ></DT ><DD ><P > These clauses determine whether the new role is a <SPAN CLASS="QUOTE" >"superuser"</SPAN >, 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, <TT CLASS="LITERAL" >NOSUPERUSER</TT > is the default. </P ></DD ><DT ><TT CLASS="LITERAL" >CREATEDB</TT ><BR><TT CLASS="LITERAL" >NOCREATEDB</TT ></DT ><DD ><P > These clauses define a role's ability to create databases. If <TT CLASS="LITERAL" >CREATEDB</TT > is specified, the role being defined will be allowed to create new databases. Specifying <TT CLASS="LITERAL" >NOCREATEDB</TT > will deny a role the ability to create databases. If not specified, <TT CLASS="LITERAL" >NOCREATEDB</TT > is the default. </P ></DD ><DT ><TT CLASS="LITERAL" >CREATEROLE</TT ><BR><TT CLASS="LITERAL" >NOCREATEROLE</TT ></DT ><DD ><P > These clauses determine whether a role will be permitted to create new roles (that is, execute <TT CLASS="COMMAND" >CREATE ROLE</TT >). A role with <TT CLASS="LITERAL" >CREATEROLE</TT > privilege can also alter and drop other roles. If not specified, <TT CLASS="LITERAL" >NOCREATEROLE</TT > is the default. </P ></DD ><DT ><TT CLASS="LITERAL" >CREATEUSER</TT ><BR><TT CLASS="LITERAL" >NOCREATEUSER</TT ></DT ><DD ><P > These clauses are an obsolete, but still accepted, spelling of <TT CLASS="LITERAL" >SUPERUSER</TT > and <TT CLASS="LITERAL" >NOSUPERUSER</TT >. Note that they are <SPAN CLASS="emphasis" ><I CLASS="EMPHASIS" >not</I ></SPAN > equivalent to <TT CLASS="LITERAL" >CREATEROLE</TT > as one might naively expect! </P ></DD ><DT ><TT CLASS="LITERAL" >INHERIT</TT ><BR><TT CLASS="LITERAL" >NOINHERIT</TT ></DT ><DD ><P > These clauses determine whether a role <SPAN CLASS="QUOTE" >"inherits"</SPAN > the privileges of roles it is a member of. A role with the <TT CLASS="LITERAL" >INHERIT</TT > attribute can automatically use whatever database privileges have been granted to all roles it is directly or indirectly a member of. Without <TT CLASS="LITERAL" >INHERIT</TT >, membership in another role only grants the ability to <TT CLASS="COMMAND" >SET ROLE</TT > to that other role; the privileges of the other role are only available after having done so. If not specified, <TT CLASS="LITERAL" >INHERIT</TT > is the default. </P ></DD ><DT ><TT CLASS="LITERAL" >LOGIN</TT ><BR><TT CLASS="LITERAL" >NOLOGIN</TT ></DT ><DD ><P > 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 <TT CLASS="LITERAL" >LOGIN</TT > 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, <TT CLASS="LITERAL" >NOLOGIN</TT > is the default, except when <TT CLASS="COMMAND" >CREATE ROLE</TT > is invoked through its alternative spelling <A HREF="sql-createuser.html" >CREATE USER</A >. </P ></DD ><DT ><TT CLASS="LITERAL" >REPLICATION</TT ><BR><TT CLASS="LITERAL" >NOREPLICATION</TT ></DT ><DD ><P > These clauses determine whether a role is allowed to initiate streaming replication or put the system in and out of backup mode. A role having the <TT CLASS="LITERAL" >REPLICATION</TT > attribute is a very highly privileged role, and should only be used on roles actually used for replication. If not specified, <TT CLASS="LITERAL" >NOREPLICATION</TT > is the default. </P ></DD ><DT ><TT CLASS="LITERAL" >CONNECTION LIMIT</TT > <TT CLASS="REPLACEABLE" ><I >connlimit</I ></TT ></DT ><DD ><P > If role can log in, this specifies how many concurrent connections the role can make. -1 (the default) means no limit. </P ></DD ><DT ><TT CLASS="LITERAL" >PASSWORD</TT > <TT CLASS="REPLACEABLE" ><I >password</I ></TT ></DT ><DD ><P > Sets the role's password. (A password is only of use for roles having the <TT CLASS="LITERAL" >LOGIN</TT > 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 <TT CLASS="LITERAL" >PASSWORD NULL</TT >. </P ></DD ><DT ><TT CLASS="LITERAL" >ENCRYPTED</TT ><BR><TT CLASS="LITERAL" >UNENCRYPTED</TT ></DT ><DD ><P > These key words control whether the password is stored encrypted in the system catalogs. (If neither is specified, the default behavior is determined by the configuration parameter <A HREF="runtime-config-connection.html#GUC-PASSWORD-ENCRYPTION" >password_encryption</A >.) If the presented password string is already in MD5-encrypted format, then it is stored encrypted as-is, regardless of whether <TT CLASS="LITERAL" >ENCRYPTED</TT > or <TT CLASS="LITERAL" >UNENCRYPTED</TT > is specified (since the system cannot decrypt the specified encrypted password string). This allows reloading of encrypted passwords during dump/restore. </P ></DD ><DT ><TT CLASS="LITERAL" >VALID UNTIL</TT > '<TT CLASS="REPLACEABLE" ><I >timestamp</I ></TT >'</DT ><DD ><P > The <TT CLASS="LITERAL" >VALID UNTIL</TT > 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. </P ></DD ><DT ><TT CLASS="LITERAL" >IN ROLE</TT > <TT CLASS="REPLACEABLE" ><I >role_name</I ></TT ></DT ><DD ><P > The <TT CLASS="LITERAL" >IN ROLE</TT > clause lists one or more existing roles to which the new role will be immediately added as a new member. (Note that there is no option to add the new role as an administrator; use a separate <TT CLASS="COMMAND" >GRANT</TT > command to do that.) </P ></DD ><DT ><TT CLASS="LITERAL" >IN GROUP</TT > <TT CLASS="REPLACEABLE" ><I >role_name</I ></TT ></DT ><DD ><P ><TT CLASS="LITERAL" >IN GROUP</TT > is an obsolete spelling of <TT CLASS="LITERAL" >IN ROLE</TT >. </P ></DD ><DT ><TT CLASS="LITERAL" >ROLE</TT > <TT CLASS="REPLACEABLE" ><I >role_name</I ></TT ></DT ><DD ><P > The <TT CLASS="LITERAL" >ROLE</TT > clause lists one or more existing roles which are automatically added as members of the new role. (This in effect makes the new role a <SPAN CLASS="QUOTE" >"group"</SPAN >.) </P ></DD ><DT ><TT CLASS="LITERAL" >ADMIN</TT > <TT CLASS="REPLACEABLE" ><I >role_name</I ></TT ></DT ><DD ><P > The <TT CLASS="LITERAL" >ADMIN</TT > clause is like <TT CLASS="LITERAL" >ROLE</TT >, but the named roles are added to the new role <TT CLASS="LITERAL" >WITH ADMIN OPTION</TT >, giving them the right to grant membership in this role to others. </P ></DD ><DT ><TT CLASS="LITERAL" >USER</TT > <TT CLASS="REPLACEABLE" ><I >role_name</I ></TT ></DT ><DD ><P > The <TT CLASS="LITERAL" >USER</TT > clause is an obsolete spelling of the <TT CLASS="LITERAL" >ROLE</TT > clause. </P ></DD ><DT ><TT CLASS="LITERAL" >SYSID</TT > <TT CLASS="REPLACEABLE" ><I >uid</I ></TT ></DT ><DD ><P > The <TT CLASS="LITERAL" >SYSID</TT > clause is ignored, but is accepted for backwards compatibility. </P ></DD ></DL ></DIV ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN69898" ></A ><H2 >Notes</H2 ><P > Use <A HREF="sql-alterrole.html" >ALTER ROLE</A > to change the attributes of a role, and <A HREF="sql-droprole.html" >DROP ROLE</A > to remove a role. All the attributes specified by <TT CLASS="COMMAND" >CREATE ROLE</TT > can be modified by later <TT CLASS="COMMAND" >ALTER ROLE</TT > commands. </P ><P > The preferred way to add and remove members of roles that are being used as groups is to use <A HREF="sql-grant.html" >GRANT</A > and <A HREF="sql-revoke.html" >REVOKE</A >. </P ><P > The <TT CLASS="LITERAL" >VALID UNTIL</TT > clause defines an expiration time for a password only, not for the role <I CLASS="FOREIGNPHRASE" >per se</I >. In particular, the expiration time is not enforced when logging in using a non-password-based authentication method. </P ><P > The <TT CLASS="LITERAL" >INHERIT</TT > attribute governs inheritance of grantable privileges (that is, access privileges for database objects and role memberships). It does not apply to the special role attributes set by <TT CLASS="COMMAND" >CREATE ROLE</TT > and <TT CLASS="COMMAND" >ALTER ROLE</TT >. For example, being a member of a role with <TT CLASS="LITERAL" >CREATEDB</TT > privilege does not immediately grant the ability to create databases, even if <TT CLASS="LITERAL" >INHERIT</TT > is set; it would be necessary to become that role via <A HREF="sql-set-role.html" >SET ROLE</A > before creating a database. </P ><P > The <TT CLASS="LITERAL" >INHERIT</TT > attribute is the default for reasons of backwards compatibility: in prior releases of <SPAN CLASS="PRODUCTNAME" >PostgreSQL</SPAN >, users always had access to all privileges of groups they were members of. However, <TT CLASS="LITERAL" >NOINHERIT</TT > provides a closer match to the semantics specified in the SQL standard. </P ><P > Be careful with the <TT CLASS="LITERAL" >CREATEROLE</TT > privilege. There is no concept of inheritance for the privileges of a <TT CLASS="LITERAL" >CREATEROLE</TT >-role. That means that even if a role does not have a certain privilege but is allowed to create other roles, it can easily create another role with different privileges than its own (except for creating roles with superuser privileges). For example, if the role <SPAN CLASS="QUOTE" >"user"</SPAN > has the <TT CLASS="LITERAL" >CREATEROLE</TT > privilege but not the <TT CLASS="LITERAL" >CREATEDB</TT > privilege, nonetheless it can create a new role with the <TT CLASS="LITERAL" >CREATEDB</TT > privilege. Therefore, regard roles that have the <TT CLASS="LITERAL" >CREATEROLE</TT > privilege as almost-superuser-roles. </P ><P > <SPAN CLASS="PRODUCTNAME" >PostgreSQL</SPAN > includes a program <A HREF="app-createuser.html" ><SPAN CLASS="APPLICATION" >createuser</SPAN ></A > that has the same functionality as <TT CLASS="COMMAND" >CREATE ROLE</TT > (in fact, it calls this command) but can be run from the command shell. </P ><P > The <TT CLASS="LITERAL" >CONNECTION LIMIT</TT > option is only enforced approximately; if two new sessions start at about the same time when just one connection <SPAN CLASS="QUOTE" >"slot"</SPAN > remains for the role, it is possible that both will fail. Also, the limit is never enforced for superusers. </P ><P > 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 <A HREF="app-createuser.html" ><SPAN CLASS="APPLICATION" >createuser</SPAN ></A >, however, transmits the password encrypted. Also, <A HREF="app-psql.html" ><SPAN CLASS="APPLICATION" >psql</SPAN ></A > contains a command <TT CLASS="COMMAND" >\password</TT > that can be used to safely change the password later. </P ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN69941" ></A ><H2 >Examples</H2 ><P > Create a role that can log in, but don't give it a password: </P><PRE CLASS="PROGRAMLISTING" >CREATE ROLE jonathan LOGIN;</PRE ><P> </P ><P > Create a role with a password: </P><PRE CLASS="PROGRAMLISTING" >CREATE USER davide WITH PASSWORD 'jw8s0F4';</PRE ><P> (<TT CLASS="COMMAND" >CREATE USER</TT > is the same as <TT CLASS="COMMAND" >CREATE ROLE</TT > except that it implies <TT CLASS="LITERAL" >LOGIN</TT >.) </P ><P > 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. </P><PRE CLASS="PROGRAMLISTING" >CREATE ROLE miriam WITH LOGIN PASSWORD 'jw8s0F4' VALID UNTIL '2005-01-01';</PRE ><P> </P ><P > Create a role that can create databases and manage roles: </P><PRE CLASS="PROGRAMLISTING" >CREATE ROLE admin WITH CREATEDB CREATEROLE;</PRE ><P></P ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN69954" ></A ><H2 >Compatibility</H2 ><P > The <TT CLASS="COMMAND" >CREATE ROLE</TT > statement is in the SQL standard, but the standard only requires the syntax </P><PRE CLASS="SYNOPSIS" >CREATE ROLE <TT CLASS="REPLACEABLE" ><I >name</I ></TT > [ WITH ADMIN <TT CLASS="REPLACEABLE" ><I >role_name</I ></TT > ]</PRE ><P> Multiple initial administrators, and all the other options of <TT CLASS="COMMAND" >CREATE ROLE</TT >, are <SPAN CLASS="PRODUCTNAME" >PostgreSQL</SPAN > extensions. </P ><P > 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 <SPAN CLASS="PRODUCTNAME" >PostgreSQL</SPAN > 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. </P ><P > The behavior specified by the SQL standard is most closely approximated by giving users the <TT CLASS="LITERAL" >NOINHERIT</TT > attribute, while roles are given the <TT CLASS="LITERAL" >INHERIT</TT > attribute. </P ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN69968" ></A ><H2 >See Also</H2 ><A HREF="sql-set-role.html" >SET ROLE</A >, <A HREF="sql-alterrole.html" >ALTER ROLE</A >, <A HREF="sql-droprole.html" >DROP ROLE</A >, <A HREF="sql-grant.html" >GRANT</A >, <A HREF="sql-revoke.html" >REVOKE</A >, <A HREF="app-createuser.html" ><SPAN CLASS="APPLICATION" >createuser</SPAN ></A ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="sql-createopfamily.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="sql-createrule.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >CREATE OPERATOR FAMILY</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="sql-commands.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >CREATE RULE</TD ></TR ></TABLE ></DIV ></BODY ></HTML >