2012-11-01 52 views
2

在我的Rails應用程序(v 3.2.8)中,到目前爲止,我已經使用遷移沒有問題。pg_dump:「無法連接到服務器」在Rails「schema_format =:sql」遷移

我使用PostgreSQL 9.2作爲數據庫。我調整了我的application.rb以使用sql而不是模式自卸車(未註釋config.active_record.schema_format = :sql)。

在那之後,我開始對移民這個錯誤:

$ rake db:migrate 

    [ALL MIGRATION STUFF IS PRINTED HERE] 

    pg_dump: [archiver (db)] connection to database "my_dev_db" failed: could not connect to server: Connection refused 
     Is the server running on host "localhost" (::1) and accepting 
     TCP/IP connections on port 5432? 
    could not connect to server: Connection refused 
     Is the server running on host "localhost" (127.0.0.1) and accepting 
     TCP/IP connections on port 5432? 
    could not connect to server: Connection refused 
     Is the server running on host "localhost" (fe80::1) and accepting 
     TCP/IP connections on port 5432? 
    rake aborted! 
    Error dumping database 

I tried manually on the command line(logged in as the same user on my Mac) 

<!-- language: lang-sh --> 

    $ pg_dump my_dev_db > /tmp/db.sql 

No problems with that...happily dumps into `/tmp/db.sql` 

Why is rails having trouble with `pg_dump`? (I am on Mac OSX Lion) 

=========== 

Adding more diagnosis information 

=========== 

    $tail -10 /usr/local/var/postgres9.2/pg_hba.conf 

    local all    all          md5 
    # IPv4 local connections: 
    host all    all    127.0.0.1/32   md5 
    # IPv6 local connections: 
    host all    all    ::1/128     md5 
    # Allow replication connections from localhost, by a user with the 
    # replication privilege. 
    #local replication  rogert        trust 
    #host replication  rogert  127.0.0.1/32   trust 
    #host replication  rogert  ::1/128     trust 


    $ sudo lsof -p 62444 | awk '$5 == "unix" && $NF ~ /\// { print $NF }' 
    /tmp/.s.PGSQL.5432 

    $ ps auxw | grep post 
    postgres  1403 0.0 0.0 2435492 640 s007 S+ 21Oct12 0:00.05 bash 
    root   1401 0.0 0.0 2498096 128 s007 S 21Oct12 0:00.02 su postgres 
    rogert 62517 0.0 0.0 2426700 388 s001 R+ 9:21PM 0:00.00 grep post 
    rogert 62448 0.0 0.0 2481656 500 ?? Ss 8:46PM 0:00.03 postgres: wal writer process  
    rogert 62447 0.0 0.0 2481656 752 ?? Ss 8:46PM 0:00.07 postgres: writer process  
    rogert 62446 0.0 0.0 2481656 1040 ?? Ss 8:46PM 0:00.00 postgres: checkpointer process  
    rogert 62444 0.0 0.1 2481656 5368 s001 S  8:46PM 0:00.02 /usr/local/Cellar/postgresql/9.2.1/bin/postgres -D /usr/local/var/postgres9.2 


    $ rake db:migrate --trace 

    [ALL MIGRATION STUFF IS PRINTED HERE] 

    /Users/rogert/.rvm/gems/[email protected]_3.2.8/gems/activerecord-3.2.8/lib/active_record/railties/databases.rake:393:in `block (3 levels) in <top (required)>' 
    /Users/rogert/.rvm/gems/[email protected]_3.2.8/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `call' 
    /Users/rogert/.rvm/gems/[email protected]_3.2.8/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `block in execute' 
    /Users/rogert/.rvm/gems/[email protected]_3.2.8/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `each' 
    /Users/rogert/.rvm/gems/[email protected]_3.2.8/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `execute' 
    /Users/rogert/.rvm/gems/[email protected]_3.2.8/gems/rake-0.9.2.2/lib/rake/task.rb:158:in `block in invoke_with_call_chain' 
    /Users/rogert/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize' 
    /Users/rogert/.rvm/gems/[email protected]_3.2.8/gems/rake-0.9.2.2/lib/rake/task.rb:151:in `invoke_with_call_chain' 
    /Users/rogert/.rvm/gems/[email protected]_3.2.8/gems/rake-0.9.2.2/lib/rake/task.rb:144:in `invoke' 
    /Users/rogert/.rvm/gems/[email protected]_3.2.8/gems/activerecord-3.2.8/lib/active_record/railties/databases.rake:162:in `block (2 levels) in <top (required)>' 
    /Users/rogert/.rvm/gems/[email protected]_3.2.8/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `call' 
    /Users/rogert/.rvm/gems/[email protected]_3.2.8/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `block in execute' 
    /Users/rogert/.rvm/gems/[email protected]_3.2.8/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `each' 
    /Users/rogert/.rvm/gems/[email protected]_3.2.8/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `execute' 
    /Users/rogert/.rvm/gems/[email protected]_3.2.8/gems/rake-0.9.2.2/lib/rake/task.rb:158:in `block in invoke_with_call_chain' 
    /Users/rogert/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize' 

