1

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?

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

2 Comments

Ok. That's great. But I was only following the gorails article, which clearly implies that your windows postgres installation should be able to talk to the rails instance running in WSL. I have a question. Can you access the pgAdmin from the browser for your wsl postgres installation?
I see he recommends installing it on windows, but that feels strange since the whole development environment is on ubuntu... anyway, yes i can access pgAdmin from the browser.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.