【问题标题】:when I try to do rake db:migrate, I get an error: ActiveRecord::NODatabaseError role "ubuntu" does not exist当我尝试执行 rake db:migrate 时,出现错误:ActiveRecord::NODatabaseError 角色“ubuntu”不存在
【发布时间】:2016-02-05 10:41:12
【问题描述】:

我通过这样做启动了 postgresql 服务器:

sudo service postgresql start

然后我连接到服务:

sudo sudo -u postgres psql

然后我创建了一个数据库(我正在尝试将投票系统添加到我的应用程序):

postgres=# CREATE DATABASE "votes";

但我仍然有同样的问题。

还有,当我这样做时

rake db:create

我得到一个角色“ubuntu”不存在错误

这是我的 database.yml:

# SQLite version 3.x
#   gem install sqlite3
#
#   Ensure the SQLite 3 gem is defined in your Gemfile
#   gem 'sqlite3'
#
# default: &default
#   adapter: sqlite3
#   pool: 5
#   timeout: 5000

# development:
#   <<: *default
#   database: db/development.sqlite3

# # Warning: The database defined as "test" will be erased and
# # re-generated from your development database when you run "rake".
# # Do not set this db to the same as development or production.
# test:
#   <<: *default
#   database: db/test.sqlite3

# production:
#   <<: *default
#   database: db/production.sqlite3

# FOR HEROKU -- POSTGRES DB SETUP
# UNCOMMENT WHEN WORKING LOCALLY.
development:
  adapter: postgresql
  database: votes
  pool: 5
  timeout: 5000
  username: ubuntu

test:
  adapter: postgresql
  database: planit_test
  pool: 5
  timeout: 5000


# production:
#   <<: *default
#   database: db/production.sqlite3

我正在尝试创建一个 ubuntu 角色:

$ sudo sudo -u postgres psql
psql (9.3.10)
Type "help" for help.

postgres=# CREATE ROLE ubuntu SUPERUSER
postgres-# \q
$ rake db:migrate
rake aborted!
ActiveRecord::NoDatabaseError: FATAL:  role "ubuntu" does not exist

【问题讨论】:

  • 你配置你的databases.yml了吗?
  • 检查database.yml 并更改用户名。如果您想与用户ubuntu 连接,那么您必须先在 postgres 中创建它。检查create role 文档。创建它后,请确保您 alter role 具有正确的权限。
  • 是的,我已连接到该服务。然后我创建了用户 ubuntu SUPERUSER。用户名“ubuntu”在我的 database.yml 文件中。仍然遇到同样的问题
  • 在 Postgres 中,用户和角色是不等价的。如果CREATE USER 不够用,您应该重新阅读@radubogdan 的评论并查看CREATE ROLEALTER ROLE

标签: ruby-on-rails ruby postgresql activerecord


【解决方案1】:

请改用 postgres,它是 Postgre 的默认角色。或者试试这个:

    sudo su - postgres
    psql template1

然后

    CREATE ROLE ubuntu superuser createdb login;

您可以使用命令检查角色是否存在:

    \du

【讨论】:

    【解决方案2】:

    您收到有关 role "ubuntu" does not exist 的错误是因为您尝试使用该用户(或 role)从应用程序访问您的 postgress 会话。

    正如你指定的database.yml

    development:
      adapter: postgresql
      database: votes
      pool: 5
      timeout: 5000
      username: ubuntu
    

    注意最后一行:username: ubuntu?

    您可以通过以下两种方式来解决此问题:

    1) 删除该行(也许将其注释掉) - 这将使您的应用程序使用默认设置名称(或 role,您可能想要调用它)连接到您的 postgress 会话。默认的大多数时间是postgress 或您机器上的用户名。

    2) 为您的 postgress 创建角色 ubuntu。为此,您可以check out this answer.

    以上两种方法中的任何一种都应该适合您。

    【讨论】:

    • 我刚刚将用户名:ubuntu 添加到 database.yml,认为这将是一个修复。但是,我已将其删除,但仍然遇到相同的 NoDatabseError 问题。我会试试你的第二个选项
    • 我对我应该做什么感到困惑。听从您的建议,当我尝试创建一个新角色时,看看我的结果(我编辑了上面的问题)。
    • 当你进入psql并列出所有用户\du时,输出是什么?
    • 列出的唯一角色名称是 postgres
    • 尝试使用postgres 作为database.ym 中的用户名。当你这样做时会发生什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-04
    • 1970-01-01
    • 2013-11-28
    • 2012-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多