Windows 11下PostgreSQL已运行但Rails测试无法连接问题求助
解决方案
方案一:创建PostgreSQL的root角色
Windows环境下PostgreSQL默认超级用户是postgres,先通过该用户登录创建root角色:
- 打开CMD/PowerShell,执行命令登录psql:
输入安装PostgreSQL时设置的psql -U postgrespostgres用户密码,进入交互式命令行。 - 在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
相关产品推荐
相关产品推荐

