totn PostgreSQL

PostgreSQL: CREATE USER statement

This PostgreSQL tutorial explains how to use the PostgreSQL CREATE USER statement with syntax and examples.

Description

The CREATE USER statement creates a database account that allows you to log into the PostgreSQL database.

Syntax

The syntax for the CREATE USER statement in PostgreSQL is:

CREATE USER user_name
  [ WITH [ ENCRYPTED | UNENCRYPTED ] PASSWORD 'password_value'
  | VALID UNTIL 'expiration' ];

Parameters or Arguments

user_name
The name of the database account that you wish to create.
password_value
The password to assign to user_name.
expiration
The date/time value when the password will expire. If you never want the password to expire, you set expiration to 'infinity'.

Example

Let's look at how to create a user in PostgreSQL using the CREATE USER statement.

For example:

CREATE USER techonthenet;

In this example, the CREATE USER statement would create a new user called techonthenet. This new user would not have a password, but you could use the ALTER USER statement to assign a password later.

If you wanted to assign a password at the time that the user is created, you could change the CREATE USER statement as follows:

CREATE USER techonthenet
  WITH PASSWORD 'fantastic';

This would create a user called techonthenet with a password of 'fantastic'.

If you wanted to create the user techonthenet with a password of 'fantastic' that expires on January 1, 2015, you would use the CREATE USER statement as follows:

CREATE USER techonthenet
  WITH PASSWORD 'fantastic'
  VALID UNTIL 'Jan 1, 2015';

If you wanted the password for the user techonthenet to never expire, you would use the CREATE USER statement as follows:

CREATE USER techonthenet
  WITH PASSWORD 'fantastic'
  VALID UNTIL 'infinity';