最后更新:

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

其他回答

已经安装了Postgres与自制程序,这是我所做的启动Postgres,并保持在前台查看日志:

/opt/homebrew/opt/postgresql/bin/postgres -D /opt/homebrew/var/postgres

PostgreSQL有两个主要组件:数据库服务器和客户端。

有一个通过CLI包含的客户端,或者像我一样,你可能习惯了像phpMyAdmin这样的工具,所以它需要一个单独的GUI客户端。

例如,在macOS上:安装Postgres。该应用程序大约65 MB,来自:https://postgresapp.com/

然后按照下面的指示去做:

使用右边的按钮安装并初始化服务器。 使用sudo mkdir -p /etc/paths更新$PATHd && echo /Applications/Postgres。app/Contents/Versions/latest/bin | sudo tee /etc/paths.d/postgresapp . app/Contents/Versions/latest/bin 最后,安装pgAdmin GUI,大约100 MB,从https://www.pgadmin.org/download/pgadmin-4-macos/下载

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


您可能想启动PG服务器,但它不是。

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

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

具体版本:-

 tail -f /usr/local/var/postgres@[VERSION_NUM]/server.log

Eg:

 tail -f /usr/local/var/postgres@11/server.log

这个答案的变体是:https://stackoverflow.com/a/13103603/2394728

initdb `brew --prefix`/var/postgres/data -E utf8`` &&  pg_ctl -D /usr/local/var/postgres/data -l logfile start

使用brew安装postgresql: brew Install postgresql,您可以使用“@”符号指定版本:brew Install postgresql@14 Start postgresql: Start postgresql或特定版本的brew服务Start postgresql@14 Stop postgresql:停止postgresql