Greg Houston

Joined

10 Experience
0 Lessons Completed
0 Questions Solved

Activity

Posted in Setup Windows 10 Discussion

on WSL 1 (and WSL 2), rake running inside linux cant connect to postgresql running in windows through a unix domain socket. Instead you will need to connect using regular network over TCP/IP.

first, you need to determine your windows ip address. You can find this by looking in windows network settings; or by running ipconfig in powershell. In your linux, ipconfig.exe | grep IPv4 | cut -d: -f2 should work as well.

next in your rails cofig/database.yaml, uncomment the #host 127.0.0.1 line and change it to your ip address. Also, update the username, password, and port as necessary. (did you create the user in postgresql?)

for example:

...
development:
  <<: *default
  database: myapp_development

  # The specified database role being used to connect to postgres.
  # To create additional roles in postgres see `$ createuser --help`.
  # When left blank, postgres will use the default role. This is
  # the same name as the operating system user that initialized the database.
  username: myapp

  # The password associated with the postgres role (username).
  password: <%= ENV['MYAPP_DATABASE_PASSWORD'] %>

  # Connect on a TCP socket. Omitted by default since the client uses a
  # domain socket that doesn't need configuration. Windows does not have
  # domain sockets, so uncomment these lines.
  #host: 127.0.0.1
  host: 10.0.0.99

  # The TCP port the server listens on. Defaults to 5432.
  # If your server runs on a different port number, change accordingly.
  port: 5432
...

finally, you have to modify postgresql's pg_hba.conf file (located in postgresql's data folder) to give permission for the incoming network connection.

example:

...
# From rails myapp in local Ubuntu WSL distro
host    myapp_development myapp         10.0.0.99/32            md5
...

current documentation for WSL 2 says WSL 2 will also work using the ip address of the host. So this method should continue to work for WSL 2. docs: Accessing Windows Applications From Linux