2016-02-03 20 views
0

Postgres要求我們在初始設置之外做些什麼額外的事情?例如,bash命令在使用Rails構建應用程序時,Postgres要求我們執行內置SQLite 3的操作嗎?

$ rake db:migrate 

在我切換到postgres後不起作用。

到目前爲止,我已經手動輸入 「寶石 'PG'」 中的Gemfile,跑到捆綁安裝PG,交換源碼中的database.yml到PostgreSQL,與創建數據庫:

$ rails generate scaffold Product \ 
      title:string description:text image_url:string price:decimal 

(下面的教程)加在遷移文件的東西,當我跑耙分貝:遷移,我得到:

rake aborted! 
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"? 
/usr/local/lib/ruby/gems/2.3.0/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/postgresql_adapter.rb:651:in `initialize' 
/usr/local/lib/ruby/gems/2.3.0/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/postgresql_adapter.rb:651:in `new' 
/usr/local/lib/ruby/gems/2.3.0/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/postgresql_adapter.rb:651:in `connect' 
/usr/local/lib/ruby/gems/2.3.0/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/postgresql_adapter.rb:242:in `initialize' 
/usr/local/lib/ruby/gems/2.3.0/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/postgresql_adapter.rb:44:in `new' 
/usr/local/lib/ruby/gems/2.3.0/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/postgresql_adapter.rb:44:in `postgresql_connection' 
/usr/local/lib/ruby/gems/2.3.0/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:438:in `new_connection' 
/usr/local/lib/ruby/gems/2.3.0/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:448:in `checkout_new_connection' 
/usr/local/lib/ruby/gems/2.3.0/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:422:in `acquire_connection' 
/usr/local/lib/ruby/gems/2.3.0/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:349:in `block in checkout' 
/usr/local/lib/ruby/gems/2.3.0/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:348:in `checkout' 
/usr/local/lib/ruby/gems/2.3.0/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:263:in `block in connection' 
/usr/local/lib/ruby/gems/2.3.0/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:262:in `connection' 
/usr/local/lib/ruby/gems/2.3.0/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:571:in `retrieve_connection' 
/usr/local/lib/ruby/gems/2.3.0/gems/activerecord-4.2.5.1/lib/active_record/connection_handling.rb:113:in `retrieve_connection' 
/usr/local/lib/ruby/gems/2.3.0/gems/activerecord-4.2.5.1/lib/active_record/connection_handling.rb:87:in `connection' 
/usr/local/lib/ruby/gems/2.3.0/gems/activerecord-4.2.5.1/lib/active_record/migration.rb:916:in `initialize' 
/usr/local/lib/ruby/gems/2.3.0/gems/activerecord-4.2.5.1/lib/active_record/migration.rb:823:in `new' 
/usr/local/lib/ruby/gems/2.3.0/gems/activerecord-4.2.5.1/lib/active_record/migration.rb:823:in `up' 
/usr/local/lib/ruby/gems/2.3.0/gems/activerecord-4.2.5.1/lib/active_record/migration.rb:801:in `migrate' 
/usr/local/lib/ruby/gems/2.3.0/gems/activerecord-4.2.5.1/lib/active_record/tasks/database_tasks.rb:137:in `migrate' 
/usr/local/lib/ruby/gems/2.3.0/gems/activerecord-4.2.5.1/lib/active_record/railties/databases.rake:44:in `block (2 levels) in <top (required)>' 
Tasks: TOP => db:migrate 
(See full trace by running task with --trace) 
+0

我沒有做的是爲postgres添加userid和密碼。可以導致這個錯誤? – iamabot

+0

您應該首先執行'rake db:setup':如果數據庫尚不存在,這將創建數據庫。但爲了能夠做到這一點,確保數據庫正在運行;) – nathanvda

回答

0

請提供更多的信息,你對在Postgres軌道的問題還不是很清楚。爲什麼不耙db:遷移工作?在控制檯輸出中顯示的錯誤是什麼等等。通常,當您使用postgres設置rails時,數據庫導軌應該自動將gem'pg'包含在您的gem文件中。之後,你還應該有一個database.yml文件,其中適配器設置爲postgresql。然後你應該用rake db:create爲這個rails項目創建一個新的數據庫。一旦你這樣做了,你會想爲你的postgres數據庫創建一些資源/模型,從而創建遷移文件。當你有你的遷移文件時,你需要在數據庫中爲你的模型/資源指定哪個表/列,當你運行rake db時:migrate rails會在你的postgres數據庫中創建這些表。

+0

謝謝,我已經更新了我的問題。 – iamabot

相關問題