简体   繁体   中英

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?

When you say Ubuntu in a Window I suppose you have a Ubuntu running on a VM on your Windows OS.

From the Ubuntu system point of view, localhost is it's own IP, not the one where PostgreSQL is running.

The virtualization engine usually assigns an IP to the VMs running on it, depending on the configuration of the engine, the network configuration of the VM in it and the network configuration of the system inside the VM (usually, plain DHCP). I'd consider looking at all of them.

If you are creating an internal network (which is the default on some virtualization engines and usually is in the range 192.168.122.0/24 ), the IP of your host (the system running in the hardware, the one running the virtualization engine, the one running the PostgreSQL server, not the VM) inside this private network will probably be 192.168.122.1 . That will be the IP where the VM will be able to connect to the PostgreSQL server.

Note: You'll need to ensure PostgreSQL is started after the virtualization engine starts the virtual network, so PostgreSQL will open the port on this network, too (well, I don't know very much about how Windows handles opening TCP ports, but it's a good practice).

Edit

While the above commands and tips are still useful, Ubuntu in a Window runs WSL , which is not a VM engine. As stated in the WSL FAQs :

WSL shares the IP address of Windows, as it is running on Windows. As such you can access any ports on localhost eg if you had web content on port 1234 you could https://localhost:1234 into your Windows browser.

the network stack is shared, so localhost and 127.0.0.1 are good hosts to connect applications across Windows and Linux apps. Also, as stated in the FAQs , your firewall might be blocking network connections. If disabling the firewall solves the problem, you'll need to explicitly allow the desired connection.

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.

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