繁体   English   中英

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

[英]Postgres : Create a role and login with psql -U role

代码:

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

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

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

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

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

sudo -u hello psql

自动登录到 shell。

所以我遇到了一个类似的问题,基本上postgres期望的是你想要选择的数据库,

当你运行命令 psql -U hello

假设您正在尝试访问 hello 数据库,但默认情况下仅创建 postgres db。 试试这个命令 psql -U hello postgres

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM