简体   繁体   English

Rails:Postgres 权限被拒绝在 rake db:create:all 上创建数据库

[英]Rails: Postgres permission denied to create database on rake db:create:all

I am trying to create postgres databases for development and tests.我正在尝试为开发和测试创建 postgres 数据库。 I'm using:我正在使用:

  • OSX Yosemite OSX优胜美地
  • Rails version: 4.2.0导轨版本:4.2.0
  • git version: 2.2.2 git 版本:2.2.2
  • psql version: 9.4.0 psql 版本:9.4.0
  • ruby version: 2.1.0p0红宝石版本:2.1.0p0
  • HomeBrew version: 0.9.5 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:all returns 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?怎么了?

EDIT I just tried changing the username in the database.yml to my username that I'm using on my Mac.编辑我只是尝试将 database.yml 中的用户名更改为我在 Mac 上使用的用户名。 It worked.有效。 It also told me that not only maybe_test already exists, but it also just told me that myapp_development already exists too.它还告诉我不仅maybe_test已经存在,而且还告诉我myapp_development已经存在。

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

This all seems way too confusing and reminds me of PHP setup with Apache back in the very old days.这一切似乎太混乱了,让我想起了过去使用 Apache 进行 PHP 设置。 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 PostgreSQL during development too.我不想每次创建新应用程序时都必须处理问题,并尝试遵循 Heroku 建议在开发过程中使用 PostgreSQL。

I have faced same issues when running rake db:test:prepare in postgresql on my Ruby on Rails project.在我的Ruby on Rails项目postgresql运行rake db:test:prepare时,我遇到了同样的问题。 This is pretty clear from the error message, that its a permission issue for the user.从错误消息中可以清楚地看出,这是用户的权限问题。 I added CREATEDB permission for new_user as following from the console.我从控制台为new_user添加了CREATEDB权限,如下所示。

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 privileges and owner as following command.您可能有数据库所有权的另一个问题,为此您可以按照以下命令更改数据库privilegesowner

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

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.也许从架构中删除用户名和密码,看到您的测试数据库确实已创建。

create database demo;
create user demotest with password '123';
grant all privileges on database demo to demotest;
commit;

      This is script for creation of database. But any existing database having password '123' then change your password for new database to password '1234'. This procedure working for me.
 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM