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

从Windows的DBeaver连接WSL中PostgreSQL时密码验证失败

问题与解决方案:WSL2 PostgreSQL无法被Windows DBeaver连接

问题背景

  • 环境:WSL2 Ubuntu 20.04,PostgreSQL 12.19,Windows端安装DBeaver
  • 已完成操作:通过sudo passwd postgres将Linux系统的postgres用户密码设为root,可在WSL终端用sudo -u postgres psql访问数据库
  • 连接尝试:用DBeaver填写以下信息连接
    host: 127.0.0.1
    port: 5432
    username: postgres
    password: root
    
  • 错误提示:FATAL: password authentication failed for user "postgres",服务器日志显示User "postgres" has no password assigned
  • 已尝试无效操作:执行sudo ufw allow 5432/tcp、修改pg_hba.conf的IPv4配置为0.0.0.0/0并设md5认证、更新postgresql.conf设置listen_addresses = '*'

核心原因

你混淆了Linux系统的postgres用户密码和PostgreSQL数据库的postgres用户密码,这是两个完全独立的密码体系。之前仅修改了系统用户密码,并未设置数据库用户的密码,导致连接时认证失败。

解决步骤

  1. 进入PostgreSQL命令行终端:
    sudo -u postgres psql
    
  2. 在psql交互界面中,设置数据库postgres用户的密码:
    ALTER USER postgres WITH PASSWORD 'root';
    
    执行后返回ALTER ROLE即表示设置成功,输入\q退出psql。
  3. 确保pg_hba.conf的认证规则正确(若之前已修改可跳过):
    • 编辑配置文件:
      sudo nano /etc/postgresql/12/main/pg_hba.conf
      
    • 找到IPv4本地连接的配置行,修改为:
      host    all             all             0.0.0.0/0               md5
      
    • 按Ctrl+O保存,Ctrl+X退出编辑器。
  4. 重启PostgreSQL服务使配置生效:
    sudo systemctl restart postgresql
    
  5. 回到Windows端的DBeaver,重新测试连接,使用密码root即可成功连接。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.21 04:12:07