postgresqlcreateuser

PostgreSQL `createuser` without specifying username


I ran:

$ createuser --pwprompt

and saw:

Enter password for new role: 

I expected to be prompted for a username, so at this point I hit CTRL+C.

The man page synopsis says that the username is optional:

createuser [connection-option...] [option...] [username]

The only other info given is:

username

Specifies the name of the PostgreSQL user to be created. This name must be different from all existing roles in this PostgreSQL installation.

What user is createuser operating on when none is given?


Solution

  • You failed to read this part of the documentation (it took me a while too):

    --interactive

    Prompt for the user name if none is specified on the command line, and also prompt for whichever of the options -d/-D, -r/-R, -s/-S is not specified on the command line. (This was the default behavior up to PostgreSQL 9.1.)

    You didn't specify --interactive, so you are not prompted.

    In consequence, PostgreSQL chooses the default user: unless PGUSER is set, that is your operating system user.

    So, assuming that you are operating system user postgres, you effectively did the following:

    createuser -U postgres postgres
    

    which is of course silly and would have led to

    createuser: creation of new role failed: ERROR:  role "postgres" already exists
    

    if you hadn't chickened out and interrupted execution.