$sudo vi /usr/local/var/postgres9.2/postgresql.conf 

[search for listen address] 

# - Connection Settings - 

listen_addresses = 'localhost'   # what IP address(es) to listen on; 
             # comma-separated list of addresses; 
             # defaults to 'localhost'; use '*' for all 
             # (change requires restart) 
port = 5432        # (change requires restart) 
max_connections = 100     # (change requires restart) 

請告訴我奇怪的是,如果我完全從我的Rails應用程序中刪除這兩條線路,遷移工作。所以,如果它與TCP連接和listenting的一個問題,是如何工作的移民本身(但不會當我把這兩再次打開)

  1. application.rb中 - config.active_record.schema_format =: SQL
  2. 在我的移民文件中的一個 - t.hstore:屬性
+0

你沒有測試同樣的事情;正如@Tom所指出的那樣,'pg_dump'默認使用unix域套接字,而不是TCP/IP。嘗試運行'pg_dump -h 127.0.0.1 -p 5432 my_dev_Db>/tmp/db.sql'。它運行正常嗎? –

+1

在Mac OS X上,使用'sudo lsof -i -P | grep -i「listen」'列出偵聽的TCP/IP端口。在你的情況下,你對'sudo lsof -i -P |感興趣grep -i「listen」| grep postgres或者'sudo lsof -i -P | grep -i「listen」| grep 5432'。 –

回答

5

您的PostgreSQL實例最有可能配置爲不偵聽TCP/IP,至少在本地主機上。

postgresql.conf檢查listen_addresses。見the documentation。它最有可能設置爲''(空字符串),因此服務器只能在UNIX域套接字上偵聽。

psql,pg_dump等將仍然工作,如果Pg沒有監聽TCP/IP,因爲他們默認連接到本地unix域套接字。 Ruby pg gem是一個圍繞libpq的包裝,與psql等使用的客戶端庫相同,它也默認使用unix域套接字,除非明確指定連接參數。

但是,Rails似乎將一個明確的IP地址傳遞給pg_dump--導致它嘗試通過TCP/IP進行連接,看起來Pg沒有在監聽,導致觀察到「連接被拒絕」錯誤。

或者,可能PostgreSQL被編譯爲默認的端口而不是5432.相同的設置被編譯爲默認的libpq,所以它會自動連接到新的端口。但是,如果Rails在嘗試運行時指定一個明確的端口pg_dump,它將優先使用內置的默認端口。檢查postgresql.conf中的port指令;如果是不是註釋掉或設置爲5432這可能是你的問題。port指令記錄在與上面鏈接的相同頁面中。

BTW,到PG時運行定位postgresql.conf運行psql template1 -c "SHOW config_file;"

+0

$ psql template1 -c「SHOW config_file;」 CONFIG_FILE -------------------------------------------- /USR /local/var/postgres9.2/postgresql.conf (1 row) – sat

+0

此外,運行lsof監聽端口,顯示我postgres正在監聽5432. [[postgres 62706 rogert 5u IPv6 0xffffff8013361800 0t0 TCP localhost:5432(LISTEN) postgres 62706 rogert 6u IPv4 0xffffff801bd08880 0t0 TCP localhost:5432(LISTEN) postgres 62706 rogert 7u IPv6 0xffffff8013361bc0 0t0 TCP localhost:5432(LISTEN)]] – sat

+0

@BVSat OK ...這是您需要查看的文件。我沒有發佈它,因爲*我*需要的信息,但因爲我認爲*你*可能;我經常看到人們不知道如何找到'postgresql.conf'的問題。 –

0

pg_dump在命令行中會使用UNIX域套接字,而軌道工具試圖建立一個TCP連接到本地主機要訪問的PostgreSQL 。

看看你的pg_hba.conf(對我來說這是在/ var/lib/pgsql/data /中)並檢查是否有這樣一行,其中md5表示將使用密碼認證:

host all   all   127.0.0.1/32   md5 

(請確保您完全理解這一點,如果你要在生產環境中運行它!)

您可以測試運行pg_dump的時候在命令行上明確指定-h localhost通過TCP連接。如果對pg_hba.conf進行了任何更改,請記住重新啓動PostgreSQL。

+0

似乎沒有工作。奇怪。首先,我不知道rails如何連接以及如何連接pg_dump。如果我用-h localhost選項使用pg_dump,則會得到相同的錯誤。這樣就完全排除了軌道。但我的問題仍然存在。到目前爲止,這是我所看到的(見我原來的職位額外dignosis) – sat

+0

如果它是一個'pg_hba.conf'問題,該錯誤會從PG服務器的報告,有沒有'的pg_hba.conf'進入用戶/數據庫/主機組合。這是一個「拒絕連接」,所以問題是服務器沒有收聽。第一個和最後一個參數是正確的,但是'pg_hba.conf'建議在這裏不適用。 –

+0

增加了更多的診斷信息 - 仍然是相同的錯誤。我更改後重新啓動服務器 – sat

1

的根本原因竟然是該以爲本地迴環接口過濾連接是一個好主意第三方防火牆軟件包。

the comments thread