Postgres:创建角色并使用 psql -U 角色登录

won*_*der 6 postgresql

代码:

postgres=# create role hello;
CREATE ROLE
postgres=# \du
                             List of roles
 Role name |                   Attributes                   | Member of
-----------+------------------------------------------------+-----------
 hello     | Cannot login                                   | {}
 postgres  | Superuser, Create role, Create DB, Replication | {}

postgres=# ALTER ROLE hello WITH LOGIN;
ALTER ROLE
postgres=# ALTER ROLE hello WITH CREATEDB;
ALTER ROLE
postgres=# \du
                             List of roles
 Role name |                   Attributes                   | Member of
-----------+------------------------------------------------+-----------
 hello     | Create DB                                      | {}
 postgres  | Superuser, Create role, Create DB, Replication | {}

postgres=# ALTER ROLE hello WITH PASSWORD '123';
ALTER ROLE
postgres=# \q
-bash-4.2$ logout
[root@host test_user]# psql -U hello
Password for user hello:
psql: FATAL:  database "hello" does not exist
Run Code Online (Sandbox Code Playgroud)

我正在尝试创建一个hello在 Postgres 中命名的角色CREATE ROLE,并更改其登录和创建数据库的权限。但是,当我尝试登录时,-U它会显示上述内容。难道我的理解有误吗-U

Mek*_*cha 6

默认情况下,Postgres 身份验证系统做出的另一个假设是,将有一个与用于登录的角色同名的数据库,该角色有权访问该数据库。

请参阅此处:链接以获取更多信息。

创建数据库后,您可以执行以下操作:

sudo -u hello psql
Run Code Online (Sandbox Code Playgroud)

自动登录到 shell。

  • “#”是超级用户帐户的提示符。`=>` 表示您不是超级用户。 (6认同)