简体   繁体   English

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

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

I'm trying to setup Rails to work in WSL 2. I have followed this guide to do so.我正在尝试将 Rails 设置为在 WSL 2 中工作。我已按照本指南进行操作。 On executing the command rails db:create I get the following error:在执行命令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)

As per the instructions I have installed postgresql using the interactive installer by Enterprise DB.根据说明,我使用 Enterprise DB 的交互式安装程序安装了 postgresql。 Is there some way the WSL installation isn't able to see the postgres installation on Windows? WSL 安装是否有某种方式无法在 Windows 上看到 postgres 安装?

I followed the same guide and had the same problem.我遵循相同的指南并遇到了同样的问题。 Fixed it by explicitly specifying the host, username and password for accessing postgres database like so in database.yml:通过显式指定访问 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 } %>

The problem was that WSL (ubuntu) was trying to connect to postgres on windows via a Unix socket, which did not exist.问题是 WSL (ubuntu) 试图通过 Unix 套接字连接到 windows 上的 postgres,该套接字不存在。 Just like using --host option while running psql --host=127.0.0.1 database_name , we have set a default for rails here.就像在运行psql --host=127.0.0.1 database_name时使用--host选项一样,我们在这里为 rails 设置了默认值。

If you want to install and use postgres in your windows environment than what are you using WSL for?如果您想在 windows 环境中安装和使用 postgres,那么您使用 WSL 的目的是什么? You should install postgres on ubuntu(or any other distro that you use).您应该在 ubuntu(或您使用的任何其他发行版)上安装 postgres。

I also use WSL and have ruby, rails, postgres etc all installed on ubuntu and it works great.我也使用 WSL,并在 ubuntu 上安装了 ruby、rails、postgres 等,效果很好。

暂无
暂无

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

相关问题 无法通过 WSL 使用 Rails 启动本地服务器 - Cannot start local server with Rails via WSL 嗨,我正在尝试在 WSL Ubuntu 中运行 rails db:create 并不断收到有关 postgres 的此错误 - Hi I am trying to run rails db:create in WSL Ubuntu and keep getting this error in regards to postgres Rails:rake db:create:all(无法连接到服务器) - Rails: rake db:create:all (could not connect to server) 无法使用Rails 5连接到Heroku上的安装数据库 - Cannot connect to setup database on heroku with rails 5 Rails rake db:setup在服务器上失败:无法通过套接字&#39;/tmp/mysql.sock&#39;连接到本地MySQL服务器 - Rails rake db:setup fails on server: Can't connect to local MySQL server through socket '/tmp/mysql.sock' rake db:create&rails服务器 - rake db:create & rails server 在rails上创建db时出现rails错误(rails db:create) - The error of rails when create db on rails (rails db:create) (Rails新手)rails服务器错误:无法加载此类文件 - bootsnap / setup(LoadError) - (New to Rails) rails server error: cannot load such file — bootsnap/setup (LoadError) Rails:rake db:create:all(无法连接到服务器)psql正常工作 - Rails: rake db:create:all (could not connect to server) psql works fine Rails db:create using PostgreSQL return PG::ConnectionBad: 无法连接到服务器:连接被拒绝 - Rails db:create using PostgreSQL return PG::ConnectionBad: could not connect to server: Connection refused
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM