September 26, 2024: PostgreSQL 17 Released!
Supported Versions: Current (17) / 16 / 15 / 14 / 13 / 12
Development Versions: devel
Unsupported versions: 11 / 10 / 9.6 / 9.5 / 9.4 / 9.3 / 9.2 / 9.1 / 9.0 / 8.4 / 8.3 / 8.2 / 8.1 / 8.0 / 7.4 / 7.3 / 7.2 / 7.1
This documentation is for an unsupported version of PostgreSQL.
You may want to view the same page for the current version, or one of the other supported versions listed above instead.

createuser

Name

createuser -- define a new PostgreSQL user account

Synopsis

createuser [option...] [username]

Description

createuser creates a new PostgreSQL user. Only superusers (users with usesuper set in the pg_shadow table) can create new PostgreSQL users, so createuser must be invoked by someone who can connect as a PostgreSQL superuser.

Being a superuser also implies the ability to bypass access permission checks within the database, so superuserdom should not be granted lightly.

createuser is a wrapper around the SQL command CREATE USER. There is no effective difference between creating users via this utility and via other methods for accessing the server.

Options

createuser accepts the following command-line arguments:

username

Specifies the name of the PostgreSQL user to be created. This name must be unique among all users of this PostgreSQL installation.

-a
--adduser

The new user is allowed to create other users. (Note: Actually, this makes the new user a superuser. The option is poorly named.)

-A
--no-adduser

The new user is not allowed to create other users (i.e., the new user is a regular user, not a superuser). This is the default.

-d
--createdb

The new user is allowed to create databases.

-D
--no-createdb

The new user is not allowed to create databases. This is the default.

-e
--echo

Echo the commands that createuser generates and sends to the server.

-E
--encrypted

Encrypts the user's password stored in the database. If not specified, the default password behavior is used.

-i number
--sysid number

Allows you to pick a non-default user ID for the new user. This is not necessary, but some people like it.

-N
--unencrypted

Does not encrypt the user's password stored in the database. If not specified, the default password behavior is used.

-P
--pwprompt

If given, createuser will issue a prompt for the password of the new user. This is not necessary if you do not plan on using password authentication.

-q
--quiet

Do not display a response.

You will be prompted for a name and other missing information if it is not specified on the command line.

createuser also accepts the following command-line arguments for connection parameters:

-h host
--host host

Specifies the host name of the machine on which the server is running. If the value begins with a slash, it is used as the directory for the Unix domain socket.

-p port
--port port

Specifies the TCP port or local Unix domain socket file extension on which the server is listening for connections.

-U username
--username username

User name to connect as (not the user name to create).

-W
--password

Force password prompt (to connect to the server, not for the password of the new user).

Environment

PGHOST
PGPORT
PGUSER

Default connection parameters

Diagnostics

In case of difficulty, see CREATE USER and psql for discussions of potential problems and error messages. The database server must be running at the targeted host. Also, any default connection settings and environment variables used by the libpq front-end library will apply.

Examples

To create a user joe on the default database server:

$ createuser joe
Shall the new user be allowed to create databases? (y/n) n
Shall the new user be allowed to create more new users? (y/n) n
CREATE USER

To create the same user joe using the server on host eden, port 5000, avoiding the prompts and taking a look at the underlying command:

$ createuser -h eden -p 5000 -D -A -e joe
CREATE USER joe NOCREATEDB NOCREATEUSER;
CREATE USER

To create the user joe as a superuser, and assign a password immediately:

$ createuser -P -d -a -e joe
Enter password for new user: xyzzy
Enter it again: xyzzy
CREATE USER joe PASSWORD 'xyzzy' CREATEDB CREATEUSER;
CREATE USER

In the above example, the new password isn't actually echoed when typed, but we show what was typed for clarity. However the password will appear in the echoed command, as illustrated — so you don't want to use -e when assigning a password, if anyone else can see your screen.