You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WSL2环境下PostgreSQL 12无法启动的问题求助

问题解决指南:WSL2中PostgreSQL 12启动失败(端口5432被占用)

问题背景

在WSL2环境使用PostgreSQL 12.14,执行psql无法连接服务器,启动服务时提示端口5432被占用。日志显示无法绑定127.0.0.1,netstat发现tcp6的5432端口处于LISTEN状态但无对应PID,pg_lsclusters显示12版本的main集群处于down状态,同时找不到pg_hba.conf文件,执行ps aux | grep 'postgres *-D'无结果。操作目录为克隆的concordia项目。

相关错误信息

psql连接错误

psql: error: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

服务启动错误日志

sudo service postgresql start
 * Starting PostgreSQL 12 database server                                                                                * Error: /usr/lib/postgresql/12/bin/pg_ctl /usr/lib/postgresql/12/bin/pg_ctl start -D /var/lib/postgresql/12/main -l /var/log/postgresql/postgresql-12-main.log -s -o  -c config_file="/etc/postgresql/12/main/postgresql.conf"  exited with status 1:
2023-05-11 15:31:28.195 EDT [1195] LOG:  starting PostgreSQL 12.14 (Ubuntu 12.14-0ubuntu0.20.04.1) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0, 64-bit
2023-05-11 15:31:28.196 EDT [1195] LOG:  could not bind IPv4 address "127.0.0.1": Address already in use
2023-05-11 15:31:28.196 EDT [1195] HINT:  Is another postmaster already running on port 5432? If not, wait a few seconds and retry.
2023-05-11 15:31:28.196 EDT [1195] WARNING:  could not create listen socket for "localhost"
2023-05-11 15:31:28.196 EDT [1195] FATAL:  could not create any TCP/IP sockets
2023-05-11 15:31:28.196 EDT [1195] LOG:  database system is shut down
pg_ctl: could not start server
Examine the log output.

netstat输出结果

root@2300-JMFC9K3:/home/artemisia/concordia# netstat -a -p -n
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp6       0      0 :::5432                 :::*                    LISTEN      -
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags       Type       State         I-Node   PID/Program name     Path
unix  2      [ ACC ]     SEQPACKET  LISTENING     17496    -                    /run/WSL/7_interop
etc.

pg_lsclusters输出结果

Ver Cluster Port Status Owner    Data directory              Log file
12  main    5432 down   postgres /var/lib/postgresql/12/main /var/log/postgresql/postgresql-12-main.log

解决步骤

1. 排查Windows主机侧的端口占用

WSL2网络与Windows主机共享,tcp6的5432端口LISTEN但无PID,大概率是Windows系统内的PostgreSQL或其他程序占用了端口:

  • 打开Windows命令提示符或PowerShell,执行:
    netstat -ano | findstr :5432
    
  • 找到对应PID后,打开任务管理器,通过PID定位进程并结束(如果是Windows版PostgreSQL,直接停止服务即可)。

2. 清理WSL内PostgreSQL残留进程

若Windows侧无端口占用,可能是WSL内存在PostgreSQL僵尸进程:

  • 强制杀死所有postgres相关进程:
    sudo pkill -9 postgres
    
  • 重新启动服务:
    sudo service postgresql start
    

3. 定位pg_hba.conf配置文件

PostgreSQL 12的配置文件默认路径为:

/etc/postgresql/12/main/pg_hba.conf

直接用编辑器打开:

sudo nano /etc/postgresql/12/main/pg_hba.conf

若找不到,执行全局搜索:

sudo find / -name pg_hba.conf

4. 修改PostgreSQL监听端口(可选)

若端口占用问题反复出现,可修改监听端口规避冲突:

  • 打开postgresql.conf配置文件:
    sudo nano /etc/postgresql/12/main/postgresql.conf
    
  • 将port参数改为未被占用的端口(如5433):
    port = 5433
    
  • 重启服务:
    sudo service postgresql restart
    
  • 连接时指定新端口:
    psql -p 5433 -U postgres
    

5. 创建默认管理员账号

服务启动成功后,进入psql shell:

sudo -u postgres psql

执行SQL创建管理员用户(替换admin_user和admin_pass为自定义用户名和密码):

CREATE USER admin_user WITH SUPERUSER PASSWORD 'admin_pass';

内容的提问来源于stack exchange,提问作者Pendelluft

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 22:24:57