Ask A Question

Notifications

You’re not receiving notifications from this thread.

rake db:create fails while following Windows 11 Rails setup guide

Emmett Carpenter asked in Rails

As the title states, while following the Windows 11 Rails setup guide I found that whenever I run rake db:create it fails with the following error:

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"?

I know that postgres is running because I can connect to it from Windows command prompt with psql -U postgres. I also made sure to add username: postgres and password: myPassword to database.yml in my new project.

Any ideas?

Reply

Try setting it to use host: 127.0.0.1 in database.yml.

By default, it's going to look for the Unix domain socket, which means a file on the linux system and not the port from your Windows PostgreSQL server.

Reply

Thanks for the response. I tried using host: 127.0.0.1 as well as host: 192.168.1.5, which was the address of my device, but this also failed with the following error:

Caused by:
PG::ConnectionBad: could not connect to server: Connection refused
        Is the server running on host "127.0.0.1" and accepting
        TCP/IP connections on port 5432?
Reply

I have a lot of experience running rails on windows...

since windows subsystem for Linux has been out most windows rails devs have moved to wsl as its a better platform form for rails development with the benefit of not having to work out the windows`isms trying to get ruby to work and compile gems correctly

I personally haven't done any rails development on windows for about 3 years since WSL is out.
now that WSL2 supports systemd you will be able to run services pretty close to a native linux experience.

Follow this guide to get WSL2 up and running

https://ubuntu.com/tutorials/install-ubuntu-on-wsl2-on-windows-11-with-gui-support#1-overview

once you are up and running you will need to get ruby, postgres, and other libraries

# install gpg keys
gpg --keyserver keyserver.ubuntu.com --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
# install rvm
curl -sSL https://get.rvm.io | bash
# install ruby 3.2.1
# rvm will handle most of the ubuntu packages for you
rvm install 3.2.1

# install the postgres dev library so rails gem install will work
sudo apt install  libpq-dev -y

# install the rails gem (latest version)
gem install rails
  • optional stuff # redis
sudo apt install redis-server

start postgres and redis via an alias

add the following to your ~/.bashrc file

alias dev-start='sudo systemctl start postgresql && sudo systemctl start redis-server'

I personally do not have postgres or redis-server running as start on boot because its a waste of laptop battery until I need it
when you want to run just run dev-start to have both services to run.

now back to postgres. Since this is on WSL you will have access to postgres unix socket. but your local wsl user will probably not have access to it.
you will have 2 options to fix this up.

  • add your local wsl user as a postgres user and give it super user access to postgres
  • lower permission of postgres to allow unix socket access for your user with out any password

if you want to use postgres unix socket you have to remove the :host from the database.yml file . removing the :host tells the pg gem to use the unix socket instead

Reply

Thanks for the reply. I'll do this, as the GoRails guide already had me set up almost everything through WSL and it seems simpler than trying to link the Windows version of Postgres to it.

Reply

I just updated the Windows 10 and 11 guides.

You have to manually enable systemd right now, but you can start Postgres using:

sudo service postgresql start
Reply

btw systemdis now enabled by default in the Ubuntu Preview application.

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 86,946+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.