简体   繁体   中英

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. On executing the command rails db:create I get the following error:

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. Is there some way the WSL installation isn't able to see the postgres installation on Windows?

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:

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. Just like using --host option while running psql --host=127.0.0.1 database_name , we have set a default for rails here.

If you want to install and use postgres in your windows environment than what are you using WSL for? You should install postgres on ubuntu(or any other distro that you use).

I also use WSL and have ruby, rails, postgres etc all installed on ubuntu and it works great.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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