最后更新:

我忘了运行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

但是当我试图启动Ruby on Rails服务器时,我仍然看到这个:

服务器是否运行在主机“localhost”上并接受 5432端口上的TCP/IP连接?

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

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


当前回答

docker run --name some-postgres -e POSTGRES_PASSWORD=mysecretpassword -d postgres

https://hub.docker.com/_/postgres

其他回答

到目前为止,如果你已经通过brew安装PostgreSQL,启动/停止/重新启动PostgreSQL最简单的方法是卸载和/或加载安装时附带的launchd配置文件:

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

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

Homebrew包管理器包括自动启动的launchctl plists。有关详细信息,请运行brew info postgres。

手动启动

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

手动停止

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

自动启动

让launchd现在启动postgresql并在登录时重新启动:

Brew服务启动postgresql


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

pg_ctl -D /usr/local/var/postgres状态的结果是什么?

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

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

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

检查postgresql.conf中的listen_addresses和port:

“监听|端口”/usr/local/var/ postgresql.conf

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

清理

PostgreSQL最有可能通过Homebrew、Fink、MacPorts或EnterpriseDB安装程序安装。

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

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

我也面临着同样的问题。我尝试了所有这些方法,但没有一个奏效。

我最终通过将Django设置中的PostgreSQL HOST从localhost更改为127.0.0.1来让它工作。

这招每次都管用,灵感来自克雷格·林格:

brew install proctools
sudo pkill -u postgres

Proctools包含pkill。如果你没有Homebrew: https://brew.sh/

启动PostgreSQL服务器。

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

使用实例关闭PostgreSQL服务器。

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

你也可以通过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'

你可以输入“pg-start”启动PostgreSQL,输入“pg-stop”关闭PostgreSQL。