win11结合wsl2搭建开发环境之六:基于wsl子系统Ubuntu24.04,编译postgresql-17
基于wsl子系统Ubuntu24.04,编译postgresql-17
前置准备
apt-get install build-essential libreadline-dev zlib1g zlib1g-dev bison flex libpq-dev libicu-dev
添加用户
adduser postgres
编译安装
cd /mnt/d/wsl/install/
wget https://ftp.postgresql.org/pub/source/v17.2/postgresql-17.2.tar.gz
tar zxvf postgresql-17.2.tar.gz
cd postgresql-17.2/
./configure --prefix=/usr/local/postgresql-17
make -j$(nproc) && make install
# 修改目录所有权
chown -R postgres:postgres /usr/local/postgresql-17
配置数据库
初始化数据库
mkdir -p /data/postgresql
chown -R postgres:postgres /data/postgresql
# 使用指定用户
su postgres -c "/usr/local/postgresql-17/bin/initdb -d /data/postgresql"
修改相关配置
cd /usr/local/postgresql-17/share/
cp postgresql.conf.sample postgresql.conf
vim postgresql.conf
修改内容如下
listen_addresses = 'localhost'
log_directory = '/mnt/d/log/postgresql'
修改ip权限,允许远程链接
cd /data/postgresql
vim pg_hba.conf
# 找到IPv4 local connections这句话
# 然后添加自己需要的IP网段
host all all 192.168.16.168/24 md5
通过systemctl管理
touch /etc/systemd/system/postgresql-17.service
vim /etc/systemd/system/postgresql-17.service
内容如下
[Unit]
Description=postgresql-17.service
After=network.target
[Service]
Type=forking
User=postgres
Environment=PGSTARTTIMEOUT=270
Environment=PGDATA=/data/postgresql
ExecStart=/usr/local/postgresql-17/bin/pg_ctl start -D ${PGDATA} -s -w -t ${PGSTARTTIMEOUT}
ExecReload=/usr/local/postgresql-17/bin/pg_ctl reload -D ${PGDATA} -s
ExecStop=/usr/local/postgresql-17/bin/pg_ctl stop -D ${PGDATA} -s -m fast
[Install]
WantedBy=multi-user.target
验证 是否正常运行
systemctl daemon-reload
systemctl stop postgresql-17
systemctl start postgresql-17
systemctl reload postgresql-17
systemctl restart postgresql-17
systemctl status postgresql-17
# 应显示 Active: active (running)
开机自启
systemctl enable postgresql-17
修改postgres用户的密码
/usr/local/postgresql-17/bin/psql -d postgres
alter user postgres with password 'postgres';

浙公网安备 33010602011771号