バージョンごとのドキュメント一覧

CREATE ROLE

CREATE ROLE <refpurpose>define a new database role</refpurpose> — 新しいデータベースロールを定義する

概要

CREATE ROLE name [ [ WITH ] option [ ... ] ]


<phrase>where <replaceable class="parameter">option</replaceable> can be:</phrase>

ここでoptionは以下の通りです。

      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
CAUTION: remember to keep create_user.sgml and create_group.sgml in sync when changing the above synopsis!

説明

<title>Description</title>

<command>CREATE ROLE</command> adds a new role to a <productname>PostgreSQL</productname> database cluster. A role is an entity that can own database objects and have database privileges; a role can be considered a <quote>user</quote>, a <quote>group</quote>, or both depending on how it is used. Refer to <xref linkend="user-manag"/> and <xref linkend="client-authentication"/> for information about managing users and authentication. You must have <literal>CREATEROLE</literal> privilege or be a database superuser to use this command. CREATE ROLEは、PostgreSQLデータベースクラスタに新しいロールを加えます。 ロールとは、データベースオブジェクトを所有することができ、データベース権限を持つことができる実体のことです。 ロールは、使用状況に応じてユーザグループ、もしくは、その両方であるとみなすことができます。 ユーザの管理と認証に関する情報については、第22章第21章を参照してください。 このコマンドを使用するには、CREATEROLE権限を持つか、データベースのスーパーユーザでなければなりません。

Note that roles are defined at the database cluster level, and so are valid in all databases in the cluster. ロールはデータベースクラスタのレベルで定義されるため、クラスタ内のすべてのデータベースで有効となることに注意してください。

パラメータ

<title>Parameters</title>
name

The name of the new role. 新しいロールの名前です。

SUPERUSER
NOSUPERUSER

These clauses determine whether the new role is a <quote>superuser</quote>, 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, <literal>NOSUPERUSER</literal> is the default. これらの句によって、新しいロールがスーパーユーザとなるかどうかが決まります。 スーパーユーザはデータベース内のすべてのアクセス制限より優先します。 スーパーユーザという状態は危険ですので、本当に必要な場合にのみ使用しなければなりません。 新しくスーパーユーザを作成するには、スーパーユーザでなければなりません。 指定されなかった場合のデフォルトはNOSUPERUSERです。

CREATEDB
NOCREATEDB

These clauses define a role's ability to create databases. If <literal>CREATEDB</literal> is specified, the role being defined will be allowed to create new databases. Specifying <literal>NOCREATEDB</literal> will deny a role the ability to create databases. If not specified, <literal>NOCREATEDB</literal> is the default. Only superuser roles or roles with <literal>CREATEDB</literal> can specify <literal>CREATEDB</literal>. これらの句はロールのデータベースの作成に関する権限を定義します。 CREATEDBが指定された場合、定義されたロールは新しくデータベースを作成することができます。 NOCREATEDBを使用した場合、そのロールにはデータベースを作成する権限が与えられません。 指定されなかった場合のデフォルトはNOCREATEDBです。 スーパーユーザロールもしくはCREATEDBを持つロールだけが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 <xref linkend="role-creation"/> for more details about what capabilities are conferred by this privilege. If not specified, <literal>NOCREATEROLE</literal> is the default. これらの句は、他のロールの作成、変更、削除、コメント付与、およびセキュリティラベルの変更をロールに許可するかどうかを決定します。 この権限によって付与される機能の詳細は、ロールの作成を参照してください。 指定されなかった場合のデフォルトはNOCREATEROLEです。

INHERIT
NOINHERIT

When the <literal>GRANT</literal> statement is used to confer membership in one role to another role, the <literal>GRANT</literal> may use the <literal>WITH INHERIT</literal> clause to specify whether the privileges of the granted role should be <quote>inherited</quote> by the new member. If the <literal>GRANT</literal> statement does not specify either inheritance behavior, the new <literal>GRANT</literal> will be created <literal>WITH INHERIT TRUE</literal> if the member role is set to <literal>INHERIT</literal> and to <literal>WITH INHERIT FALSE</literal> if it is set to <literal>NOINHERIT</literal>. GRANT文を使用して、あるロールのメンバ資格を別のロールに付与する場合、GRANTWITH INHERIT句を使用して、付与されたロールの権限を新しいメンバで継承(inherit)するかどうかを指定できます。 GRANT文で継承の動作が指定されていない場合、新しいGRANTは、メンバロールがINHERITに設定されている場合にはWITH INHERIT TRUEで、NOINHERITに設定されている場合にはWITH INHERIT FALSEで作成されます。

