Ruby-on-rails 使用PostgreSQL时角色不存在且无法创建数据库

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

Role does not exist and unable to create database when using PostgreSQL

ruby-on-railsrubyruby-on-rails-3postgresql

提问by LearningRoR

I am using Heroku for my application and it requires PostgreSQL but you can still use SQLite3 for development. Since Heroku strongly advised against having 2 different databases I decided to change to PostgreSQL for development. I installed the gem pgand also went to the official PostgreSQL site to get the Windows installer and also changed my database.yml. During installation it requires a password for PostgreSQL so I made one. I had to change the pg_hba.conffile from using md5to trustin order get past: fe_sendauth: no password suppliedwhen trying to create the database.

我在我的应用程序中使用 Heroku,它需要 PostgreSQL,但您仍然可以使用 SQLite3 进行开发。由于 Heroku 强烈建议不要使用 2 个不同的数据库,因此我决定更改为 PostgreSQL 进行开发。我安装了gem pg并且还去了官方 PostgreSQL 站点以获取 Windows 安装程序并更改了我的database.yml. 在安装过程中,它需要 PostgreSQL 的密码,所以我做了一个。我不得不改变pg_hba.conf文件使用md5trust为了得到过去:fe_sendauth: no password supplied试图创建数据库时。

# TYPE  DATABASE        USER            ADDRESS                 METHOD

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

After getting rid of that though, I now get this:

在摆脱它之后,我现在得到了这个:

$ rake db:create
(in C:/app)
FATAL:  role "User" does not exist 
Couldn't create database for {"adapter"=>"postgresql", "encoding"=>"utf8", 
"database"=>"app_test", "pool"=>5, "username"=>nil, "password"=>nil} 

I do still have my development.sqlite3and text.sqlite3present, could that be the issue? What must be done?

我仍然有我的development.sqlite3text.sqlite3礼物,这可能是问题吗?必须做什么?

Here is my full gist: https://gist.github.com/1522188

这是我的完整要点:https: //gist.github.com/1522188

回答by mu is too short

Add a username to your database.yml, might as well use your application's name (or some variant of the name) as the username, I'll use app_nameas a placeholder:

将用户名添加到您的database.yml, 不妨使用您的应用程序名称(或名称的某些变体)作为用户名,我将app_name用作占位符:

development:
  adapter: postgresql
  encoding: utf8
  database: app_development
  pool: 5
  username: app_name
  password:

Then create the user (AKA "role") inside PostgreSQL using psql.exe:

然后使用psql.exe以下命令在 PostgreSQL 中创建用户(又名“角色”):

$ psql -d postgres
postgres=# create role app_name login createdb;
postgres=# \q

The first line is in your terminal, the next two are inside psql. Then do your rake db:create.

第一行在您的终端中,接下来的两行在psql. 然后做你的rake db:create.

The Useruser is possibly a default but useris already takenfor other purposes in PostgreSQL so you'd have to quote it to preserve the case if you wanted to use Useras a username:

User用户可能是一个默认值,但user已采取了PostgreSQL中用于其他用途,所以你不得不引用它,如果你想使用保存的情况下User为用户名:

postgres=# create role "User" login createdb;

You're better off creating one user per-application anyway.

无论如何,您最好为每个应用程序创建一个用户。

You'll want to do similar things for your testentry in database.ymlas well.

你也会想要为你的test条目做类似的事情database.yml

回答by Dennis

PostgreSQL will try to create the database with your account (login) name if a usernameisn't specified in your config/database.yml. On OS X and Linux you can you see who this is with whoami. Looks like you're using Windows.

如果 ausername未在您的config/database.yml. 在 OS X 和 Linux 上,您可以通过whoami. 看起来您正在使用 Windows。

Solution A:Create a PostgreSQL userthat matches the one it's looking for. For example

解决方案 A:创建一个与其要查找的用户相匹配的PostgreSQL 用户。例如

createuser --superuser some_user

Solution B:Change the DB user by explicitly setting a username as shown in mu's answer.

解决方案 B:通过显式设置用户名更改数据库用户,如 mu 的回答所示

回答by Ishan Kanade

If you have a specific account/user on your machine for postgres called postgres for example.

例如,如果您的机器上有一个特定的 postgres 帐户/用户,称为 postgres。

Then executing this command will bring a prompt for you to enter a role name.

然后执行此命令会提示您输入角色名称。

sudo -u postgres createuser --interactive

Then doing

然后做

rake db:create

Should work!

应该管用!