PostgreSQL 无法创建用户

PostgreSQL can't create user

问题:我可以从 shell(bash、OSX)但不能从 postgres cli 创建用户或数据库。来自 bash 如果成功,我不会得到确认。

如果我尝试在 psql 中创建角色,那么我不会得到任何响应,也不会生成任何错误。如果我尝试从 bash createuser 然后如果成功它不会报告任何内容,如果不成功那么它会生成错误:"role username already exists".

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-# 

使用 createdb 时会发生类似的事情。

如何在 postgres cli 中创建用户和数据库? 在 bash 中对大多数 postgres 命令没有响应是正常的吗?

信息:用户及其权限:

Yunti-# \du
                             List of roles
 Role name |                   Attributes                   | Member of 
-----------+------------------------------------------------+-----------
 Yunti     | Superuser, Create role, Create DB, Replication | {}
 anything  |                                                | {}
 monkey    |                                                | {}

我不知道 postgres 客户端是否在 OSX 中使用其他命令,因为它在 Linux 中,但我认为它是相同的。

docs link 显示了 postgres 客户端的一些选项:

似乎“\l”列出了数据库,而您希望看到的选项是角色及其访问权限,即“\du”。

从客户端创建数据库时,您应该会收到 "CREATING DATABASE" 形式的响应。也许您遇到了某种语法错误?

我不认为用户是在别处创建的。

希望这能解决您的一些问题。

您的语句未执行,因为您没有使用 ;.

正确终止它们

引用 from the manual:

A command is composed of a sequence of tokens, terminated by a semicolon (";").

并在 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.

如果你这样做,你会得到这样的输出:

psql (9.4.4)
Type "help" for help.

postgres=# CREATE ROLE testuser;
CREATE ROLE  ---<<<< this tells you the statement was executed

postgres=#