Search code examples
postgresqlpsql

Username seen as database in psql command?


This question is similar to create database using psql in shell script takes username as db name, but since that doesn't have an accepted answer I'm asking it again here. I've created a user with username myuser and password mypassword:

> psql
psql (11.5)
Type "help" for help.

kurt=# \du
                                   List of roles
 Role name |                         Attributes                         | Member of 
-----------+------------------------------------------------------------+-----------
 kurt      | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
 myuser    |                                                            | {}

kurt=# 

However, if I do psql --username=myuser, I get an error that database "myuser" does not exist:

> psql --username=myuser
psql: FATAL:  database "myuser" does not exist

I'm a bit confused by this error message, because according to psql --help, this is a user name, not a database name:

Connection options:
  -h, --host=HOSTNAME      database server host or socket directory (default: "local socket")
  -p, --port=PORT          database server port (default: "5432")
  -U, --username=USERNAME  database user name (default: "kurt")
  -w, --no-password        never prompt for password
  -W, --password           force password prompt (should happen automatically)

Any idea what I'm doing wrong? Here is the version of psql I'm using:

> psql --version
psql (PostgreSQL) 11.5

Solution

  • Quote from the manual

    The default user name is your operating-system user name, as is the default database name

    If you specify a username, that username is then also assumed as the default for the database name.