PostgreSQL 无法创建用户
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32147953/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PostgreSQL can't create user
提问by Yunti
Issue: I can create users or databases from the shell (bash, OSX) but not postgres cli. From bash I get no confirmation if successful.
问题:我可以从 shell(bash、OSX)而不是 postgres cli 创建用户或数据库。如果成功,我不会从 bash 得到确认。
If I try to CREATE ROLE in psql then I get no response and it doesn't generate any error. If I try to createuserfrom bash then if successful it reports back nothing, if unsuccessful then it does generate the error: "role username already exists".
如果我尝试在 psql 中创建角色,那么我不会收到任何响应,也不会产生任何错误。如果我尝试CREATEUSER从bash接下来如果成功他报告倒没什么,如果不成功则它产生的错误:“角色的用户名已经存在”。
Example:
Yunti-# CREATE ROLE testuser
Yunti-# \du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------+-----------
Yunti | Superuser, Create role, Create DB, Replication | {}
anything | | {}
monkey | | {}
Yunti-# CREATE DATABASE testdb
Yunti-# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+-------+----------+-------------+-------------+-------------------
Yunti | Yunti | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
postgres | Yunti | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | Yunti | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/Yunti +
| | | | | Yunti=CTc/Yunti
template1 | Yunti | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/Yunti +
| | | | | Yunti=CTc/Yunti
test | Yunti | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
test5 | Yunti | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
(6 rows)
Yunti-#
A similar thing happens when using createdb.
使用 createdb 时也会发生类似的事情。
How can I create users and databases in postgres cli? And is this normal to get no response to most postgres commands in bash?
如何在 postgres cli 中创建用户和数据库?在 bash 中对大多数 postgres 命令没有响应是否正常?
Info: users and their privileges:
信息:用户及其权限:
Yunti-# \du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------+-----------
Yunti | Superuser, Create role, Create DB, Replication | {}
anything | | {}
monkey | | {}
回答by a_horse_with_no_name
Your statements are not executed, because you don't terminate them properly using a ;
.
您的语句不会执行,因为您没有使用;
.
Quote from the manual:
手册中的引用:
A command is composed of a sequence of tokens, terminated by a semicolon (";").
命令由一系列标记组成,以分号 (";") 结尾。
And in the manual for psql:
At the prompt, the user can type in SQL commands. Ordinarily, input lines are sent to the server when a command-terminating semicolon is reached. An end of line does not terminate a command. Thus commands can be spread over several lines for clarity. If the command was sent and executed without error, the results of the command are displayed on the screen.
在提示符下,用户可以键入 SQL 命令。通常,当到达命令终止分号时,输入行会发送到服务器。行尾不会终止命令。因此,为了清楚起见,命令可以分布在多行上。如果命令发送和执行没有错误,命令的结果将显示在屏幕上。
If you do that, you get an output like this:
如果你这样做,你会得到这样的输出:
psql (9.4.4)
Type "help" for help.
postgres=# CREATE ROLE testuser;
CREATE ROLE ---<<<< this tells you the statement was executed
postgres=#
回答by zervyvin
I dont know if the postgres client is using other commands in OSX as it is in Linux but I assume it is the same.
我不知道 postgres 客户端是否像在 Linux 中一样在 OSX 中使用其他命令,但我认为它是相同的。
This docs linkshows some options for the postgres client:
此文档链接显示了 postgres 客户端的一些选项:
It seems like "\l" lists the databases while the option you would like to see is roles and their access which is "\du".
似乎“\l”列出了数据库,而您希望看到的选项是角色及其访问权限,即“\du”。
When creating a database from within the client you should get a response in the form of "CREATING DATABASE". Maybe you are having some sort of syntax error?
从客户端创建数据库时,您应该得到“CREATING DATABASE”形式的响应。也许你有某种语法错误?
I don't think users is created elsewhere.
我不认为用户是在其他地方创建的。
I hope this solves some of your problems.
我希望这可以解决您的一些问题。