如何在 Mac OS X 上启动 PostgreSQL 服务器?

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

How to start PostgreSQL server on Mac OS X?

macospostgresqlhomebrew

提问by Ramy

FINAL UPDATE:

最终更新:

I had forgotten to run the initdbcommand.

我忘记运行initdb命令了。

< /FINAL UPDATE>

< /最终更新>

by running this command

通过运行这个命令

ps auxwww | grep postgres

I see that postgres is not running

我看到 postgres 没有运行

> ps auxwww | grep postgres
remcat          1789   0.0  0.0  2434892    480 s000  R+   11:28PM   0:00.00 grep postgres

this raises the question: How do I start the postgresql server?

这提出了一个问题:如何启动 postgresql 服务器?

update:

更新:

>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

update 2:

更新2:

The touch was not successful so I did this instead:

触摸不成功,所以我这样做了:

> mkdir /usr/local/var/postgres
> vi /usr/local/var/postgres/server.log
> ls /usr/local/var/postgres/          
server.log

But when I try to start rails server, I still see this:

但是当我尝试启动 rails server 时,我仍然看到这个:

Is the server running on host "localhost" and accepting
TCP/IP connections on port 5432?

update 3:

更新3:

> pg_ctl -D /usr/local/var/postgres status
pg_ctl: no server running

update 4:

更新 4:

I found that there WAS NO pg_hba.conf (only pg_hba.conf.sample) so I modified the sample and renamed it (to remover the .sample). Here are the contents:

我发现没有 pg_hba.conf (只有 pg_hba.conf.sample),所以我修改了示例并将其重命名(以去除 .sample)。以下是内容:

 # IPv4 local connections:
 host    all             all             127.0.0.1/32           trust
 # IPv6 local connections:
 host    all             all             ::1/128                trust

but I don't understand this:

但我不明白这一点:

> pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start     
server starting
> pg_ctl -D /usr/local/var/postgres status                                     
pg_ctl: no server running

also:

还:

sudo find / -name postgresql.conf
find: /dev/fd/3: Not a directory
find: /dev/fd/4: Not a directory

update 5:

更新 5:

sudo pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
Password:
pg_ctl: cannot be run as root
Please log in (using, e.g., "su") as the (unprivileged) user that will own the server process.

update 6:

更新 6:

this seems odd:

这似乎很奇怪:

> egrep 'listen|port' /usr/local/var/postgres/postgresql.conf
egrep: /usr/local/var/postgres/postgresql.conf: No such file or directory

though, I did do this:

不过,我确实这样做了:

>sudo find / -name "*postgresql.conf*"
find: /dev/fd/3: Not a directory
find: /dev/fd/4: Not a directory
/usr/local/Cellar/postgresql/9.0.4/share/postgresql/postgresql.conf.sample
/usr/share/postgresql/postgresql.conf.sample

so I did this:

所以我这样做了:

egrep 'listen|port' /usr/local/Cellar/postgresql/9.0.4/share/postgresql/postgresql.conf.sample 
#listen_addresses = 'localhost'     # what IP address(es) to listen on;
#port = 5432                # (change requires restart)
                # supported by the operating system:
                #   %r = remote host and port

so I tried this:

所以我试过这个:

> cp /usr/local/Cellar/postgresql/9.0.4/share/postgresql/postgresql.conf.sample /usr/local/Cellar/postgresql/9.0.4/share/postgresql/postgresql.conf        
> cp /usr/share/postgresql/postgresql.conf.sample /usr/share/postgresql/postgresql.conf 

still getting the same "Is the server running?" message.

仍然得到相同的“服务器正在运行吗?” 信息。

回答by James Allman

The Homebrewpackage manager includes launchctl plists to start automatically. For more information run brew info postgres.

自制包管理器包括launchctl的Plist自动启动。有关更多信息,请运行brew info postgres

Start manually:

手动启动:

pg_ctl -D /usr/local/var/postgres start

pg_ctl -D /usr/local/var/postgres start

Stop manually:

手动停止:

pg_ctl -D /usr/local/var/postgres stop

pg_ctl -D /usr/local/var/postgres stop

Start automatically:

自动启动:

"To have launchd start postgresql now and restart at login:"

“现在启动 postgresql 并在登录时重新启动:”

brew services start postgresql

brew services start postgresql



What is the result of pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start?

结果是pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start什么?

What is the result of pg_ctl -D /usr/local/var/postgres status?

结果是pg_ctl -D /usr/local/var/postgres status什么?

Are there any error messages in the server.log?

server.log 中是否有任何错误消息?

Make sure tcp localhost connections are enabled in pg_hba.conf:

确保在 pg_hba.conf 中启用了 tcp localhost 连接:

# IPv4 local connections:
host    all             all             127.0.0.1/32            trust

Check the listen_addresses and port in postgresql.conf:

检查 postgresql.conf 中的 listen_addresses 和端口:

egrep 'listen|port' /usr/local/var/postgres/postgresql.conf

egrep 'listen|port' /usr/local/var/postgres/postgresql.conf

#listen_addresses = 'localhost'     # what IP address(es) to listen on;
#port = 5432                # (change requires restart)


Cleaning up

