简体   繁体   English

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

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

Code:代码:

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

I am trying to create a role named hello in Postgres with CREATE ROLE and changed it's permission for logging in and created database.我正在尝试使用CREATE ROLE在 Postgres 中创建一个名为hello的角色,并更改了它的登录权限和创建数据库。 However, when I try to login with -U it shows me the above.但是,当我尝试使用-U登录时,它会显示上述内容。 Is my understanding wrong here for -U ?我对-U的理解是错误的吗?

By default, another assumption that the Postgres authentication system makes is that there will be an database with the same name as the role being used to login, which the role has access to.默认情况下,Postgres 身份验证系统所做的另一个假设是,将有一个与用于登录的角色同名的数据库,该角色可以访问该数据库。

See here: link for more info.请参阅此处: 链接以获取更多信息。

After creating the database, you can then do:创建数据库后,您可以执行以下操作:

sudo -u hello psql

to log in to the shell automatically.自动登录到 shell。

so i ran into a similar issue, Basically what postgres is expecting is a database that you want to choose,所以我遇到了一个类似的问题,基本上postgres期望的是你想要选择的数据库,

And when you run the command psql -U hello当你运行命令 psql -U hello

It is assuming that you are trying to access hello database, But by default only postgres db is created .假设您正在尝试访问 hello 数据库,但默认情况下仅创建 postgres db。 Try this command instead psql -U hello postgres试试这个命令 psql -U hello postgres

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

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