【问题标题】:FATAL: role "root" does not exist, rails only on website, rails console works致命:角色“root”不存在,rails 仅在网站上,rails 控制台有效
【发布时间】:2015-08-19 22:05:37
【问题描述】:

所以我继承了这个乱七八糟的 Rails 项目。

我正在将数据库从 mysql 迁移到 postgres。

我现在在尝试打开我的测试网页时遇到此错误

ActiveRecord::NoDatabaseError at /test 致命:角色“root”不存在 运行$ bin/rake db:create db:migrate 来创建你的数据库

整个网站都会出现这个错误。

但是 rails 控制台可以连接到数据库,我可以通过控制台在数据库中创建对象。

这是我的 database.yml

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

development:
  <<: *default
  database: aas_development

test:
  <<: *default
  database: aas_test

production:
  <<: *default
  database: aas_production
  username: aas
  password: <%= ENV['ASS_DATABASE_PASSWORD'] %>

【问题讨论】:

    标签: mysql ruby-on-rails database postgresql


    【解决方案1】:

    创建数据库用户(角色):

    Postgres 使用角色的概念来区分可以连接到数据库的各种用户。当它首次安装在服务器上时,默认的 postgres 用户实际上名为“postgres”。

    要创建自定义用户,首先切换到默认用户:

    sudo su – postgres

    现在在 PostgreSQL 系统中创建新角色(用户):

    createuser

    它会提示你如下:

    Enter name of role to add: newuser

    Shall the new role be a superuser? (y/n) y

    如果你想为新创建的角色(用户)设置密码,那么命令应该是:

    createuser --pwprompt

    你的 database.yml 应该是这样的。

    default: &default
     adapter: postgresql
     pool: 5
     timeout: 5000
     username: your_username
     password: your_password
    
    development:
     <<: *default
     database: db_name_development
    
    test:
     <<: *default
     database: db_name_test
    
    production:
     <<: *default
     database: db_name
    

    【讨论】:

    • 我的 database.yml 中的任何地方都不存在“root”角色,为什么我需要创建用户?除了我的 rails 控制台命令有效之外,我还可以从那里访问数据库。这意味着其他地方正在尝试使用“root”角色访问数据库?没有?
    猜你喜欢
    • 1970-01-01
    • 2018-02-18
    • 2013-03-24
    • 1970-01-01
    • 2015-08-03
    • 1970-01-01
    • 2011-05-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多