【问题标题】:Setting up postgresql database with Rails on existing project在现有项目上使用 Rails 设置 postgresql 数据库
【发布时间】:2015-05-07 00:07:07
【问题描述】:

我是 Rails 和 Ruby 的新手,我正在尝试让现有项目在我的环境中运行,我正在使用 Ubuntu 14.04 和 Rubymine、Rails 4.2 和 Ruby 2.2.0

这是迁移文件:

class CreatePeople < ActiveRecord::Migration
  def change

    create_table :people do |t|
      t.string :pid, limit: 20, null: false
      t.string :firstname, limit: 40, null: false
      t.string :lastname, limit: 40, null: false
      t.integer :age
      t.string :gender, limit: 4, null: false
      t.datetime :birthday
      t.string :country, limit: 2
      t.timestamps null: false
    end

    add_index :people, :firstname
    add_index :people, :lastname
    add_index :people, :country
    add_index :people, :pid, unique: true
  end
end

我的数据库.yml:

config=/opt/local/lib/postgresql84/bin/pg_config

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

development:
  <<: *default
  database: vasco_annuaire

  <<: *default
  database: annuaire_test


  <<: *default
  database: annuaire_production
  username: annuaire
  password: <%= ENV['ANNUAIRE_DATABASE_PASSWORD'] %>

运行 rake db:create 给我

“角色ferenan不存在”

启动 localhost:3000 给

ActiveRecord::NoDatabaseError FATAL: 角色“ferenan”不存在

提取的源代码(在 #661 行附近):659 660 661 662 663 664

    rescue ::PG::Error => error
      if error.message.include?("does not exist")
        raise ActiveRecord::NoDatabaseError.new(error.message, error)
      else
        raise
      end

我也无法通过 Pg-Admin-III 连接到 Postgresql

server name : Postgresql
host : localhost
port : 5432
maintenance_db : postgres
username : postgres
no password

使用这些参数我得到消息:

fe_sendauth : 没有提供密码

我已将此行添加到我的 pg_hba.conf 中:

host    sa    all         192.168.0.nnn/32     trust

还有这个到我的 postgresql.conf:

listen_addresses = '*'

然后运行以下命令:

sudo /etc/init.d/postgresql reload

我不知道该做什么以及如何使这些事情协同工作,但正如我的上级之前向我展示的那样,该项目确实有效,不幸的是他目前无法帮助我解决这个问题。任何帮助将不胜感激。

【问题讨论】:

    标签: ruby-on-rails ruby postgresql rake rubymine


    【解决方案1】:

    默认情况下,当未指定用户名时,postgesql 会尝试使用当前用户(在您的情况下为 ferenan)进行连接,但在 vanilla 安装中,不存在此类角色。

    要连接到角色 postgres,请将您的 database.yml 更改为:

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

    关于 Pg-Admin-III,您似乎正在尝试从 localhost 连接,所以我建议您尝试将以下内容添加到 pg_hba.conf(它信任来自具有 md5 授权的 localhost 的请求):

    host    all             all             127.0.0.1/32            trust
    

    【讨论】:

    • 进行这些更改然后运行 ​​rake db:create 给了我:致命:用户“postgres”的对等身份验证失败。仍然无法通过 pg-admin 连接到 Postgresql,它仍然给出:fe_sendauth:没有提供密码
    • @ferenan - 我的答案的第二部分有一些错误(错误的文件和错误的身份验证类型),抱歉,现在试试。
    • 我认为它是 pg_hba.conf 文件,但即使将 md5 更改为 trust 仍然会出现相同的错误:FATAL: Peer authentication failed for user "postgres"
    • @ferenan - 您在 pg_hba.conf 中还有其他行吗?寻找peer授权。
    • 是的,我没有注释这两行:local all postgres peer local all all peer
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-24
    • 2012-09-24
    • 2015-04-01
    • 2014-03-02
    • 2020-08-09
    • 2017-12-31
    • 2013-07-20
    相关资源
    最近更新 更多