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

Windows 11下PostgreSQL已运行但Rails测试无法连接问题求助

解决方案

方案一:创建PostgreSQL的root角色

Windows环境下PostgreSQL默认超级用户是postgres,先通过该用户登录创建root角色:

  • 打开CMD/PowerShell,执行命令登录psql:
    psql -U postgres
    
    输入安装PostgreSQL时设置的postgres用户密码,进入交互式命令行。
  • 在psql中创建带登录权限和数据库创建权限的root角色(替换你的密码为自定义密码):
    CREATE ROLE root WITH LOGIN PASSWORD '你的密码' CREATEDB;
    
  • 退出psql:
    \q
    
  • 回到Rails项目,确保config/database.yml中test环境的username为root,password为你刚才设置的密码,然后重新运行rails test。

方案二:修改Rails数据库配置(更推荐)

PostgreSQL默认不存在root角色,直接修改Rails配置使用默认的postgres用户更合理:

  • 打开Rails项目的config/database.yml文件,找到test环境配置段,调整username和password为PostgreSQL的postgres用户信息:
    test:
      <<: *default
      database: 你的项目名_test
      username: postgres
      password: 你的postgres用户密码
      host: localhost
      port: 5432
    
  • 保存文件后,执行rails test即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.13 06:35:48