如何在 MacOS 上重启 PostgreSQL 服务器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7990539/
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-10-20 23:23:47  来源:igfitidea点击:

How to restart PostgreSQL server on MacOS?

macospostgresql

提问by daydreamer

I tried the following and it failed

我尝试了以下方法但失败了

bash-3.2$ pg_ctl restart
pg_ctl: no database directory specified and environment variable PGDATA unset

I am using Macbook Proand dont remember the directory where my data is stored and so can't set PGDATA either, is there a way to restart Postgres at all without losing data?

我正在使用Macbook Pro并且不记得我的数据存储的目录,因此也无法设置 PGDATA,有没有办法在不丢失数据的情况下重新启动 Postgres?

Thank you

谢谢

回答by a_horse_with_no_name

As you want to re-start Postgres I assume the server is already running.

当您想重新启动Postgres 时,我假设服务器已经在运行。

Using the superuser account you can query the location of the data directory through SQL:

使用超级用户帐户可以通过 SQL 查询数据目录的位置:

select name, setting
from pg_settings
where name = 'data_directory'

With that information you can supply the data directory to the pg_ctl command using the -Dswitch (see the manual for details)

有了这些信息,您可以使用-D开关将数据目录提供给 pg_ctl 命令(有关详细信息,请参阅手册)

回答by pjammer

If you used brewyou could find the restart(start|stop) instructions by doing a brew info postgresqlSome issues that happen stem from the fact I've seen the plist not load, on OS startup, for some reason. I always forgot what to do and this helped.

如果您使用过,brew您可以通过执行以下操作找到重新启动(开始|停止)指令brew info postgresql发生的一些问题源于我看到 plist 未加载的事实,在操作系统启动时,出于某种原因。我总是忘记要做什么,这很有帮助。

on my Postgres 9.2 brew recipe it had:

在我的 Postgres 9.2 brew 配方中,它有:

launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist

回答by Jeremy Chone

One liner(with default postgresSql 9.6 install)

一个班轮(默认安装 postgresSql 9.6)

sudo su postgres -c "/Library/PostgreSQL/9.6/bin/pg_ctl restart -D /Library/PostgreSQL/9.6/data"

Full version

完整版本

Get the pgdata folder (gave me /Library/PostgreSQL/9.6/data) (thanks @a_horse_with_no_name)

获取 pgdata 文件夹(给了我/Library/PostgreSQL/9.6/data)(感谢@a_horse_with_no_name)

select name, setting from pg_settings where name = 'data_directory';

For me it returns '/Library/PostgreSQL/9.6/data'

对我来说它返回'/Library/PostgreSQL/9.6/data'

Then

然后

sudo su - postgres
./bin/pg_ctl restart -D /Library/PostgreSQL/9.6/data

Note: postgres' user home dir should be "/Library/PostgreSQL/9.6"

注意:postgres 的用户主目录应该是“/Library/PostgreSQL/9.6”