使用postgresql的Rails不会创建数据库

时间:2022-07-14 20:42:06

I came back to Rails after a long time, and now I am working on a demo project for learning, and wanted to generate the app with Postgresql, it's giving me an error. I generated a new Rails 5 app with postgresql adapter, but when I'm trying to create the database using rails db:create, it's giving me the following error:

很长一段时间后我回到了Rails,现在我正在研究一个用于学习的演示项目,并希望用Postgresql生成应用程序,它给了我一个错误。我使用postgresql适配器生成了一个新的Rails 5应用程序,但是当我尝试使用rails db:create创建数据库时,它给出了以下错误:

FATAL:  Peer authentication failed for user "postgres"
Couldn't create database for {"adapter"=>"postgresql", "encoding"=>"unicode", "pool"=>5, "database"=>"myapp_development", "username"=>"postgres", "password"=>"mypassword"}
rails aborted!
PG::ConnectionBad: FATAL:  Peer authentication failed for user "postgres"
/home/indu/.rvm/gems/ruby-2.5.0/gems/pg-0.21.0/lib/pg.rb:56:in `initialize'
/home/indu/.rvm/gems/ruby-2.5.0/gems/pg-0.21.0/lib/pg.rb:56:in `new'
/home/indu/.rvm/gems/ruby-2.5.0/gems/pg-0.21.0/lib/pg.rb:56:in `connect'
/home/indu/.rvm/gems/ruby-2.5.0/gems/activerecord-5.0.6/lib/active_record/connection_adapters/postgresql_adapter.rb:671:in `connect'
/home/indu/.rvm/gems/ruby-2.5.0/gems/activerecord-5.0.6/lib/active_record/connection_adapters/postgresql_adapter.rb:217:in `initialize'
/home/indu/.rvm/gems/ruby-2.5.0/gems/activerecord-5.0.6/lib/active_record/connection_adapters/postgresql_adapter.rb:37:in `new'
/home/indu/.rvm/gems/ruby-2.5.0/gems/activerecord-5.0.6/lib/active_record/connection_adapters/postgresql_adapter.rb:37:in `postgresql_connection'
/home/indu/.rvm/gems/ruby-2.5.0/gems/activerecord-5.0.6/lib/active_record/connection_adapters/abstract/connection_pool.rb:729:in `new_connection'
/home/indu/.rvm/gems/ruby-2.5.0/gems/activerecord-5.0.6/lib/active_record/connection_adapters/abstract/connection_pool.rb:773:in `checkout_new_connection'
/home/indu/.rvm/gems/ruby-2.5.0/gems/activerecord-5.0.6/lib/active_record/connection_adapters/abstract/connection_pool.rb:752:in `try_to_checkout_new_connection'
/home/indu/.rvm/gems/ruby-2.5.0/gems/activerecord-5.0.6/lib/active_record/connection_adapters/abstract/connection_pool.rb:713:in `acquire_connection'
/home/indu/.rvm/gems/ruby-2.5.0/gems/activerecord-5.0.6/lib/active_record/connection_adapters/abstract/connection_pool.rb:490:in `checkout'
/home/indu/.rvm/gems/ruby-2.5.0/gems/activerecord-5.0.6/lib/active_record/connection_adapters/abstract/connection_pool.rb:364:in `connection'
/home/indu/.rvm/gems/ruby-2.5.0/gems/activerecord-5.0.6/lib/active_record/connection_adapters/abstract/connection_pool.rb:883:in `retrieve_connection'
/home/indu/.rvm/gems/ruby-2.5.0/gems/activerecord-5.0.6/lib/active_record/connection_handling.rb:128:in `retrieve_connection'
/home/indu/.rvm/gems/ruby-2.5.0/gems/activerecord-5.0.6/lib/active_record/connection_handling.rb:91:in `connection'
/home/indu/.rvm/gems/ruby-2.5.0/gems/activerecord-5.0.6/lib/active_record/tasks/postgresql_database_tasks.rb:6:in `connection'
/home/indu/.rvm/gems/ruby-2.5.0/gems/activerecord-5.0.6/lib/active_record/tasks/postgresql_database_tasks.rb:15:in `create'
/home/indu/.rvm/gems/ruby-2.5.0/gems/activerecord-5.0.6/lib/active_record/tasks/database_tasks.rb:109:in `create'
/home/indu/.rvm/gems/ruby-2.5.0/gems/activerecord-5.0.6/lib/active_record/tasks/database_tasks.rb:129:in `block in create_current'
/home/indu/.rvm/gems/ruby-2.5.0/gems/activerecord-5.0.6/lib/active_record/tasks/database_tasks.rb:294:in `block in each_current_configuration'
/home/indu/.rvm/gems/ruby-2.5.0/gems/activerecord-5.0.6/lib/active_record/tasks/database_tasks.rb:293:in `each'
/home/indu/.rvm/gems/ruby-2.5.0/gems/activerecord-5.0.6/lib/active_record/tasks/database_tasks.rb:293:in `each_current_configuration'
/home/indu/.rvm/gems/ruby-2.5.0/gems/activerecord-5.0.6/lib/active_record/tasks/database_tasks.rb:128:in `create_current'
/home/indu/.rvm/gems/ruby-2.5.0/gems/activerecord-5.0.6/lib/active_record/railties/databases.rake:27:in `block (2 levels) in <top (required)>'
/home/indu/.rvm/gems/ruby-2.5.0/gems/railties-5.0.6/lib/rails/commands/rake_proxy.rb:14:in `block in run_rake_task'
/home/indu/.rvm/gems/ruby-2.5.0/gems/railties-5.0.6/lib/rails/commands/rake_proxy.rb:11:in `run_rake_task'
/home/indu/.rvm/gems/ruby-2.5.0/gems/railties-5.0.6/lib/rails/commands/commands_tasks.rb:51:in `run_command!'
/home/indu/.rvm/gems/ruby-2.5.0/gems/railties-5.0.6/lib/rails/commands.rb:18:in `<top (required)>'
/home/indu/Projects/myapp/bin/rails:9:in `require'
/home/indu/Projects/myapp/bin/rails:9:in `<top (required)>'
/home/indu/.rvm/gems/ruby-2.5.0/gems/spring-2.0.2/lib/spring/client/rails.rb:28:in `load'
/home/indu/.rvm/gems/ruby-2.5.0/gems/spring-2.0.2/lib/spring/client/rails.rb:28:in `call'
/home/indu/.rvm/gems/ruby-2.5.0/gems/spring-2.0.2/lib/spring/client/command.rb:7:in `call'
/home/indu/.rvm/gems/ruby-2.5.0/gems/spring-2.0.2/lib/spring/client.rb:30:in `run'
/home/indu/.rvm/gems/ruby-2.5.0/gems/spring-2.0.2/bin/spring:49:in `<top (required)>'
/home/indu/.rvm/gems/ruby-2.5.0/gems/spring-2.0.2/lib/spring/binstub.rb:31:in `load'
/home/indu/.rvm/gems/ruby-2.5.0/gems/spring-2.0.2/lib/spring/binstub.rb:31:in `<top (required)>'
/home/indu/Projects/myapp/bin/spring:15:in `require'
/home/indu/Projects/myapp/bin/spring:15:in `<top (required)>'
bin/rails:3:in `load'
bin/rails:3:in `<main>'
Tasks: TOP => db:create
(See full trace by running task with --trace)

Following is my database.yml file.

以下是我的database.yml文件。

default: &default
  adapter: postgresql
  encoding: unicode
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

development:
  <<: *default
  database: myapp_development
  username: postgres
  password: mypassword

test:
  <<: *default
  database: myapp_test
  username: postgres
  password: mypassword

production:
  <<: *default
  database: myapp_production
  username: myapp
  password: <%= ENV['MYAPP_DATABASE_PASSWORD'] %>

Thank You in advance!

先感谢您!

1 个解决方案

#1


0  

You need to modify your pg_hba.conf file for peer authentication. change your user from peer to md5 and restart your server.

您需要修改pg_hba.conf文件以进行对等身份验证。将您的用户从peer更改为md5并重新启动服务器。

people oftenly face this error with postgres when they setup new projects

人们在设置新项目时经常会遇到与postgres相关的错误

a detailed answer can be found here

详细的答案可以在这里找到

#1


0  

You need to modify your pg_hba.conf file for peer authentication. change your user from peer to md5 and restart your server.

您需要修改pg_hba.conf文件以进行对等身份验证。将您的用户从peer更改为md5并重新启动服务器。

people oftenly face this error with postgres when they setup new projects

人们在设置新项目时经常会遇到与postgres相关的错误

a detailed answer can be found here

详细的答案可以在这里找到