Ruby-on-rails Rails 和 PostgreSQL:角色 postgres 不存在

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

Rails and PostgreSQL: Role postgres does not exist

ruby-on-railspostgresqlrake

提问by Adam

I have installed PostgreSQL on my Mac OS Lion, and am working on a rails app. I use RVM to keep everything separate from my other Rails apps.

我已经在我的 Mac OS Lion 上安装了 PostgreSQL,并且正在开发一个 Rails 应用程序。我使用 RVM 将所有内容与我的其他 Rails 应用程序分开。

For some reason when I try to migrate the db for the first time rake cannot find the postgres user. I get the error

出于某种原因,当我第一次尝试迁移数据库时,rake 找不到 postgres 用户。我收到错误

 FATAL:  role "postgres" does not exist

I have pgAdmin so I can clearly see there is a postgres user in the DB - the admin account in fact - so I'm not sure what else to do.

我有 pgAdmin,所以我可以清楚地看到数据库中有一个 postgres 用户 - 实际上是管理员帐户 - 所以我不确定还能做什么。

I read somewhere about people having issues with PostgreSQL because of which path it was installed in, but then I don't think I would have gotten that far if it couldn't find the db.

我在某处读到有人因为 PostgreSQL 安装在哪个路径而遇到问题,但是如果找不到 db,我认为我不会走那么远。

采纳答案by Erwin Brandstetter

This message pops up, when the database user does not exist. Compare the manual here.
Multiple local databasescannotbe the explanation. Roles are valid cluster-wide. The manual again:

当数据库用户不存在时,会弹出此消息。比较这里手册
多个本地数据库不能解释。角色在集群范围内有效。该手册再次

Note that roles are defined at the database cluster level, and so are valid in all databases in the cluster.

请注意,角色是在数据库集群级别定义的,因此在集群中的所有数据库中都有效。

You must be ending up in another database-cluster. That would be another server running on the same machine, listening to a different port. Or, more likely, on a different machine.

您必须在另一个database-cluster 中结束。那将是在同一台机器上运行的另一台服务器,侦听不同的端口。或者,更有可能,在不同的机器上。

Could it be that the message comes, in fact, from the remote server?

消息实际上可能来自远程服务器吗?

回答by Chris Sherlock

Actually, for some unknown reason, I found the issue was actually because the postgresql role hadn't been created.

实际上,出于某种未知原因,我发现问题实际上是因为尚未创建 postgresql 角色。

Try running:

尝试运行:

createuser -s -r postgres

Note that roles are the way that PostgreSQL maintains database permissions. If there is no role for the postgres user, then it can't access anything. The createuser command is a thin wrapperaround the commands CREATE USER, CREATE ROLE, etc.

请注意,角色是 PostgreSQL维护数据库权限的方式。如果 postgres 用户没有角色,则它无法访问任何内容。createuser 命令是CREATE USER、CREATE ROLE 等命令的瘦包装器

回答by Prem

Recently i got this problem immediately after installing postgres. If it comes immediately after installation, you might be missing the default user, postgres. In that case, you can create default user postgres using below command.

最近我在安装 postgres 后立即遇到了这个问题。如果它在安装后立即出现,则您可能缺少默认用户 postgres。在这种情况下,您可以使用以下命令创建默认用户 postgres。

createuser -s -U $USER

createuser -s -U $USER

Ex: createuser -s -U $USER
enter your required role name: postgres
enter password for your the user: 

It will prompt you to enter required database role name and password Once you complete the process, you can login to the postgres console using below command

它将提示您输入所需的数据库角色名称和密码 完成此过程后,您可以使用以下命令登录 postgres 控制台

psql -U 'your_database_name'

Ex: psql -U postgres
Here, You need to enter the password if you have given any, while creating the user.

例如: psql -U postgres
这里,如果您提供了密码,则在创建用户时需要输入密码。

Hope it helps :)

希望能帮助到你 :)

回答by Hyman

I was on OSX 10.8, and everything I tried would give me the FATAL: role "USER" does not exist. Like many people said here, run createuser -s USER, but that gave me the same error. This finally worked for me:

我在 OSX 10.8 上,我尝试的一切都会给我FATAL: role "USER" does not exist. 就像很多人在这里说的那样, run createuser -s USER,但这给了我同样的错误。这最终对我有用:

$ sudo su
# su postgres
# createuser -s --username=postgres MYUSERNAME

The createuser -s --username=postgrescreates a superuser (-s flag) by connecting as postgres (--username=postgres flag).

createuser -s --username=postgres创建通过连接成postgres(--username = postgres的标志)超级用户(-s标志)。

I see that your question has been answered, but I want to add this answer in for people using OSX trying to install PostgreSQL 9.2.4.

