如何在 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

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

答案

# IPv4 local connections:
host    all             all             127.0.0.1/32            trust
#listen_addresses = 'localhost'     # what IP address(es) to listen on;
#port = 5432                # (change requires restart)
brew && brew list|grep postgres
fink && fink list|grep postgres
port && port installed|grep postgres
brew services start postgresql
brew services stop postgresql
initdb /usr/local/var/postgres -E utf8

如果您的计算机突然重启

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

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

brew install postgresql
initdb /usr/local/var/postgres -E utf8
gem install lunchy
lunchy start postgres
lunchy stop postgres
alias postgres.server="sudo -u postgres pg_ctl -D /Library/PostgreSQL/9.2/data"
postgres.server start
postgres.server stop
launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
pg_ctl -D /usr/local/var/postgres stop -s -m fast
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'

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

运行一个应用程序,服务器已启动并正在运行。关闭应用程序,然后服务器关闭。

http://postgresapp.com/

brew install postgres
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