Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
386 views
in Technique[技术] by (71.8m points)

ruby on rails - PG::ConnectionBad at /sign_in could not connect to server: Connection but I can connect with psql

I have Ubuntu in a Window (which I installed from Microsoft Store). In this Ubuntu, I have a Ruby web app in this Ubuntu which connects to Postgres in my Window (not Ubuntu). This is my database.yml (/apps/appname/config/)

development:
  adapter: postgresql
  encoding: unicode
  pool: 5
  host: localhost
  port: 5432
  database: xxx
  username: postgres
  password: 

All these I have setup accordingly. When I run rails server, the web app starts and listening to localhost:3000. When I open the browser at localhost:3000, I get this error below:

> PG::ConnectionBad at /sign_in could not connect to server: Connection
> refused   Is the server running on host "localhost" (127.0.0.1) and
> accepting     TCP/IP connections on port 5432?

I have troubleshoot by running psql and I can connect to the database at port 5432.

Server [localhost]:
Database [postgres]: xxx
Port [5432]:
Username [postgres]:
Password for user postgres:
psql (10.15)
WARNING: Console code page (437) differs from Windows code page (1252)
         8-bit characters might not work correctly. See psql reference
         page "Notes for Windows users" for details.
Type "help" for help.

How do I troubleshoot this further? I realize that I didn't run Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux in Windows PowerShell in Window before I install Ubuntu. Could the issue due to this step which I miss out?

question from:https://stackoverflow.com/questions/65641530/pgconnectionbad-at-sign-in-could-not-connect-to-server-connection-but-i-can

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

First, I'm assuming this is a WSL2 instance, not WSL1.

WSL2 instances really do run in a VM. The "VirtualMachinePlatform" component is a subset of Hyper-V. The WSL2 instances are running NAT'd (not bridged) behind their own virtual NIC vEthernet (WSL).

localhost to the WSL2 instance points to the virtual interface, not to the Windows host. On the flip side, Windows does attempt to detect ports bound inside a WSL2 instance and allow Windows applications (such as a web-browser) access to the services on those ports via localhost. However, this seems to break down for some people every so often, requiring a wsl --shutdown of the VM in order to restore the functionality.

So yes, you can access the Rails app (running in WSL) using your web browser (running in Windows) at localhost. However, you can't access the Postgres server running in Windows from the Rails server running in WSL2 via localhost.

Or at least you shouldn't. I'm surprised that the psql command seems to be working. Is it that:

  • psql is running under WSL? (shouldn't work via localhost)
  • Or perhaps you mean that you are running psql from PowerShell or cmd? That would of course work via localhost
  • Or perhaps the psql command is in the Windows path that gets propagated to WSL? That would also allow it to work via localhost.

But ultimately, what you should need for accessing the Windows Postgres server from within WSL is to simply use a Windows IP or address pointing to it. I've detailed some options in this answer a few days ago. But short answer:

  • Use mDNS, the ".local" domain. If your hostname is stevesdesktop, then try replacing localhost in your database.yml with stevesdesktop.local.
  • If that doesn't work, use the Windows IP directly.
  • Or edit /etc/hosts with the IP and a name to assign.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...