无法连接到数据库 postgres:致命:角色“_postgres”不存在

Could not connect to database postgres: FATAL: role"_postgres" does not exist

正在使用 OS X 10.10,安装了 postgreSQL 和 PostGIS here,psql 版本 9.3.5。我很难获得 postgreSQL 运行.

我在我的电脑上以管理员身份安装了这些包。我的用户名是 christoph

我可以登录的唯一方法是通过:

$ psql -U postgres

我想创建名为 christoph 的用户作为我在计算机上的管理员名称。 我试过了(从终端,没有 "logged in into psql"):

$ sudo -u postgres createuser christoph
> sudo: unknown user: postgres

然后我读了一点并尝试(从终端,而不是"logged in into psql"):

$ sudo -u _postgres createuser christoph
> Password: ****
> Could not connect to database postgres: FATAL: role "_postgres" does not exist

为什么这样行得通?

在 OS X 的最新版本中以及使用某些安装方法,系统用户是在 'postgres' 前添加或附加“_”创建的,因此您的系统用户的名称是 postgres__postgres,而不是 'postgres'。我不使用 OS X,所以我不知道是什么驱使他们这样做。似乎他们想要遵守系统帐户的命名模式。 不要与名称 postgres 的 Postgres DB 用户(登录角色)混淆。这种不匹配会导致各种混乱。至少人们开始意识到一些语法元素的不同含义......

因此您可以通过以下方式登录 Postgres:

$ psql -U postgres

postgres 是这里的 DB 角色,而不是 OS 用户。

但这行不通:

$ sudo -u postgres

因为没有 OS 这个名字的用户。改为尝试:

$ sudo -u _postgres

但是peer authentication还是不行,因为没有同名的数据库用户_postgres。相关回答:

  • Postgres user does not exist?

标准安装中默认激活的身份验证方法是对等身份验证,本地系统上的系统用户无需密码即可访问同名数据库角色。这解释了最后一条错误消息:

Could not connect to database postgres: FATAL: role "_postgres" does not exist

您的系统尝试使用对等身份验证使用与当前 OS 用户相同的名称登录数据库,但由于命名不匹配而失败。

你的最后一个命令应该像这样工作:

$ sudo -u _postgres createuser <b>-U postgres</b> christoph

添加的 -U postgrescreateuser 的一个选项,用于指定用于登录的数据库角色。

您还需要输入密码。我会考虑使用 a .pgpass file 中的条目进行无密码访问,而系统用户不同于假定关联的数据库角色。

相关:

  • Login Failed with Existing User on PostgreSQL
  • Run batch file with psql command without password