【问题标题】:Can not CREATE USER in postgresql无法在 postgresql 中创建用户
【发布时间】:2023-03-11 00:01:01
【问题描述】:

我在 Windows 的 powershell、命令提示符和 ubuntu 上进行了尝试:

postgres=# CREATE USER sally WITH PASSWORD 'secret';
CREATE ROLE
postgres=# \du
                                   List of roles
 Role name |                         Attributes                         | Member of
-----------+------------------------------------------------------------+-----------
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
 sally     |                                                            | {}
postgres=# \q
> psql -U sally
Password for user sally:
psql: error: could not connect to server: FATAL:  password authentication failed for user "sally"

如果我写 psql$ sudo su - postgres(在 ubuntu 中)

它会询问我的 Windows 用户名(恭敬地):

Password for user xyz:

[sudo] password for xyz:

这是我的pg_hba.conf

...
# local      DATABASE  USER  METHOD  [OPTIONS]
# host       DATABASE  USER  ADDRESS  METHOD  [OPTIONS]
# hostssl    DATABASE  USER  ADDRESS  METHOD  [OPTIONS]
# hostnossl  DATABASE  USER  ADDRESS  METHOD  [OPTIONS]
#
...
# TYPE  DATABASE        USER            ADDRESS                 METHOD

# IPv4 local connections:
host    all             all             127.0.0.1/32            md5
# IPv6 local connections:
host    all             all             ::1/128                 md5
# Allow replication connections from localhost, by a user with the
# replication privilege.
host    replication     all             127.0.0.1/32            md5
host    replication     all             ::1/128                 md5

【问题讨论】:

    标签: postgresql powershell command-prompt ubuntu-20.04


    【解决方案1】:

    在连接时添加-h 127.0.0.1,或在pg_hba.conf 中添加local 规则。

    此外,如果它与用户名不同,请指定所需的数据库名称:-d <dbname>

    【讨论】:

    • 我不知道为什么我的电脑这么奇怪,它确实连接到数据库,只有在我登录到'postgres'和create database sally(相同的用户名和数据库):sally=>,和我还测试了我是否create database mydb 它又给了我psql: error: could not connect to server: FATAL: password authentication failed for user "mydb"
    • 我还注意到 在与所有者 sally 创建数据库之前如果我尝试以 psql -U sally -h 127.0.0.1 连接我得到 = psql: error: could not connect to server: FATAL: database "sally" does not exist 为什么它在寻找数据库,当我 1. 创建用户, 2. 尝试连接到USER?
    • 在连接时指定数据库名称,否则 psql 将寻找与用户同名的数据库。可能是postgres,所以在命令末尾添加-d postgres 或只是postgres
    猜你喜欢
    • 2015-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    • 2020-07-05
    • 1970-01-01
    • 2014-04-10
    • 2017-11-10
    相关资源
    最近更新 更多