Ruby-on-rails Postgres 权限被拒绝在 rake db:create:all 上创建数据库

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

Postgres permission denied to create database on rake db:create:all

ruby-on-railsrubypostgresql

提问by Beengie

I am trying to create postgres databases for development and tests.

我正在尝试为开发和测试创建 postgres 数据库。

I'm using...

我正在使用...

  • OSX Yosemite
  • Rails version: 4.2.0
  • git version: 2.2.2
  • psql version: 9.4.0
  • ruby version: 2.1.0p0
  • HomeBrew version: 0.9.5
  • OSX优胜美地
  • 导轨版本:4.2.0
  • git 版本:2.2.2
  • psql 版本:9.4.0
  • 红宝石版本:2.1.0p0
  • HomeBrew 版本:0.9.5

Gemfile...

宝石档案...

gem 'pg'

database.yml

数据库.yml

default: &default
  adapter: postgresql
  encoding: unicode
  pool: 5

development:
  <<: *default
  database: myapp_development
  username: username
  password: 

test:
  <<: *default
  database: myapp_test

rake db:create:allreturns

rake db:create:all回报

PG::InsufficientPrivilege: ERROR:  permission denied to create database
: CREATE DATABASE "myapp_development" ENCODING = 'unicode'
.... (lots of tracing)
Couldn't create database for {"adapter"=>"postgresql", "encoding"=>"unicode", "pool"=>5, "database"=>"myapp_development", "username"=>"username", "password"=>nil}
myapp_test already exists

What is wrong?

怎么了?

EDITI just tried changing the username in the database.yml to my username that I'm using on my Mac. It worked. It also told me that not only maybe_test' already exists, but it also just told me thatmyapp_development` already exists too.

编辑我只是尝试将 database.yml 中的用户名更改为我在 Mac 上使用的用户名。有效。它还告诉我不仅maybe_test' already exists, but it also just told me thatmyapp_development` 已经存在。

  • Why wouldn't it be able to use the other username that I had created and assigned a role to CREATEDB?
  • Why did it say that the development couldn't be created then tell me that it already existed?
  • 为什么它不能使用我创建并为其分配角色的其他用户名CREATEDB
  • 为什么说不能创建开发然后告诉我它已经存在?

This all seems way too confusing and reminds me of php setup with apache back in the very old days. I don't want to have to deal with problems every time I create a new app and try to follow the heroku recommendations to use postgres during development too.

这一切似乎太混乱了,让我想起了过去使用 apache 进行的 php 设置。我不想每次创建新应用程序时都必须处理问题,并尝试遵循 heroku 建议在开发过程中也使用 postgres。

回答by Rokibul Hasan

I have faced same issues when running rake db:test:preparein postgresqlon my Ruby on Railsproject. This is pretty clear from the error message, that its a permission issue for the user. I added CREATEDBpermission for new_useras following from the console.

运行时,我面临同样的问题rake db:test:preparepostgresql我的Ruby on Rails的项目。从错误消息中可以清楚地看出,这是用户的权限问题。我从控制台添加CREATEDBnew_user以下权限。

To access postgres console:

要访问 postgres 控制台:

$ sudo -u postgres -i

postgres@host:~$ psql

In there:

在那里:

postgres=# ALTER USER new_user CREATEDB;

It's working perfect for now. You may have another issues with database ownership, for this you can change database privilegesand owneras following command.

它现在工作完美。您可能有数据库所有权的另一个问题,为此您可以更改数据库privilegesowner按照以下命令。

postgres=# GRANT ALL PRIVILEGES ON  DATABASE database_name to new_user;
postgres=# ALTER DATABASE database_name owner to new_user;

回答by fivetwentysix

Looking at your schema your credentials for development and test are different.

查看您的架构,您的开发和测试凭据是不同的。

Perhaps remove username and password from the schema, seeing that your test database did get created.

也许从架构中删除用户名和密码,看到您的测试数据库确实已创建。