In <productname>PostgreSQL</productname> versions before 16, the <literal>GRANT</literal> statement did not support <literal>WITH INHERIT</literal>. Therefore, changing this role-level property would also change the behavior of already-existing grants. This is no longer the case. 16より前のPostgreSQLのバージョンでは、GRANT文はWITH INHERITをサポートしませんでした。 したがって、このロールレベル属性を変更すると、既存の権限の付与の動作も変更されました。 これはもはや当てはまりません。

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 <literal>LOGIN</literal> 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, <literal>NOLOGIN</literal> is the default, except when <command>CREATE ROLE</command> is invoked through its alternative spelling <link linkend="sql-createuser"><command>CREATE USER</command></link>. これらの句により、ロールがログイン可能かどうか、つまり、そのロールをクライアント接続時にセッションを認証するための名前として使用することができるかどうかが決まります。 LOGIN属性を持つロールはユーザとみなすことができます。 この属性を持たないロールは、データベース権限を管理する際に有用ですが、普通の意味ではユーザとはいえません。 指定されなかった場合のデフォルトはNOLOGINですが、CREATE ROLEがその別名である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 <literal>REPLICATION</literal> attribute is a very highly privileged role, and should only be used on roles actually used for replication. If not specified, <literal>NOREPLICATION</literal> is the default. Only superuser roles or roles with <literal>REPLICATION</literal> can specify <literal>REPLICATION</literal>. これらの句は、ロールがレプリケーションロールかどうかを決定します。 レプリケーションモード(物理または論理レプリケーション)のサーバに接続できるためには、またレプリケーションスロットを作成または削除できるためには、ロールはこの属性(またはスーパーユーザ)を持っている必要があります。 REPLICATION属性を持つロールは、非常に高度な権限を持つものです。 このため実際にレプリケーションで使用するロールでのみ使用しなければなりません。 指定されない場合のデフォルトはNOREPLICATIONです。 スーパーユーザロールまたはREPLICATIONを持つロールだけがREPLICATIONを指定できます。

BYPASSRLS
NOBYPASSRLS

These clauses determine whether a role bypasses every row-level security (RLS) policy. <literal>NOBYPASSRLS</literal> is the default. Only superuser roles or roles with <literal>BYPASSRLS</literal> can specify <literal>BYPASSRLS</literal>. これらの構文は、ロールがすべての行単位セキュリティ(RLS)ポリシーを無視するかどうかを決定します。 NOBYPASSRLSがデフォルトです。 スーパーユーザロールまたはBYPASSRLSを持つロールだけがBYPASSRLSを指定できます。

Note that pg_dump will set <literal>row_security</literal> to <literal>OFF</literal> 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. pg_dumpはテーブルのすべての内容が確実にダンプされるようにするため、row_securityをデフォルトでOFFに設定することに注意してください。 pg_dumpを実行するユーザが適切な権限を持っていなければ、エラーが返されます。 しかしながら、スーパーユーザおよびダンプされるテーブルの所有者は、常に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. ロールがログイン可能である場合、これは、ロールが確立できる最大同時接続数を指定します。 -1(デフォルト)は無制限を意味します。 通常の接続のみがこの制限の対象として数えられることに注意してください。 準備されたトランザクションやバックグラウンドワーカーの接続はこの制限の対象にはなりません。

[ ENCRYPTED ] PASSWORD 'password'
PASSWORD NULL

Sets the role's password. (A password is only of use for roles having the <literal>LOGIN</literal> 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 <literal>PASSWORD NULL</literal>. ロールのパスワードを設定します。 (パスワードはLOGIN属性を持つロールでのみ使用されますが、この属性を持たないロールにも定義することができます。) パスワード認証を行う予定がなければ、このオプションを省略することができます。 パスワードの指定がなければ、パスワードがNULLに設定され、そのアカウントでのパスワード認証は常に失敗します。 NULLというパスワードを明示的にPASSWORD NULLと記述することもできます。

注記

Specifying an empty string will also set the password to null, but that was not the case before <productname>PostgreSQL</productname> 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. 空の文字列を指定した場合もパスワードをNULLに設定しますが、PostgreSQLのバージョン10より前はそのようになっていませんでした。 以前のバージョンでは、認証方式と細かいバージョンによって空の文字列が使えたり使えなかったりし、また、いずれにせよlibpqは空の文字列を拒絶していました。 この曖昧さを避けるためには、空の文字列の指定は避けるのが良いです。

