參考文章:https://xu.ci/2019/12/wsl2ubuntupostgres.html
1.導(dǎo)入證書
```
sudo apt-get install wget ca-certificates
wget --quiet -O?https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
```
2.增加官網(wǎng)給出的源、更新apt并安裝postgresql
```
sudo sh -c'echo "deb http://apt.postgresql.org/pub/repos/apt/ `lsb_release -cs`-pgdg main" >> /etc/apt/sources.list.d/pgdg.list'
sudo apt-get update
sudo apt-get install postgresql postgresql-contrib
```
3.修改postgres用戶密碼
```
sudo passwd postgres
```
4.測試鏈接修改密碼
```
su - postgres
pg_ctlcluster 12 main start
psql
\conninfo
```
之前安裝postgres的時(shí)候有一個(gè)提示:
所以我直接輸入了
```
pg_ctlcluster 12 main start
```
就出現(xiàn)了報(bào)錯(cuò)计福。實(shí)際上應(yīng)該先切換用戶到postgres馆蠕,再psql咨油。
5.?修改數(shù)據(jù)庫配置文件
```
sudo su
cd /etc/postgresql/12/main
vim postgresql.conf
```
把?#listen_addresses = 'localhost'?改為?listen_addresses = '*'
#fsync = on?改為?fsync = off
6.設(shè)置開機(jī)啟動(dòng)
```
sudo vim /etc/init.wsl
```
添加:
```
#! /bin/sh
pg_ctlcluster 12 main start
```
保存后退出:
```
sudo chmod +x /etc/init.wsl
```
在win下開一個(gè)新的txt文檔,文檔里寫入:
```
Set ws = WScript.CreateObject("WScript.Shell")
ws.run"wsl -d ubuntu -u root /etc/init.wsl"
```
然后重命名該文檔拂封,記得把.txt后綴去掉乍迄,改名為linux-start.vbs
接著按win+R,輸入shell:startup
把linux-start.vbs文件放到這里即可