我看到您的问题已得到解答,但我想为使用 OSX 尝试安装 PostgreSQL 9.2.4 的人添加此答案。

回答by sovanlandy

I met this issue right on when I first install the Heroku's POSTGRES.app thing. After one morning trial and error i think this one line of code solved problem. As describe earlier, this is because postgresql does not have default role the first time it is set up. And we need to set that.

我第一次安装 Heroku 的 POSTGRES.app 时就遇到了这个问题。经过一个上午的反复试验,我认为这一行代码解决了问题。如前所述,这是因为 postgresql 在第一次设置时没有默认角色。我们需要设置它。

sovanlandy=# CREATE ROLE postgres LOGIN;

You must log in to your respective psql console to use this psql command.

您必须登录到各自的 psql 控制台才能使用此 psql 命令。

Also noted that, if you already created the role 'postgre' but still get permission errors, you need to alter with command:

还要注意的是,如果您已经创建了角色“postgre”但仍然出现权限错误,则需要使用以下命令进行更改:

sovanlandy=# ALTER ROLE postgres LOGIN;

Hope it helps!

希望能帮助到你!

回答by Yunrock

In the Heroku documentation; Getting started whit rails 4, they say:

在 Heroku 文档中;入门 whit rails 4,他们说:

You will also need to remove the username field in your database.yml if there is one so: In file config/database.yml remove: username: myapp

如果有的话,您还需要删除 database.yml 中的用户名字段:在文件 config/database.yml 中删除:用户名:myapp

Then you just delete that line in "development:", if you don't pg tells to the database that works under role "myapp"

然后你只需删除“development:”中的那一行,如果你不 pg 告诉在角色“myapp”下工作的数据库

This line tells rails that the database myapp_development should be run under a role of myapp. Since you likely don't have this role in your database we will remove it. With the line remove Rails will try to access the database as user who is currently logged into the computer.

这一行告诉 rails 数据库 myapp_development 应该在 myapp 的角色下运行。由于您的数据库中可能没有此角色,我们将删除它。随着行 remove Rails 将尝试以当前登录到计算机的用户身份访问数据库。

Also remember to create the database for development:

还记得创建用于开发的数据库:

$createdb myapp_development

Repleace "myapp" for your app name

将“myapp”替换为您的应用程序名称

回答by Jonathan Julian

Could you have multiple local databases? Check your database.ymland make sure you are hitting the pg db that you want. Use rails consoleto confirm.

你可以有多个本地数据库吗?检查database.yml并确保您正在点击所需的 pg db。使用rails console确认。

回答by maudulus

My answer was much more simple. Just went to the db folder and deleted the id column, which I had tried to forcefully create, but which is actually created automagically. I also deleted the USERNAME in the database.yml file (under the config folder).

我的回答要简单得多。刚刚进入 db 文件夹并删除了 id 列,我试图强行创建,但实际上是自动创建的。我还删除了 database.yml 文件(在 config 文件夹下)中的 USERNAME。

回答by Robert Cambil

In Ubuntu local user command prompt, but not root user, type

在 Ubuntu 本地用户命令提示符下,而不是 root 用户,输入

sudo -u postgres createuser username

sudo -u postgres createuser 用户名

username above should match the name indicated in the message "FATAL: role 'username' does not exist."

上面的用户名应与消息“致命:角色‘用户名’不存在”中指示的名称相匹配。

Enter password for username.

输入用户名的密码。

Then re-enter the command that generated role does not exist message.

然后重新输入生成角色不存在消息的命令。

回答by Mayur

The installation procedure creates a user account called postgresthat is associated with the default Postgres role. In order to use Postgres, you can log into that account. But if not explicitly specified the rails app looks for a different role, more particularly the role having your unix username which might not be created in the postgres roles.

安装过程会创建一个名为的用户帐户postgres,该帐户与默认 Postgres 角色相关联。为了使用 Postgres,您可以登录该帐户。但是,如果没有明确指定,rails 应用程序会寻找不同的角色,尤其是具有您的 unix 用户名的角色,这可能不是在 postgres 角色中创建的。

To overcome that, you can create a new role, first by switching over to the default role postgres which was created during installation

为了克服这个问题,您可以创建一个新角色,首先切换到安装期间创建的默认角色 postgres

sudo -i -u postgres

sudo -i -u postgres

After you are logged in to the postgres account, you can create a new user by the command:

登录postgres账号后,可以通过命令新建一个用户:

createuser --interactive

createuser --interactive

This will prompt you with some choices and, based on your responses, execute the correct Postgres commands to create a user.

这将提示您进行一些选择,并根据您的回答执行正确的 Postgres 命令以创建用户。

Pass over a role name and some permissions and the role is created, you can then migrate your db

传递角色名称和一些权限并创建角色,然后您可以迁移您的数据库