首页 > 解决方案 > rails db:create 成功,但随后给出错误

问题描述

我有一个带有 Dockerized PostgreSQL 数据库的 Rails 应用程序。当我运行rails dborrails 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 db工作rails db:migrate正常。当我尝试rails db:createandrails db:drop时,我得到了上面的错误。

有任何想法吗?

标签: ruby-on-railspostgresqldocker

解决方案


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


推荐阅读