打扫干净

Postgres was most likely installed via Homebrew, Fink, MacPortsor the EnterpriseDBinstaller.

Postgres 很可能是通过HomebrewFinkMacPortsEnterpriseDB安装程序安装的。

Check the output of the following commands to determine which package manager it was installed with:

检查以下命令的输出以确定它是使用哪个包管理器安装的:

brew && brew list|grep postgres
fink && fink list|grep postgres
port && port installed|grep postgres

回答by malopezcruz

If you want to manually start and stop postgresql (installed via homebrew), the easiest way is:

如果要手动启动和停止 postgresql(通过自制软件安装),最简单的方法是:

brew services start postgresql

and

brew services stop postgresql

If you have specific version, make sure to suffix the version, for example:

如果您有特定版本,请确保为版本添加后缀,例如:

brew services start postgresql@10

回答by Yan

I had almost the exact same issue, and you cited the initdb command as being the fix. This was also the solution for me, but I didn't see that anyone posted it here, so for those who are looking for it:

我遇到了几乎完全相同的问题,并且您引用了 initdb 命令作为解决方法。这也是我的解决方案,但我没有看到有人在这里发布它,所以对于那些正在寻找它的人:

initdb /usr/local/var/postgres -E utf8

回答by rafaelportela

If your computer was abruptly restarted

如果您的计算机突然重新启动

First, you have to delete the file /usr/local/var/postgres/postmaster.pidThen you can restart the service using one of the many other mentioned methods depending on your install.

首先,您必须删除文件/usr/local/var/postgres/postmaster.pid然后您可以根据您的安装使用许多其他提到的方法之一重新启动服务。

You can verify this by looking at the logs of Postgres to see what might be going on: tail -f /usr/local/var/postgres/server.log

您可以通过查看 Postgres 的日志以查看可能发生的情况来验证这一点: tail -f /usr/local/var/postgres/server.log

回答by pisaruk

Another approach is using lunchy gem (a wrapper for launchctl):

另一种方法是使用午餐宝石(launchctl 的包装器):

brew install postgresql
initdb /usr/local/var/postgres -E utf8
gem install lunchy

To start postgres:

要启动 postgres:

lunchy start postgres

To stop postgres:

要停止 postgres:

lunchy stop postgres

For further info refer to: "How to Install PostgreSQL on a Mac With Homebrew and Lunchy"

有关更多信息,请参阅:“如何使用 Homebrew 和 Lunchy 在 Mac 上安装 PostgreSQL

回答by Kenial

Here my 2 cents: I made an alias for postgres pg_ctl and put it in .bash_profile(my postgresql version is 9.2.4, and database path is /Library/PostgreSQL/9.2/data).

这是我的 2 美分:我为 postgres pg_ctl 创建了一个别名并将其放入 .bash_profile(我的 postgresql 版本是 9.2.4,数据库路径是 /Library/PostgreSQL/9.2/data)。

alias postgres.server="sudo -u postgres pg_ctl -D /Library/PostgreSQL/9.2/data"

Launch new terminal.

启动新终端。

And then? You can start/stop your postgresql server with this:

进而?您可以使用以下命令启动/停止 postgresql 服务器:

postgres.server start
postgres.server stop

回答by Markus Amalthea Magnuson

The cleanest way by farto start/stop/restart postgres if you have installed it through brewis to simply unload and/or load the launchd configuration file that comes with the installation:

到目前为止,启动/停止/重新启动 postgres的最干净方法brew是简单地卸载和/或加载安装随附的 launchd 配置文件:

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

The first line will stop postgres and the second line will start it. No need to specify any data directories etc. since everything is in that file.

第一行将停止 postgres,第二行将启动它。无需指定任何数据目录等,因为所有内容都在该文件中。

回答by Todd

To start the postgresql server:

要启动 postgresql 服务器:

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

to end the postgresql server:

结束 postgresql 服务器:

pg_ctl -D /usr/local/var/postgres stop -s -m fast

You can also create an alias via CLI to make it easier:

您还可以通过 CLI 创建别名以使其更容易:

alias pg-start='pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start'
alias pg-stop='pg_ctl -D /usr/local/var/postgres stop -s -m fast'

With these you can just type "pg-start" to start Postgres and "pg-stop" to shut it down.

有了这些,您只需输入“pg-start”来启动 Postgres,输入“pg-stop”来关闭它。

回答by Crystian Le?o

For test purposes, i think PostgreSQL App is the best option!

出于测试目的,我认为 PostgreSQL App 是最好的选择!

Run an App, and the server is up and running. Close the App, and the server goes down.

运行一个应用程序,服务器就启动并运行了。关闭应用程序,服务器就宕机了。

http://postgresapp.com/

http://postgresapp.com/

回答by jkaluzka

when you install postgresql using homebrew:

当您使用自制软件安装 postgresql 时:

brew install postgres

at the end of the output you will see this methods to start server:

在输出结束时,您将看到以下启动服务器的方法:

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:
    postgres -D /usr/local/var/postgres

I think this is the best way.

我认为这是最好的方法。

You can add alias into your .profile for convenience.

为方便起见,您可以在 .profile 中添加别名。