【问题标题】:rails db:create succeeds, but then gives errorrails db:create 成功,但随后出现错误
【发布时间】:2021-05-02 19:25:46
【问题描述】:

我有一个带有 Dockerized PostgreSQL 数据库的 Rails 应用程序。当我运行rails dbrails db:migrate 时,它工作得非常好。

当我运行rails db:create 时,它的行为有所不同:

$ rails db:create                                                                                                     
Created database 'my_app_development'                                                                                                        
could not connect to server: No such file or directory                                                                                       
        Is the server running locally and accepting                                                                                          
        connections on Unix domain socket "/tmp/.s.PGSQL.5432"?                                                                              
Couldn't create 'my_app_test' database. Please check your configuration.                                                  
rails aborted!                                                                                                                               
ActiveRecord::ConnectionNotEstablished: could not connect to server: No such file or directory                                               
        Is the server running locally and accepting                                                                                          
        connections on Unix domain socket "/tmp/.s.PGSQL.5432"?                                                                              
/Users/jasonswett/Documents/code/rails_application_template/my_app/bin/rails:5:in `<top (required)>'   
/Users/jasonswett/Documents/code/rails_application_template/my_app/bin/spring:10:in `block in <top (required)>'                              
/Users/jasonswett/Documents/code/rails_application_template/my_app/bin/spring:7:in `tap'                                                     
/Users/jasonswett/Documents/code/rails_application_template/my_app/bin/spring:7:in `<top (required)>'                                        
                                                                                                                                             
Caused by:                                                                                                                                   
PG::ConnectionBad: could not connect to server: No such file or directory                                                                    
        Is the server running locally and accepting                                                                                          
        connections on Unix domain socket "/tmp/.s.PGSQL.5432"?                                                                              
/Users/jasonswett/Documents/code/rails_application_template/my_app/bin/rails:5:in `<top (required)>'                                         
/Users/jasonswett/Documents/code/rails_application_template/my_app/bin/spring:10:in `block in <top (required)>'                              
/Users/jasonswett/Documents/code/rails_application_template/my_app/bin/spring:7:in `tap'                         
/Users/jasonswett/Documents/code/rails_application_template/my_app/bin/spring:7:in `<top (required)>'                     
Tasks: TOP => db:create                                                                                                                      
(See full trace by running task with --trace)

我的怀疑是,由于某种原因,rails db:create 试图连接到我在127.0.0.1:5432 上的数据库(这可行),但随后又试图在localhost:5432 上再次连接到它,这将无法正常工作,因为localhost 赢了不要去我的容器,只有127.0.0.1:5432会。

同样,rails dbrails db:migrate 工作正常。当我尝试rails db:createrails db:drop 时,我得到了上面的错误。

有什么想法吗?

【问题讨论】:

    标签: ruby-on-rails postgresql docker


    【解决方案1】:

    想通了。只是我的测试数据库设置错误(设置为localhost而不是127.0.0.1)。

    【讨论】:

      猜你喜欢
      • 2013-02-13
      • 1970-01-01
      • 2019-02-05
      • 1970-01-01
      • 1970-01-01
      • 2011-12-07
      • 1970-01-01
      • 2012-10-13
      • 1970-01-01
      相关资源
      最近更新 更多