The password is always stored encrypted in the system catalogs. The <literal>ENCRYPTED</literal> keyword has no effect, but is accepted for backwards compatibility. The method of encryption is determined by the configuration parameter <xref linkend="guc-password-encryption"/>. If the presented password string is already in MD5-encrypted or SCRAM-encrypted format, then it is stored as-is regardless of <varname>password_encryption</varname> (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. パスワードは必ず暗号化されてシステムカタログに保存されます。 ENCRYPTEDキーワードには何の効果もありませんが、後方互換性のために受け付けられます。 暗号化の方法は設定パラメータpassword_encryptionによって決定されます。 指定されたパスワード文字列が既にMD5またはSCRAMの暗号化形式になっている場合は、password_encryptionと関係なく、そのまま保存されます(指定のパスワードを暗号化した文字列を復号できないので、パスワードを別の形式で暗号化することができないからです)。 これにより、ダンプ/リストア時に暗号化したパスワードを再ロードすることができます。

VALID UNTIL 'timestamp'

The <literal>VALID UNTIL</literal> 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. VALID UNTIL句は、ロールのパスワードが無効になる日時を設定します。 この句が省略された場合、パスワードは永遠に有効になります。

IN ROLE role_name

The <literal>IN ROLE</literal> clause causes the new role to be automatically added as a member of the specified existing roles. (Note that there is no option to add the new role as an administrator; use a separate <command>GRANT</command> command to do that.) IN ROLE句を指定すると、新しいロールが自動的に指定した既存のロールのメンバとして追加されます。 (新しく作成するロールを管理者として追加するオプションがないことに注意してください。このためには別途GRANTコマンドを使用してください。)

IN GROUP role_name
<para><literal>IN GROUP</literal> is an obsolete spelling of <literal>IN ROLE</literal>.

IN GROUPIN ROLEの別名で、廃止予定です。

ROLE role_name

The <literal>ROLE</literal> clause causes one or more specified existing roles to be automatically added as members of the new role. This in effect makes the new role a <quote>group</quote>. ROLE句を指定すると、指定された1つ以上の既存のロールが新しいロールのメンバとして自動的に追加されます。 これは実質的に新しく作成したロールをグループとします。

ADMIN role_name

The <literal>ADMIN</literal> clause is like <literal>ROLE</literal>, but the named roles are added to the new role <literal>WITH ADMIN OPTION</literal>, giving them the right to grant membership in this role to others. ADMIN句はROLEと似ていますが、新しく作成されるロールに指定されたロールがWITH ADMIN OPTIONとして追加される点が異なります。 つまり、新しく作成されるロールのメンバ資格を他者に与えることができる権利を、指定されたロールに与えます。

USER role_name

The <literal>USER</literal> clause is an obsolete spelling of the <literal>ROLE</literal> clause. USER句はROLE句の別名で廃止予定です。

SYSID uid

The <literal>SYSID</literal> clause is ignored, but is accepted for backwards compatibility. SYSID句は無視されますが、後方互換性を維持するために受け付けられます。

注釈

<title>Notes</title>

Use <link linkend="sql-alterrole"><command>ALTER ROLE</command></link> to change the attributes of a role, and <link linkend="sql-droprole"><command>DROP ROLE</command></link> to remove a role. All the attributes specified by <command>CREATE ROLE</command> can be modified by later <command>ALTER ROLE</command> commands. ロールの属性を変更するにはALTER ROLEを、ロールを削除するにはDROP ROLEを使用してください。 CREATE ROLEで指定したすべての属性は、後でALTER ROLEコマンドで変更可能です。

The preferred way to add and remove members of roles that are being used as groups is to use <link linkend="sql-grant"><command>GRANT</command></link> and <link linkend="sql-revoke"><command>REVOKE</command></link>. グループとして使用しているロールのメンバの追加、および、削除についての推奨方法は、GRANTREVOKEを使用することです。

The <literal>VALID UNTIL</literal> 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. VALID UNTIL句は、パスワードの有効期限を定義するだけで、ロール自体の有効期限ではありません。 特に、パスワードを使わない認証方式でログインを行う場合には、この有効期限は強制されません。

The <literal>INHERIT</literal> 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 <command>CREATE ROLE</command> and <command>ALTER ROLE</command>. For example, being a member of a role with <literal>CREATEDB</literal> privilege does not immediately grant the ability to create databases, even if <literal>INHERIT</literal> is set; it would be necessary to become that role via <link linkend="sql-set-role"><command>SET ROLE</command></link> before creating a database. INHERIT属性は、許可可能な権限(つまり、データベースオブジェクトに対するアクセス権限とロールのメンバ資格)の継承を管理します。 これは、CREATE ROLEALTER ROLEで設定される特別なロール属性には適用されません。 INHERITが設定されていたとしても、例えば、CREATEDB権限を持つロールのメンバであっても、データベース作成権限は即座に付与されません。 データベースを作成する前にSET ROLEを使用してそのロールにならなければなりません。

The <literal>INHERIT</literal> attribute is the default for reasons of backwards compatibility: in prior releases of <productname>PostgreSQL</productname>, users always had access to all privileges of groups they were members of. However, <literal>NOINHERIT</literal> provides a closer match to the semantics specified in the SQL standard. 後方互換性を維持するため、INHERIT属性はデフォルトです。 以前のリリースのPostgreSQLでは、ユーザは常にメンバとなっているすべてのグループの権限でアクセスできました。 しかし、NOINHERITの方が標準SQLの規定の意味により合ったものを提供します。

<productname>PostgreSQL</productname> includes a program <xref linkend="app-createuser"/> that has the same functionality as <command>CREATE ROLE</command> (in fact, it calls this command) but can be run from the command shell. PostgreSQLには、CREATE ROLEと同じ機能を持つプログラム(実際にこのコマンドを呼び出しています)createuserがあり、コマンドシェルから実行することができます。

The <literal>CONNECTION LIMIT</literal> option is only enforced approximately; if two new sessions start at about the same time when just one connection <quote>slot</quote> remains for the role, it is possible that both will fail. Also, the limit is never enforced for superusers. CONNECTION LIMITオプションが加える制限は厳密ではありません。 もしそのロールに1つだけ接続スロットが残っていた時に、ほぼ同時に2つのセッションが新しく始まった場合、両方とも失敗する可能性があります。 また、この制限はスーパーユーザには適用されません。

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 <xref linkend="app-createuser"/>, however, transmits the password encrypted. Also, <xref linkend="app-psql"/> contains a command <command>\password</command> that can be used to safely change the password later. このコマンドで暗号化しないパスワードを指定するときには注意しなければなりません。 パスワードはサーバに平文で送信されます。 クライアントのコマンド履歴やサーバのログにこれが残ってしまうかもしれません。 しかし、createuserコマンドはパスワードを暗号化して送信します。 また、psqlには\passwordコマンドがあり、これを使用して後でパスワードを安全に変更することができます。

<title>Examples</title>

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';

(<command>CREATE USER</command> is the same as <command>CREATE ROLE</command> except that it implies <literal>LOGIN</literal>.) CREATE USERLOGINを意味する点を除き、CREATE ROLEと同一です。

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. 2004年まで有効なパスワードを持つロールを作成します。 2005年に1秒でも入った時点でパスワードは無効になります。

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;

互換性

<title>Compatibility</title>

The <command>CREATE ROLE</command> statement is in the SQL standard, but the standard only requires the syntax CREATE ROLE文は標準SQLで規定されています。 しかしSQLでは以下の構文のみを要求しています。

CREATE ROLE name [ WITH ADMIN role_name ]

Multiple initial administrators, and all the other options of <command>CREATE ROLE</command>, are <productname>PostgreSQL</productname> extensions. 複数の初期管理者やそのほかのCREATE ROLEのオプションは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 <productname>PostgreSQL</productname> 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では、ユーザとロールという概念を定義し、それらを別の概念としてみなしています。 また、ユーザを定義するコマンドはすべて、各データベース実装で規定するものとしています。 PostgreSQLでは、ユーザとロールを単一の実体に統一することを選択しています。 したがって、ロールは標準よりも非常に多くのオプションの属性を持っています。

The behavior specified by the SQL standard is most closely approximated by giving users the <literal>NOINHERIT</literal> attribute, while roles are given the <literal>INHERIT</literal> attribute. ユーザはNOINHERIT属性を与えること、ロールはINHERIT属性を与えることで、標準SQLで規定された振舞いに最も近くなります。

関連項目

<title>See Also</title> SET ROLE, ALTER ROLE, DROP ROLE, GRANT, REVOKE, createuser, createrole_self_grant