2011-11-02 87 views
64

如何判斷我的Postgresql服務器是否正在運行?如何檢查PostgreSQL服務器的狀態Mac OS X

我得到這個消息:

[~/dev/working/sw] sudo bundle exec rake db:migrate 
rake aborted! 
could not connect to server: Connection refused 
    Is the server running on host "localhost" and accepting 
    TCP/IP connections on port 5432? 

更新:

> which postgres 
/usr/local/bin/postgres 
> pg_ctl -D /usr/local/bin/postgres -l /usr/local/bin/postgres/server.log start 
pg_ctl: could not open PID file "/usr/local/bin/postgres/postmaster.pid": Not a directory 

更新2:

>pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start 
server starting 
sh: /usr/local/var/postgres/server.log: No such file or directory 
+1

您還應該在Mac OS/X上尋找多個版本的postgres。如果你已經通過homebrew安裝了postgres,那麼當你的路徑設置不正確時,你可以得到上述錯誤 - 我只是意外地欺騙了我的路徑,一切開始使用postgres的系統安裝,直到我調整路徑使用brew安裝 –

回答

62

來檢查正在運行的進程的最簡單方法:

ps auxwww | grep postgres 

,並尋找看起來像這樣的命令(您的版本可能不是8.3):

/Library/PostgreSQL/8.3/bin/postgres -D /Library/PostgreSQL/8.3/data 

要啓動服務器,執行這樣的事情:

/Library/PostgreSQL/8.3/bin/pg_ctl start -D /Library/PostgreSQL/8.3/data -l postgres.log 
+0

好吧,如果我沒有找到類似的東西,我該如何啓動服務器? – Ramy

+0

@雷米編輯回答 – Bohemian

+1

'pgrep postgres'也可以。或者'ps auxwww | grep [p] ostgres'來防止'grep'被拾取。 –

8

這取決於在哪裏你的postgresql服務器已安裝。您可以使用pg_ctl手動啓動服務器,如下所示。

pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start 
+0

看到我的問題編輯,請 – Ramy

+0

@Ramy您在路徑中用* var *代替* bin *。 – jamesallman

+0

嗯......好的。我想我需要檢查我的安裝位置。請參閱新的更新。 – Ramy

13

你可能沒有初始化postgres。

如果您使用HomeBrew進行安裝,則必須先運行init才能使其他設備可用。

要查看說明書,運行brew info postgres

# Create/Upgrade a Database 
If this is your first install, create a database with: 
    initdb /usr/local/var/postgres -E utf8 

To have launchd start postgresql at login: 
    ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents 
Then to load postgresql now:  
    launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist 
Or, if you don't want/need launchctl, you can just run: 
    pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start 

一旦運行,它應該這樣說:

成功。現在,您可以使用啓動數據庫服務器:

postgres -D /usr/local/var/postgres or 
pg_ctl -D /usr/local/var/postgres -l logfile start 

如果您仍然有問題,請檢查你的防火牆。如果你使用一個像HandsOff一樣的好東西!並且配置爲阻止流量,那麼您的頁面將不會看到數據庫。

31

您可以運行下面的命令,以確定是否postgress運行:

$ pg_ctl status 

您還需要設置PGDATA環境變量。

下面是我在我的~/.bashrc文件Postgres的:

export PGDATA='/usr/local/var/postgres' 
export PGHOST=localhost 
alias start-pg='pg_ctl -l $PGDATA/server.log start' 
alias stop-pg='pg_ctl stop -m fast' 
alias show-pg-status='pg_ctl status' 
alias restart-pg='pg_ctl reload' 

要讓它們生效,記得源像這樣:

$ . ~/.bashrc 

現在,試試吧,你應該得到這樣的東西:

$ show-pg-status 
pg_ctl: server is running (PID: 11030) 
/usr/local/Cellar/postgresql/9.2.4/bin/postgres 
+0

鑑於您的錯誤消息,我敢打賭SamGoody的建議是運行您的initdb命令將修復您的「連接被拒絕」問題。修復後,試試我的建議以獲取您的postgres數據庫服務器狀態。 – l3x

+0

哇....我一直在重建postgres多年。這是完美的解決方案! – mindtonic

4

pg_ctl status命令建議在其他答案中檢查postmaster流程是否存在,如果是,則報告它正在運行。這並不一定意味着它已準備好接受連接或執行查詢。

最好使用另一種方法,如使用psql來運行簡單的查詢並檢查退出代碼,例如, psql -c 'SELECT 1',或使用pg_isreadyto check the connection status

+2

'psql -c「SELECT 1」-d {dbname}>/dev/null || postgres -D/usr/local/var/postgres> postgres.log 2>&1&'如果你想一次檢查並啓動postgres(對於自動化腳本很方便)。 – Kate

6

從PostgreSQL 9.3開始,您可以使用命令pg_isready來確定PostgreSQL服務器的連接狀態。

docs

pg_isready返回0到外殼如果服務器通常接受連接,1如果服務器(在啓動過程例如)拒絕連接,2,如果有到無響應連接嘗試,如果沒有嘗試,則爲3(例如,由於參數無效)。