协慌网

登录 贡献 社区

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

最终更新:

我忘了运行initdb命令。

通过运行此命令

ps auxwww | grep postgres

我看到 postgres 没有运行

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

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

更新:

>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

更新 2:

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

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

但是当我尝试启动 rails 服务器时,我仍然看到:

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

更新 3:

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

更新 4:

我发现没有 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

但我不明白这个:

> 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

也:

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

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

更新 6:

这看起来很奇怪:

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

不过,我这样做了:

>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

所以我这样做了:

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

所以我试过这个:

> 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

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

答案

Homebrew包管理器包含自动启动的 launchctl plists。有关更多信息,请运行brew info postgres

手动启动:

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

手动停止:

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

自动启动:

“要立即启动 postgresql 并在登录时重启:”

brew services start postgresql


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

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

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

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

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

检查 postgresql.conf 中的 listen_addresses 和 port:

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

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

打扫干净

Postgres 最有可能通过HomebrewFinkMacPortsEnterpriseDB安装程序安装。

检查以下命令的输出以确定安装它的软件包管理器:

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

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

brew services start postgresql

brew services stop postgresql

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

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