WSL-2: Connect from Windows host machine to Ubuntu guest running PostgreSQL - postgresql

I'm running PostgreSQL 10 on a Windows 10 WSL-2 installation of Ubuntu 18.04. I can start the service, connect to it locally from within Ubuntu with psql, and use it for local development. So far, so good!
I'd like to be able to connect to this instance from the host Windows OS, but can't connect. From Windows cmd, I've tried using telnet to see if I can connect to the NAT IP of the Ubuntu machine:
$ telnet 172.123.456.789 5432
Connecting To 172.31.175.251...Could not open connection to the host, on port 5432: Connect failed
When I run Django's runserver on port 8000, however, I can connect:
$ telnet 172.123.456.789 8000
[connected, returns HTML]
I've tried checking to see if it was something in Ubuntu's firewall ufw, but it isn't running:
$ sudo ufw status verbose
Status: inactive
Has anyone cracked this nut?

Related

Connect to existing instance of Postgresql from WSL 2

I'm running Ubuntu WSL 2 on Windows 10. Before I installed WSL I already had Postgresql installed on my Windows 10. I wanted to connect to the database from WSL but so far it fails.
When running:
psql
I get:
psql: error: 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?
When running:
psql -h 127.0.0.1 -p 5432 -U postgres
I get:
psql: error: 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?
And WSL clearly doesn't see the database. When running this:
sudo service postgresql start
It returns:
postgresql: unrecognized service
I know that one solution is to install the database in WSL but wanted to try to connect to existing instance first.
You should edit pg_hba.conf
check wsl subnet
execute ipconfig from cmd. Below is example.
Ethernet Adapter vEthernet (WSL)
IPv4 Address . . . . . . . . . . . .: 172.19.230.81
edit pg_hba.conf
pg_hba.conf path is C:\Program Files\PostgreSQL\{postgresqlVersion}\data\pg_hba.conf. if you installed Windows Postgresql default install path.
subnet mask is use value from ipconfig
host all all 172.19.230.0/24 md5
restart postgresql
you can restart postgresql from services.msc. service name is postgresql-x64-{postgresqlVersion}.
access from psql
psql -h 172.19.230.81 -p 5432 -U postgres

Troubles connecting PostgreSQL installed in WSL2 Ubuntu from the Windows 10 host

I'm having big troubles connecting to PostgreSQL, installed in WSL2 Ubuntu, from the Windows 10 host.
Here is what I have done so far.
Set password for postgres user.
Set the following in pg_hpa.conf
local all postgres md5
host all all 0.0.0.0/0 md5
Set the following in postgresql.conf
listen_addresses = '*'
Tested successfully that I can connect with psql -U postgres - so password should be set correctly.
When I try to connect using either pgAdmin or psql from the Windows 10 host, I get the following error.
error: could not connect to server: FATAL: password authentication failed for user "postgres"
Anything else I can change in pg_hpa.conf or other PostgreSQL configurations which might help?
I'm thinking that I could have something to do with that WSL2 has its own IP, even tough that connections from the hosts are made appear coming from localhost. The connection seems to be made, but authentication failed for some reason when it is not made from within Ubuntu.
I later tested with nc -l 5432 and for some reason, port 5432 didn't reach WSL2 on local host, but did on WSL-2 IP. If I use any other free port e.g. 5434 (free both on Windows and WSL-2) it works fine and I can connect to the PostgreSQL service on WSl-2 through localhost on windows.
I didn't change any options since the original posted question - only the port.
Update: It turns out that there indeed was a process from an old install of postgres on Windows which was listening on port 5432. This of cause explains it all. ..:/
As was pointed out by the original poster, ensure that you do not have a Windows service already running and listening on that port. In my case it was a PostgreSQL instance installed as a Windows service. Whatever the underlying networking support, it seems you can have a process/service listening on port 5432 in Windows as well as a separate Linux process listening on port 5432 from WSL2.
It was not enough to stop the Windows PostgreSQL service. Connecting from the Windows pgAdmin4 installation program failed. I also disabled the service from Microsoft Services console.
I needed to also stop and start the WSL2 postgresql. Only then was I successfully able to connect from my Windows pgAdmin4 installation program to the WSL2 postgreSQL

Is the server running on host "localhost" (127.0.0.1) and accepting TCP/IP connections on port 5432

I install pgadmin4 using the following command sudo install pgadmin4 and then I installed postgresql using sudo install postgresql . In my terminal I ran the command psql -U postgres -h localhost
It shows connection refuse
Then I go to pgadmin4 and created a server and I wanted to connect to the server it shows
pgadmin connection refuse
Then I wrote host all all all md5 in pg_hba.conf file and I wrote listen_addresses = '*' to postgresql.conf file. and I wrote the following command sudo service postgresql restart but again It shows connection refuse message.
I wrote psql and it shows connections on Unix domain socket
Can you please help me in this regard? What I have missed?

PostgreSQL SSH port forwarding via Windows/PuTTY

I have PostgreSQL 9.4 running on a Linux VPS, and I need to be able to connect to it over SSH from both Linux and Windows clients. (I will later need to connect to multiple servers, and so that all clients use the same port numbers, I'm forwarding to port 5551 for the first server, then I will use 5552, 5553, etc.)
From a Linux client I just run ssh -fNg -L 5551:localhost:5432 user#remote1.com and connect to localhost:5551 with PGAdmin3 or any other client app. Works great.
On Windows, I'm using PuTTY and Pageant. I got the connection to user#remote1.com via terminal working, then I went to the SSH Tunnels and added L5432 localhost:5551. Terminal connection still works, but when I try to connect with PGAdmin3 to localhost:5551 I get an error:
could not connect to server: Connection refused (0x0000274AD/10061) Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5551?
I resolved it. Like many things, this is obvious in hindsight. I had things backward in the SSH Tunnels setup in PuTTY. It needs to be L5551 remote1.com:5432

Error Connecting to PostgreSQL on Red Hat Openshift gear

Permit my noobishness, I'm learning how to work with Linux Openshift. While following a book, I tried to connect to the server online from the command line [Bash]. I ran
rhc port-forward
and it displayed:
Checking available ports ... done
Forwarding ports ...
To connect to a service running on OpenShift, use the Local address
Service Local OpenShift
---------- -------------- ---- ---------------
httpd 127.0.0.1:8080 => 127.7.74.1:8080
postgresql 127.0.0.1:5432 => 127.7.74.2:5432
Press CTRL-C to terminate port forwarding
The book said I should run
psql -h 127.0.0.1 -p 5433 -U username password
Now why does bash complain anytime I run the command,
psql -h 127.0.0.1 ...
It says, psql command not found.
check and make sure that you have psql installed on your local machine. If not use a package manager like yum (fedora/RHEL) or Brew (Mac) to install the necessary binaries to get the psql command.
The problem was that my internet connection breaks off at some point while I am cloning to my local machine. Thank you all for you contribution. It needs a strong internet connection and it takes quite a time to clone: https://github.com/postgres/postgres.git.