2011-11-02 220 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(例如,由于参数无效)。