PostgreSQL 在用户存在时给出用户不存在

PostgreSQL gives user doesn't exists when user exists

为了创建用户,我执行了以下查询:

CREATE DATABASE yourdbname;
CREATE USER username WITH ENCRYPTED PASSWORD 'yourpass';
GRANT ALL PRIVILEGES ON DATABASE yourdbname TO username;

我检查用户是否是这样创建的:

su postgres
bash-4.2$ psql -h localhost -p 5432
psql (12.2)
Type "help" for help.
postgres=# \du
                                   List of roles
 Role name |                         Attributes                         | Member of 
-----------+------------------------------------------------------------+-----------
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
 username  |                                                            | {}

postgres=# 

现在,当我想连接时:

$psql -h localhost -p 5432 -U username
psql: error: could not connect to server: FATAL:  database "username" does not exist

谁能告诉我怎么解决?

尝试:

$ psql -h localhost -p 5432 -U username yourdbname

默认情况下,psql 始终认为您的默认数据库名称与您的用户名相同。因此,为了克服这个问题,请确保在连接到数据库时使用 -d dbname。

例如./psql -h localhost -p 5432 -d yourdbname -U 用户名