postgresql 无法登录系统用户 postgres

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23982282/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 00:43:32  来源:igfitidea点击:

Can't log into system user postgres

macospostgresqlpostgresql-9.2

提问by アレックス

I installed postgresql on mac using brew. I wasn't asked about for the password for postgresql during the installation. Now I need to create a user and can't:

我使用 brew 在 mac 上安装了 postgresql。我在安装过程中没有被问到 postgresql 的密码。现在我需要创建一个用户并且不能:

Alexs-MacBook-Air:mydir alex$ su - postgres
Password:
su: Sorry

Whatever password (including empty) I use, it is wrong. What's the easiest way to reset it?

无论我使用什么密码(包括空密码),都是错误的。什么是最简单的重置方法?

回答by Daniel Vérité

I installed postgresql on mac using brew. I wasn't asked about for the password for postgresql during the installation.

我使用 brew 在 mac 上安装了 postgresql。我在安装过程中没有被问到 postgresql 的密码。

That's normal because brewdoesn't need or create any postgresaccount. The PostgreSQL processes run under your own account. The other 3 answers so far are wrong in this regard.

这是正常的,因为brew不需要或创建任何postgres帐户。PostgreSQL 进程在您自己的帐户下运行。到目前为止,其他 3 个答案在这方面是错误的。

See the output of brew info postgresfor information.

有关brew info postgres信息,请参见 的输出。

To create a new database account, you may launch, from your own OS account:

要创建新的数据库帐户,您可以从自己的操作系统帐户启动:

/usr/local/bin/createdb someusername

/usr/local/bin/createdb someusername

or within psql:

或在 psql 中:

/usr/local/bin/psql -d postgres

/usr/local/bin/psql -d postgres

and then as an SQL command: CREATE USER someusername PASSWORD 'somepassword';

然后作为 SQL 命令: CREATE USER someusername PASSWORD 'somepassword';

This should work because brew has normally created at initdbtime:

这应该有效,因为 brew 通常在initdb时间创建:

  • a database superuser account with the same login as your OS account (seems to be alexin your case)
  • a database named postgresthat may be used to log info for administrative tasks such as creating a user.
  • 与您的操作系统帐户具有相同登录名的数据库超级用户帐户(似乎是alex您的情况)
  • 一个名为的数据库postgres,可用于记录管理任务(例如创建用户)的信息。

The point of using the full path /usr/local/binis to reach the commands provided by brew, as opposed to the commands with the same name that come with the system and are located in /usr/binor the commands with the same name that are potentially installed by other PostgreSQL providers, such as postgres.app or macports or entreprisedb. There are 5-6 competing and incompatible ways of getting postgresql installed on Mac OS X.

使用完整路径的目的/usr/local/bin是到达由 提供的命令brew,而不是系统/usr/bin附带的同名命令位于或其他 PostgreSQL 提供程序可能安装的同名命令,例如作为 postgres.app 或 macports 或 entreprisedb。在 Mac OS X 上安装 postgresql 有 5-6 种相互竞争且不兼容的方法。

EDIT: the newer versions of MacOS X desktop edition no longer have the postgres client-side commands pre-installed. This seems to be the case at least since MacOS X 10.10 (Yosemite) and possibly 10.9.

编辑:较新版本的 MacOS X 桌面版不再预先安装 postgres 客户端命令。至少从 MacOS X 10.10 (Yosemite) 和 10.9 开始,情况似乎如此。

回答by Craig Ringer

You're trying to access the systemuser named postgres. According to comments you left on the other answers, there is no such user. You can't change the password of a user that doesn't exist.

您正在尝试访问名为的系统用户postgres。根据您在其他答案中留下的评论,没有这样的用户。您无法更改不存在的用户的密码。

Perhaps it's _postgresor postgres_? I know some installers on OS X use weird names. Or perhaps your install never successfully created a postgresuser on your system in the first place, so you can't set a password for it?

也许是_postgrespostgres_?我知道 OS X 上的一些安装程序使用奇怪的名称。或者也许您的安装从未postgres在您的系统上成功创建用户,因此您无法为其设置密码?

In general you never need to set a password for this user. You can just

通常,您永远不需要为此用户设置密码。你可以

sudo -u postgres psql

or whatever command you need to run as the postgressuperuser.

或者您需要以postgres超级用户身份运行的任何命令。

Note that you shouldn't need sufor anything.

请注意,您不需要su任何东西。

回答by Erwin Brandstetter

There is no default password. To run a shell as user postgresuse:

没有默认密码。要以用户身份运行 shell,请postgres使用:

sudo su - postgres

or the more modern form (as advised by Craig):

或更现代的形式(根据 Craig 的建议):

sudo -u postgres -i

Type exitwhen done. More:
PostgreSQL error: Fatal: role "username" does not exist

exit完成后键入。更多:
PostgreSQL 错误:致命:角色“用户名”不存在

Turns out, the user wasn't created at all. Look to @Daniel's answer.

事实证明,根本没有创建用户。看看@Daniel 的回答

回答by Tamer Shlash

Why not:

为什么不:

sudo passwd postgres

This will prompt you for the password of your current account. Then it will ask you to enter the new password (twice) for the postgresuser.

这将提示您输入当前帐户的密码。然后它会要求您为用户输入新密码(两次)postgres