WSL Ubuntu中PostgreSQL间歇性无法启动问题求助
WSL Ubuntu中PostgreSQL间歇性无法启动问题求助
大家好,我最近在WSL2的Ubuntu 22.04环境里碰到了PostgreSQL 14间歇性启动失败的问题,折腾好几天没彻底解决,想请教下有没有大佬遇到过类似情况,给点排查思路~
先交代下我的环境信息:
- WSL版本:2,Ubuntu实例当前可正常启动(之前出现过Stopped状态)
- Ubuntu版本:22.04.1 LTS(jammy)
- PostgreSQL版本:14.6(Ubuntu 14.6-0ubuntu0.22.04.1)
问题详情
Windows 11更新之后就开始出现这个问题:有时候执行sudo service postgresql start或者restart时,会直接启动失败,核心报错是端口5432被占用,具体错误输出如下:
* Starting PostgreSQL 14 database server * Error: /usr/lib/postgresql/14/bin/pg_ctl /usr/lib/postgresql/14/bin/pg_ctl start -D /var/lib/postgresql/14/main -l /var/log/postgresql/postgresql-14-main.log -s -o -c config_file="/etc/postgresql/14/main/postgresql.conf" exited with status 1: 2023-03-17 21:08:03.100 CST [1028] LOG: starting PostgreSQL 14.6 (Ubuntu 14.6-0ubuntu0.22.04.1) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0, 64-bit 2023-03-17 21:08:03.103 CST [1028] LOG: could not bind IPv4 address "127.0.0.1": Address already in use 2023-03-17 21:08:03.103 CST [1028] HINT: Is another postmaster already running on port 5432? If not, wait a few seconds and retry. 2023-03-17 21:08:03.103 CST [1028] WARNING: could not create listen socket for "localhost" 2023-03-17 21:08:03.103 CST [1028] FATAL: could not create any TCP/IP sockets 2023-03-17 21:08:03.106 CST [1028] LOG: database system is shut down pg_ctl: could not start server Examine the log output.
但诡异的是,我用ps -ef | grep postgres查进程,根本找不到PostgreSQL相关的进程;用sudo netstat -tulpn检查端口占用,也没看到任何程序占用5432端口。
而且这个问题完全是间歇性的:前一天我反复退出再重启Ubuntu终端,偶尔能正常启动;昨天试了各种方法都失败,今天再执行启动命令,居然又正常了,完全摸不清规律😅
我已经尝试过的操作
- 修改PostgreSQL配置文件
sudo nano /etc/postgresql/14/main/postgresql.conf,把listen_addresses从localhost改成了* - 执行
sudo service postgresql stop确认服务已停止后,再尝试启动 - 重启Windows电脑和WSL实例
- 查看PostgreSQL日志,日志内容和命令行报错一致,还是端口占用的提示
我是用这个环境开发Ruby on Rails项目的,这个间歇性问题真的很影响开发节奏,有没有大佬能帮忙分析下可能的原因,或者给点下一步的排查方向呀?
备注:内容来源于stack exchange,提问作者JS3
相关产品推荐
相关产品推荐

