Can't start postgresql on WSL Ubuntu

pre*_*lna 8 postgresql windows-subsystem-for-linux

I'm trying to start my development instance of Postgresql on my local machine. I'm running Windows 10 Home (build 18363.900), with a WSL Ubuntu 18.04.4 LTS. I've tried a number of things:

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

(same with restart)

$ 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.
Run Code Online (Sandbox Code Playgroud)

(same for postgres)

$ 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.
Run Code Online (Sandbox Code Playgroud)

and just in case

$ sudo service postgres start
postgres: unrecognized service
Run Code Online (Sandbox Code Playgroud)

service does me no better.

Stop, of course, works as expected:

$ sudo service postgresql stop
 * Stopping PostgreSQL 10 database server                                                  [ OK ]
Run Code Online (Sandbox Code Playgroud)

but

sudo service postgresql restart
Run Code Online (Sandbox Code Playgroud)

gives the same as start.

Status also gives me an expected result:

$ sudo service postgresql status
10/main (port 5432): down
Run Code Online (Sandbox Code Playgroud)

My /etc/hosts reads

# 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
Run Code Online (Sandbox Code Playgroud)

which leads me to believe that changes to /etc/hosts won't help

I've tried to kill postmaster, but its pid keeps changing every time I try to kill it, which leads me to believe its spinning

$ ps -ef | grep postmaster | awk '{print $2}'
8151
$ ps -ef | grep postmaster | awk '{print $2}'
8154
Run Code Online (Sandbox Code Playgroud)

Shooting in the dark to kill the process doesn't work either, confusingly

$ 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
Run Code Online (Sandbox Code Playgroud)

Which is weird -- I killed pid 300

Surprisingly, nothing seems to be listening on localhost:5432

$ curl localhost:5432
curl: (52) Empty reply from server
Run Code Online (Sandbox Code Playgroud)

I'm not sure where to go from here.

whi*_*run 8

为什么不能使用 systemd 的答案很简单:WSL 从第 1 天到现在都不支持它。这是一个已知问题,您可以参考2018 年开放的这个问题这个问题使用类似的 Windows 配置和 WSL了解更多信息。

postgresql 的问题是另一个已知问题。它在 WSL 2 中得到修复。为了解决 WSL 1 上的这个问题,这篇文章告诉你设置fsync=ondata_sync_retry=true。解决问题的另一种方法是设置fsync=off. 有关 WSL 中 fsync 问题的更多信息,请参阅此问题