PostgreSQL - CREATE USER
The PostgreSQL CREATE USER statement is used to create a database account that allows the specified user to log into the PostgreSQL database.
Syntax
The syntax of using CREATE USER statement in PostgreSQL is given below:
CREATE USER user_name [ WITH IN GROUP groupname [, ...] | [ ENCRYPTED | UNENCRYPTED ] PASSWORD 'password_value' | VALID UNTIL 'expiration' ];
Parameters
user_name |
Required. Specify the name of the new user. |
groupname |
Optional. Specify a name of an existing group into which to insert the user as a new member. Multiple group names may be listed. |
password_value |
Optional. Specify the password to assign to user_name. |
expiration |
Optional. Specify the date/time value when the password will expire. If this clause is omitted the password will be valid for all time. |
Example: Create a user with no password
In the example below, the CREATE USER statement is used to create a new user called john in the PostgreSQL database with no password.
CREATE USER john;
Example: Create a user with password
To create a user with password the PASSWORD clause can be used. In the example below, a new user called john is created with password 'password1'.
CREATE USER john WITH PASSWORD 'password1';
Example: Set the password expiry date
The VALID UNTIL clause can be used to set an absolute time after which the user's password will not be valid. If this clause is omitted the password will be valid for all time. In the example below, the user's password will be valid until 'Dec 31, 2025'.
CREATE USER john WITH PASSWORD 'password1' VALID UNTIL 'Dec 31, 2025';