CREATE USER

Name

CREATE USER  --  Defines a new database user account

Synopsis

CREATE USER username [ [ WITH ] option [ ... ] ]

where option can be:
    
  SYSID uid 
  | [ ENCRYPTED | UNENCRYPTED ] PASSWORD 'password'
  | CREATEDB | NOCREATEDB | CREATEUSER | NOCREATEUSER
  | IN GROUP groupname [, ...] | VALID UNTIL 'abstime' 
  

Inputs

username

The name of the user.

uid

The SYSID clause can be used to choose the PostgreSQL user id of the user that is being created. It is not at all necessary that those match the UNIX user ids, but some people choose to keep the numbers the same.

If this is not specified, the highest assigned user id plus one (with a minimum of 100) will be used as default.

ENCRYPTED, UNENCRYPTED

These keywords control whether the password is stored encrypted in pg_shadow. (If neither is specified, the default behavior is determined by the PASSWORD_ENCRYPTION server parameter.) If the presented string is already in MD5-encrypted format, then it is stored as-is, regardless of whether ENCRYPTED or UNENCRYPTED is specified. This allows reloading of encrypted passwords during dump/restore.

See the chapter on client authentication in the Red Hat Database Administrator and User's Guide for details on how to set up authentication mechanisms. Note that older clients may lack support for the MD5 authentication mechanism that's needed to work with passwords that are stored encrypted.

password

Sets the user's password. If you do not plan to use password authentication, you can omit this option; however, the user will not be able to connect to a password-authenticated server. The password can be set or changed later using ALTERUSER.

CREATEDB, NOCREATEDB

These clauses establish the user's ability to create databases. If CREATEDB is specified, the user being defined can create databases. Using NOCREATEDB will deny a user the ability to create databases. NOCREATEDB is the default.

CREATEUSER, NOCREATEUSER

These clauses determine whether a user can create new users. This option will also make the user a superuser that can override all access restrictions. NOCREATEUSER is the default.

groupname

A name of a group into which to insert the user as a new member. Multiple group names can be listed.

abstime

The VALID UNTIL clause sets an absolute time after which the user's password is no longer valid. If this clause is omitted the user account will not expire.

Outputs

CREATE USER

Message returned if the command completes successfully.

Description

CREATE USER will add a new user to an instance of PostgreSQL. Refer to the Red Hat Database Administrator and User's Guide for information about managing users and authentication. You must be a database superuser to use this command.

Notes

PostgreSQL comes with a script createuser that has the same functionality as this command (in fact, it calls this command), but can be run from the command shell.

Use ALTER USER to change a user's password and privileges.

Use DROP USER to remove a user.

Use ALTER GROUP to add or remove the user from other groups.

For more information, refer to the Red Hat Database Administrator and User's Guide.

Usage

Create a user with no password:
CREATE USER fnasser;

Create a user with a password:
CREATE USER patrickm WITH PASSWORD 'jw8s0F4';

Create a user with a password, whose account is valid until the end of 2003. Note that after one second has ticked in 2004, the account is not valid:
CREATE USER pcheung WITH PASSWORD 'jw8s0F4' 
 VALID UNTIL 'Jan 1 2004';

Create an account where the user can create databases:
CREATE USER npadget WITH PASSWORD 'jw8s0F4' CREATEDB;

Compatibility

SQL92

There is no CREATE USER statement in SQL92.