ruby-on-rails - 使用 Ruby on Rails 的 PostgreSQL 用户

标签 ruby-on-rails ruby postgresql heroku

我正在尝试在 heroku 上部署我的应用程序。为此,我从 sqlite3 更改为 pg,因为 heroku 不支持 sqlite3。我更改了 database.yml 中的内容:

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

development:
  <<: *default
  database: db/development.pg

# 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.pg

production:
  <<: *default
  database: db/my_database_production.pg

我也添加并安装了 gem (gem install pg)。当我尝试运行 bundler exec rake db:create 时,出现以下错误:

rake aborted!
ActiveRecord::NoDatabaseError: FATAL:  role "flo" does not exist
Run `$ bin/rake db:create db:migrate` to create your database
/home/flo/Ruby/sample_app/vendor/bundle/ruby/1.9.1/gems/activerecord-4.1.6/lib/active_record/connection_adapters/postgresql_adapter.rb:898:in `rescue in connect'
/home/flo/Ruby/sample_app/vendor/bundle/ruby/1.9.1/gems/activerecord-4.1.6/lib/active_record/connection_adapters/postgresql_adapter.rb:888:in `connect'
/home/flo/Ruby/sample_app/vendor/bundle/ruby/1.9.1/gems/activerecord-4.1.6/lib/active_record/connection_adapters/postgresql_adapter.rb:568:in `initialize'
/home/flo/Ruby/sample_app/vendor/bundle/ruby/1.9.1/gems/activerecord-4.1.6/lib/active_record/connection_adapters/postgresql_adapter.rb:41:in `new'
/home/flo/Ruby/sample_app/vendor/bundle/ruby/1.9.1/gems/activerecord-4.1.6/lib/active_record/connection_adapters/postgresql_adapter.rb:41:in `postgresql_connection'
/home/flo/Ruby/sample_app/vendor/bundle/ruby/1.9.1/gems/activerecord-4.1.6/lib/active_record/connection_adapters/abstract/connection_pool.rb:435:in `new_connection'
/home/flo/Ruby/sample_app/vendor/bundle/ruby/1.9.1/gems/activerecord-4.1.6/lib/active_record/connection_adapters/abstract/connection_pool.rb:445:in `checkout_new_connection'
/home/flo/Ruby/sample_app/vendor/bundle/ruby/1.9.1/gems/activerecord-4.1.6/lib/active_record/connection_adapters/abstract/connection_pool.rb:416:in `acquire_connection'
/home/flo/Ruby/sample_app/vendor/bundle/ruby/1.9.1/gems/activerecord-4.1.6/lib/active_record/connection_adapters/abstract/connection_pool.rb:351:in `block in checkout'
/home/flo/Ruby/sample_app/vendor/bundle/ruby/1.9.1/gems/activerecord-4.1.6/lib/active_record/connection_adapters/abstract/connection_pool.rb:350:in `checkout'
/home/flo/Ruby/sample_app/vendor/bundle/ruby/1.9.1/gems/activerecord-4.1.6/lib/active_record/connection_adapters/abstract/connection_pool.rb:265:in `block in connection'
/home/flo/Ruby/sample_app/vendor/bundle/ruby/1.9.1/gems/activerecord-4.1.6/lib/active_record/connection_adapters/abstract/connection_pool.rb:264:in `connection'
/home/flo/Ruby/sample_app/vendor/bundle/ruby/1.9.1/gems/activerecord-4.1.6/lib/active_record/connection_adapters/abstract/connection_pool.rb:541:in `retrieve_connection'
/home/flo/Ruby/sample_app/vendor/bundle/ruby/1.9.1/gems/activerecord-4.1.6/lib/active_record/connection_handling.rb:113:in `retrieve_connection'
/home/flo/Ruby/sample_app/vendor/bundle/ruby/1.9.1/gems/activerecord-4.1.6/lib/active_record/connection_handling.rb:87:in `connection'
/home/flo/Ruby/sample_app/vendor/bundle/ruby/1.9.1/gems/activerecord-4.1.6/lib/active_record/migration.rb:909:in `initialize'
/home/flo/Ruby/sample_app/vendor/bundle/ruby/1.9.1/gems/activerecord-4.1.6/lib/active_record/migration.rb:807:in `new'
/home/flo/Ruby/sample_app/vendor/bundle/ruby/1.9.1/gems/activerecord-4.1.6/lib/active_record/migration.rb:807:in `up'
/home/flo/Ruby/sample_app/vendor/bundle/ruby/1.9.1/gems/activerecord-4.1.6/lib/active_record/migration.rb:785:in `migrate'
/home/flo/Ruby/sample_app/vendor/bundle/ruby/1.9.1/gems/activerecord-4.1.6/lib/active_record/railties/databases.rake:34:in `block (2 levels) in <top (required)>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)

我正在尝试在我的终端中编写 su - postgres 但从该命令我得到:

No passwd entry for user 'postgres'

我该怎么办?

最佳答案

您需要将用户“flo”添加到您的本地 postgres 实例(我通常只是将他们添加为 super 用户,所以我没有权限问题)。在您的终端中运行:

createuser -P -s -e flo

系统还会提示您设置密码。现在让我们使用“flo”。然后,您需要将其添加到您的 databases.yml 文件中:

development:
  <<: *default
  database: db/development.pg
  username: flo
  password: flo

执行此操作,然后再次尝试运行 rake db:create。检查this out for additional help .

关于ruby-on-rails - 使用 Ruby on Rails 的 PostgreSQL 用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27276932/

相关文章:

ruby-on-rails - Rails 和 Selenium : how to stop/pause execution of a test in the browser?

ruby-on-rails - 如何在 Rails 中设置模型继承?我有一个类和两个子类

c - undefined reference ...函数返回指针

postgresql - 在 hibernate 中获取具有时区值的 itimestamp

ruby-on-rails - rails : must appear in the GROUP BY clause or be used in an aggregate function

ruby-on-rails - unicorn 服务时,Rails 为预编译 Assets 生成错误的指纹

javascript - Rails 中 JSON/Javascript 解析的正确过程?

ruby - 带有 header 的 Sinatra 流式响应

ruby-on-rails - 如何使用 Rails Faker gem 生成一致的数据?

PostgreSQL - 批处理+脚本+变量