Postgresql Create User Statement

PostgreSQL: CREATE USER statement

In this PostgreSQL post 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 '<strong>password_value</strong>'
  | VALID UNTIL <strong>'expiration'</strong> ];

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

In this example, the CREATE USER statement would create a new user called AODBA. 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 AODBA
  WITH PASSWORD 'fantastic';

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

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

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

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

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