无法在WSL Ubuntu上启动PostgreSQL

11

我正在尝试在本地计算机上启动我的Postgresql开发实例。 我正在运行Windows 10家庭版(版本18363.900),带有WSL Ubuntu 18.04.4 LTS。 我已经尝试了很多方法:

$ sudo systemctl start postgresql
System has not been booted with systemd as init system (PID 1). Can't operate.

(与重新启动相同)
$ sudo /etc/init.d/postgresql start
 * Starting PostgreSQL 10 database server                                                          * Error: /usr/lib/postgresql/10/bin/pg_ctl /usr/lib/postgresql/10/bin/pg_ctl start -D /var/lib/postgresql/10/main -l /var/log/postgresql/postgresql-10-main.log -s -o  -c config_file="/etc/postgresql/10/main/postgresql.conf"  exited with status 1:
2020-06-16 20:36:37.334 PDT [8106] LOG:  could not bind IPv4 address "127.0.0.1": Permission denied
2020-06-16 20:36:37.334 PDT [8106] HINT:  Is another postmaster already running on port 5432? If not, wait a few seconds and retry.
2020-06-16 20:36:37.335 PDT [8106] WARNING:  could not create listen socket for "localhost"
2020-06-16 20:36:37.335 PDT [8106] FATAL:  could not create any TCP/IP sockets
2020-06-16 20:36:37.335 PDT [8106] LOG:  database system is shut down
pg_ctl: could not start server
Examine the log output.

(同样适用于PostgreSQL)
$ sudo service postgresql start
 * Starting PostgreSQL 10 database server                                                          * Error: /usr/lib/postgresql/10/bin/pg_ctl /usr/lib/postgresql/10/bin/pg_ctl start -D /var/lib/postgresql/10/main -l /var/log/postgresql/postgresql-10-main.log -s -o  -c config_file="/etc/postgresql/10/main/postgresql.conf"  exited with status 1:
2020-06-16 20:37:19.907 PDT [8140] LOG:  could not bind IPv4 address "127.0.0.1": Permission denied
2020-06-16 20:37:19.907 PDT [8140] HINT:  Is another postmaster already running on port 5432? If not, wait a few seconds and retry.
2020-06-16 20:37:19.907 PDT [8140] WARNING:  could not create listen socket for "localhost"
2020-06-16 20:37:19.907 PDT [8140] FATAL:  could not create any TCP/IP sockets
2020-06-16 20:37:19.908 PDT [8140] LOG:  database system is shut down
pg_ctl: could not start server
Examine the log output.

以防万一

$ sudo service postgres start
postgres: unrecognized service

服务对我没有任何帮助。

当然,停止功能按预期工作:

$ sudo service postgresql stop
 * Stopping PostgreSQL 10 database server                                                  [ OK ]

但是
sudo service postgresql restart

给出的结果与开始相同。

状态还给我一个预期的结果:

$ sudo service postgresql status
10/main (port 5432): down

我的 /etc/hosts 文件内容如下

# This file is automatically generated by WSL based on the Windows hosts file:
# %WINDIR%\System32\drivers\etc\hosts. Modifications to this file will be overwritten.
127.0.0.1       localhost
127.0.1.1       DESKTOP-LKMEB17.localdomain     DESKTOP-LKMEB17

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

这让我相信更改/etc/hosts文件不会有所帮助。

我尝试杀死postmaster进程,但每次尝试时其pid都在变化,这让我相信它正在自旋。

$ ps -ef | grep postmaster | awk '{print $2}'
8151
$ ps -ef | grep postmaster | awk '{print $2}'
8154

在黑暗中盲目地杀死进程也行不通,这会让人感到困惑。
$ ps -ef | grep postmaster
me       298   116  0 15:35 tty3     00:00:00 grep postmaster
$ kill -9 300
-bash: kill: (300) - No such process
$ kill -9 298
-bash: kill: (298) - No such process
$ kill -9 299
-bash: kill: (299) - No such process
$ kill -9 301
-bash: kill: (301) - No such process
$ ps -ef | grep postmaster
me       300   116  0 15:36 tty3     00:00:00 grep postmaster

这很奇怪——我杀掉了pid 300。

令人惊讶的是,似乎没有任何东西在监听localhost:5432。

$ curl localhost:5432
curl: (52) Empty reply from server

我不确定接下来该怎么做。


你是用apt或其他工具安装了postgresql吗?你在Ubuntu上安装了AppArmor或SELinux吗?我们如何复制你的安装过程? - Jaime
我使用apt安装了postgresql,并且在Ubuntu上没有安装SELinux或AppArmor。这是WSL上的标准Ubuntu,所以我认为设置应该遵循向导。 - prekolna
2个回答

8
为什么不能使用systemd的答案很简单:WSL从第一天起就不支持它,直到现在。这是一个已知的问题,您可以参考2018年开放的此问题使用类似Windows和WSL配置的此问题了解更多信息。
PostgreSQL的问题是另一个已知问题。它在WSL 2中得到了修复。要解决WSL 1上的此问题,请参考此帖子并设置fsync=ondata_sync_retry=true。解决问题的另一种方法是将fsync=off设置。有关WSL中fsync问题的更多信息,请参考此问题

编辑:2022年9月21日,微软宣布已将systemd支持添加到WSL中,因此通过最新更新,现在应该可以使用systemd了。(source)


4

服务停止只是告诉您服务已经关闭。它并没有说它停止了服务。

当您试图找到postmaster PID时,您正在查找自己尝试查找postmaster PID的PID:

$ ps -ef | grep postmaster
me       300   116  0 15:36 tty3     00:00:00 grep postmaster

当您使用curl检查端口5432时,它告诉您它收到了一个空回复。这意味着某些东西正在侦听端口5432。您希望看到的是“连接失败”消息。
您在Windows主机上的端口5432上运行postgresql服务器吗?如果是,这将防止您从WSL内部运行一个并且您将无法从那里查看该进程。
请尝试在WSL端更改端口至5433。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接