PostgreSQL - WSL:不会初始化

PostgreSQL - WSL : won't init

我目前正在开发一个 Django 服务器,我需要一个数据库,Sqlite3 似乎不能很好地满足我的需要,所以我正在尝试使用 PostgreSQL。我在 Windows 上进行开发,但是一旦开发完成,服务器将在 Linux 系统上 运行,所以我正在尝试使用 WSL 来满足预期的结果。

wsl -l -v  
NAME            STATE           VERSION  
Ubuntu-22.04    Running         1

我也更新到 WSL2,同样的问题。

我的问题是我无法启动数据库:

# sudo service postgresql start
No PostgreSQL clusters exist; see "man pg_createcluster"

到目前为止我做了什么:

sudo apt update
sudo apt upgrade
sudo apt install postgresql postgresql-contrib

// it s installed
> psql --version
psql (PostgreSQL) 14.2 (Ubuntu 14.2-1ubuntu1)

// no service currently running
> ps aux| grep postgres
root       583  0.0  0.0  15044  1280 tty3     S    08:42   0:00 grep --color=auto postgres

// no cluster ?!?
> sudo service postgresql start
No PostgreSQL clusters exist; see "man pg_createcluster"

// indeed no cluster are created by default 
> pg_lsclusters
Ver Cluster Port Status Owner Data directory Log file

// but i can t create one
> pg_createcluster 14.2 main
Error: no initdb program for version 14.2 found

> sudo pg_createcluster 14 main
Creating new PostgreSQL cluster 14/main ...
/usr/lib/postgresql/14/bin/initdb -D /var/lib/postgresql/14/main --auth-local peer --auth-host scram-sha-256 --no-instructions
Can't exec "/usr/lib/postgresql/14/bin/initdb": Permission denied at /usr/bin/pg_createcluster line 86.
Error: Could not open /etc/postgresql/14/main/start.conf for writing: Permission denied
Can't exec "/bin/sh": Permission denied at /usr/bin/pg_createcluster line 617.
Error: initdb failed

我也试过了:

> sudo service --status-all
// ...
[ - ]  postgresql
// ...

> sudo ps aux | grep postgres
root       769  0.0  0.0  15044  1284 tty3     S    08:48   0:00 grep --color=auto postgres

我不是很明白这个 link : Change some config file that i can't find
而这个只建议重新格式化我的磁盘..

按照 Mark 的 Link 我找到了解决方案。

  1. 好像需要WSL2
  2. 我尝试了 2 个 wsl 发行版:
# Create the file repository configuration:
sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'

# Import the repository signing key:
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -

# Update the package lists:
sudo apt-get update

# Install the latest version of PostgreSQL.
# If you want a specific version, use 'postgresql-12' or similar instead of 'postgresql':
sudo apt-get -y install postgresql postgresql-contrib