Rails:Postgres 权限被拒绝在 rake db:create:all 上创建数据库 [英] Rails: Postgres permission denied to create database on rake db:create:all

查看:72
本文介绍了Rails:Postgres 权限被拒绝在 rake db:create:all 上创建数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为开发和测试创建 postgres 数据库.我正在使用:

I am trying to create postgres databases for development and tests. I'm using:

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

宝石文件:

gem 'pg'

database.yml:

database.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 返回

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

怎么了?

编辑我只是尝试将 database.yml 中的用户名更改为我在 Mac 上使用的用户名.有效.它还告诉我,不仅 maybe_test 已经存在,而且还告诉我 myapp_development 也已经存在.

EDIT I 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 that myapp_development already exists too.

  • 为什么它不能使用我创建并为 CREATEDB<​​/code> 分配角色的其他用户名?
  • 为什么说无法创建开发,然后告诉我它已经存在?

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

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 PostgreSQL during development too.

推荐答案

我在 postgresql 中运行 rake db:test:prepare 时遇到了同样的问题 Ruby on Rails 项目.从错误消息中可以清楚地看出,这是用户的权限问题.我从控制台为 new_user 添加了 CREATEDB<​​/code> 权限,如下所示.

I have faced same issues when running rake db:test:prepare in postgresql on my Ruby on Rails project. 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.

访问 postgres 控制台:

To access postgres console:

$ sudo -u postgres -i

postgres@host:~$ psql

在那里:

postgres=# ALTER USER new_user CREATEDB;

它现在工作得很好.您可能还有其他数据库所有权问题,为此您可以按照以下命令更改数据库 privilegesowner.

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.

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

这篇关于Rails:Postgres 权限被拒绝在 rake db:create:all 上创建数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