繁体   English   中英

WSL 上的 Rails 设置:db:create 导致“无法连接到服务器错误”

[英]Rails setup on WSL: db:create results in “cannot connect to server error”

我正在尝试将 Rails 设置为在 WSL 2 中工作。我已按照本指南进行操作。 在执行命令rails db:create我得到以下错误:

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"?
Couldn't create 'myapp_development' database. Please check your configuration.
rails aborted!
ActiveRecord::NoDatabaseError: 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"?
/home/ke/rails/myapp/bin/rails:9:in `<top (required)>'
/home/ke/rails/myapp/bin/spring:15:in `<top (required)>'
bin/rails:3:in `load'
bin/rails:3:in `<main>'

Caused by:
PG::ConnectionBad: 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"?
/home/ke/rails/myapp/bin/rails:9:in `<top (required)>'
/home/ke/rails/myapp/bin/spring:15:in `<top (required)>'
bin/rails:3:in `load'
bin/rails:3:in `<main>'
Tasks: TOP => db:create
(See full trace by running task with --trace)

根据说明,我使用 Enterprise DB 的交互式安装程序安装了 postgresql。 WSL 安装是否有某种方式无法在 Windows 上看到 postgres 安装?

我遵循相同的指南并遇到了同样的问题。 通过显式指定访问 postgres 数据库的主机、用户名和密码来修复它,就像在 database.yml 中一样:

default: &default
  adapter: postgresql
  encoding: unicode
  host: 127.0.0.1 # or localhost
  username: postgres # or your username
  password: ENV['POSTGRESQL_PASSWORD'] # or your password
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

问题是 WSL (ubuntu) 试图通过 Unix 套接字连接到 windows 上的 postgres,该套接字不存在。 就像在运行psql --host=127.0.0.1 database_name时使用--host选项一样,我们在这里为 rails 设置了默认值。

如果您想在 windows 环境中安装和使用 postgres,那么您使用 WSL 的目的是什么? 您应该在 ubuntu(或您使用的任何其他发行版)上安装 postgres。

我也使用 WSL,并在 ubuntu 上安装了 ruby、rails、postgres 等,效果很好。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM