如何检查 PostgreSQL 服务器的状态 Mac OS X

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7975414/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 23:10:19  来源:igfitidea点击:

How to check status of PostgreSQL server Mac OS X

macospostgresql

提问by Ramy

How can I tell if my Postgresql server is running or not?

如何判断我的 Postgresql 服务器是否正在运行?

I'm getting this message:

我收到这条消息:

[~/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?

Update:

更新:

> 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

Update 2:

更新 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

采纳答案by Bohemian

The simplest way to to check running processes:

检查正在运行的进程的最简单方法:

ps auxwww | grep postgres

And look for a command that looks something like this (your version may not be 8.3):

并寻找类似这样的命令(您的版本可能不是 8.3):

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

To start the server, execute something like this:

要启动服务器,请执行如下操作:

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

回答by l3x

You can run the following command to determine if postgress is running:

您可以运行以下命令来确定 postgress 是否正在运行:

$ pg_ctl status

You'll also want to set the PGDATAenvironment variable.

您还需要设置PGDATA环境变量。

Here's what I have in my ~/.bashrcfile for postgres:

这是我在~/.bashrcpostgres 文件中的内容:

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'

To get them to take effect, remember to source it like so:

要使它们生效,请记住像这样获取它:

$ . ~/.bashrc

Now, try it and you should get something like this:

现在,尝试一下,你应该得到这样的结果:

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

回答by SamGoody

You probably did not init postgres.

您可能没有初始化 postgres。

If you installed using HomeBrew, the init must be run before anything else becomes usable.

如果您使用 HomeBrew 安装,则必须先运行 init,然后才能使用其他任何东西。

To see the instructions, run brew info postgres

要查看说明,请运行 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

Once you have run that, it should say something like:

一旦你运行了它,它应该是这样的:

Success. You can now start the database server using:

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

成功。您现在可以使用以下命令启动数据库服务器:

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

If you are still having issues, check your firewall. If you use a good one like HandsOff! and it was configured to block traffic, then your page will not see the database.

如果您仍然遇到问题,请检查您的防火墙。如果您使用像 HandsOff 这样的好工具!并且它被配置为阻止流量,那么您的页面将看不到数据库。

回答by benjwadams

As of PostgreSQL 9.3, you can use the command pg_isreadyto determine the connection status of a PostgreSQL server.

从 PostgreSQL 9.3 开始,您可以使用该命令pg_isready来确定 PostgreSQL 服务器的连接状态。

From the docs:

文档

pg_isready returns 0 to the shell if the server is accepting connections normally, 1 if the server is rejecting connections (for example during startup), 2 if there was no response to the connection attempt, and 3 if no attempt was made (for example due to invalid parameters).

如果服务器正常接受连接,则 pg_isready 向 shell 返回 0,如果服务器拒绝连接(例如在启动期间),则返回 1,如果对连接尝试没有响应,则返回 2,如果没有尝试进行连接,则返回 3(例如由于无效参数)。

回答by Nvick

It depends on where your postgresql server is installed. You use the pg_ctl to manually start the server like below.

这取决于您的 postgresql 服务器的安装位置。您使用 pg_ctl 手动启动服务器,如下所示。

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

回答by Owen Pauling

The pg_ctl statuscommand suggested in other answers checks that the postmaster process exists and if so reports that it's running. That doesn't necessarily mean it is ready to accept connections or execute queries.

pg_ctl status其他答案中建议的命令检查 postmaster 进程是否存在,如果存在则报告它正在运行。这并不一定意味着它已准备好接受连接或执行查询。

It is better to use another method like using psqlto run a simple query and checking the exit code, e.g. psql -c 'SELECT 1', or use pg_isreadyto check the connection status.

最好使用另一种方法,例如psql用于运行简单查询并检查退出代码,例如psql -c 'SELECT 1',或用于pg_isready检查连接状态