Connect to postgres db in docker on host from guest vagrant box - postgresql

Not sure if this is possible, but I have a Vagrant box running ubuntu 18.04 running in VirtualBox on my macbook. I also have a docker container running on my mac which has a postgres database in it.
I would like to be able to connect from within ubuntu to the database in docker.
My first attempt at this was to add the following to my VagrantFile
config.vm.network "forwarded_port", guest: 5432, host: 5432
However that resulted in the following error:
Vagrant cannot forward the specified ports on this VM, since they
would collide with some other application that is already listening
on these ports. The forwarded port to 5432 is already in use
on the host machine.
To fix this, modify your current project's Vagrantfile to use another
port. Example, where '1234' would be replaced by a unique host port:
config.vm.network :forwarded_port, guest: 5432, host: 1234
Sometimes, Vagrant will attempt to auto-correct this for you. In this
case, Vagrant was unable to. This is usually because the guest machine
is in a state which doesn't allow modifying port forwarding. You could
try 'vagrant reload' (equivalent of running a halt followed by an up)
so vagrant can attempt to auto-correct this upon booting. Be warned
that any unsaved work might be lost.
I understand the error, it makes perfect sense because the host is already listening to port 5432 (or rather the docker/postgres container is). but i don't know how to fix the problem. If i do as suggested and change the host port number it would not work because then I wouldn't be connecting to the right port.
Suggestions?

I figured it out. it's pretty easy actually.
You see, the docker postgres db is availabe at 'localhost' or '127.0.0.1' on the HOST machine, but the HOST machine appears at 10.0.2.2 for some reason to the GUEST machine. All i had to do was point my database connect script to 10.0.2.2 and no extra network config was needed.

Related

postgres doesnt restart after making the necessary .conf file changes

apologies first. On a steep learning curve more like a wall and will be verbose and lacking in jargon but too old to change that now. I'm trying to access a Postgresql-13 (with postgis-3 extensions) database from a machine other than the one where it is hosted. Before doing anything at all other than install, create and consume a spatial data file, this is the partial screen dump from
sudo netstat -ltpn
Proto
Recv-Q
Send-Q
Local Address
Foreign Address
State
PID/Program name
tcp
0
0
127.0.0.1:5432
0.0.0.0:*
LISTEN
993/postgres
... and the database is accessible from pgadmin4 on the Ubuntu 20.04 machine which is hosting it. I can also connect to the database in QGIS on the same Ubuntu machine through 127.0.0.1 and port 5432. Nothing special about that other than knowing it works. What I want to do is connect to that database from any machine running QGIS (or another GIS platform which can consume postgis).
Port forwarding rules on the router are set for 80, 8080 and 5900 to point to 10.0.0.55 which is the IP for the Ubuntu 20.04 which is hosting. I have a dynamic DNS pointing the routers IP to give it the name http://blah.blah.net (not actually that, but close).
pgadmin4 is installed and configured to run in server and I can access the database through pgadmin4 from the host or any other computer using http://blah.blah.net/pgadmin4. Not sure I got this config quite right but it works. Geoserver is also running smoothly on Tomcat9 accessible from anywhere through http://blah.blah.net:8080/Geoserver/web/. And a fancy front end in the making on http://blah.blah.net.
Have then done the following mods to the two .conf files in etc/postgresql/13/main which are well noted and documented already ...
host all all 0.0.0.0/0 md5
in the pg_hba.conf
and also ...
listen_addresses='*' (and removed the leading #)
port = 5432 (was already)
in the postgresql.conf
Then restarted postgres with ...
sudo service postgresql restart
... and then port 5432 disappears completely and the database is not accessible from anywhere. Not even the host computer. Checked that postgresql was running with ...
sudo systemctl status postgresql
... which it appears to be. But am getting nothing from QGIS or pgadmin4. Not even from the host machine.
Got a lot of questions but the most obvious is am I missing something? Not sure if I need a rule at the router for port 5432 - did try that in a similar fashion to the rules above but it didn't change anything. Appreciate any help. Cheers ... R
The local address in the netstat output:
127.0.0.1:5432
indicates that PostgreSQL is only listening on the loopback interface. Since you changed listen_addresses in postgresql.conf, I conclude that you forgot to restart PostgreSQL.
To confirm, run this query:
SELECT setting, pending_restart
FROM pg_settings
WHERE name = 'listen_addresses';
It should either show the wrong value (if you didn't reload or changed the wrong file altogether) or show pending_restart as TRUE (if you reloaded, but didn't restart).

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

Docker container, running PostgreSQL not allowing php's pg_connect from another container

I have the following architecture:
A network with 3 containers:
container_db, running PostgreSQL
container_pg_admin, running a simple pgAdmin environment
container_php_dev, running the whole environment, needed for the application to function properly (nginx, php, angular for the frontend, etc.)
When I try to pg_connect() from container_php_dev to container_db the connection gets refused. This is the actual error message I get: pg_connect(): Unable to connect to PostgreSQL server: could not connect to server: No route to host↵ Is the server running on host "container_db" (172.18.0.4) and accepting↵ TCP/IP connections on port 5432?.
However, when I try to connect to container_db from container_pg_admin I face no problem achieving this (host is container_db and port is 5432). Even if I map port 5432 of container_db to 5555 on host, I'm able to connect to 127.0.0.1:5555 from DBeaver or other DB Manager (TeamSQL).
All containers were restarted a few times as well as docker itself. And as this is my development machine, the host PC was restarted as well.
What might be the problem, how to diagnose and possibly solve? Have any of you experienced such strange docker behaviour?
Try running the container_db in the hosted network using flag --network host

Can't connect to PostgreSQL server running in Vagrant from host

There are similar questions but nothing worked for me. I have already added this line to my pg_hba.conf:
host all all all trust
And this one to my postgresql.conf:
listen_addresses = '*'
My networking settings in the Vagrantfile are:
config.vm.network "private_network", ip: "10.0.0.0"
config.vm.network :forwarded_port, guest: 3000, host: 3000, auto_correct: true
When I try to connect from the host, I get:
$ psql -h 10.0.0.0 -U <username> -d <database>
psql: could not connect to server: Permission denied
Is the server running on host "10.0.0.0" and accepting
TCP/IP connections on port 5432?
The same command from the guest works with no problems. What am I missing??
[UPDATE]
I changed the private network ip to "192.168.1.77" (got it from a working example) and it worked. Still don't know why 10.0.0.0 wasn't good though, since it is in the reserved private address space, so I'll leave the question unanswered.
You need to forward the postgres port, you're only forwarding 3000, not 5432.
I believe the reason you could not connect is that technically 10.0.0.0 is the network address of the range 10.0.0.0/8 (and 10.255.255.255 is the broadcast address and should also not work). The first address of each range is the network address and is not allowed to be a routable host (similarly 192.168.0.0 should fail in the same way since it's a 16-bit block).
In addition to forwarding port 5432 to another port on my host machine in my Vagrantfile, I added the following to my pg_hba.conf file in my vagrant machine, restarted postgresql, and it finally allowed me to connect from the host:
host all all 0.0.0.0/0 md5
I found the suggestion here: https://github.com/laravel/framework/issues/11339

How can I access Postgresql hosted in Vagrant as a guest in Windows?

I am trying to connect to Postgresql using the PGAdmin III client in Windows 8.1. Postgresql is installed in a local copy of Vagrant (Ubuntu 14.04) on my Windows machine. It's up and running on Vagrant:
LISTENING
tcp 0 0 127.0.0.1:5432 0.0.0.0:* LISTEN 814/postgres
VERIFIED USER/PASS/LOCAL CONNECTION
I can access Postgresql locally in Vagrant via SSH in Windows:
vagrant#precise32:/etc/postgresql/9.1/main$ psql -h localhost testdb myuser
Password for user myuser:
psql (9.1.15)
SSL connection (<removed)
Type "help" for help.
testdb=> \quit
PG_HBA.CONF
I added this to my pg_hba.conf file:
# IPv4 local connections:
host all all 127.0.0.1/32 md5
host all all <my IP> md5
VAGRANT CONFIG
My Vagrant config is set to port forward to 5432:
# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine. In the example below,
# accessing "localhost:8080" will access port 80 on the guest machine.
config.vm.network "forwarded_port", guest: 5432, host: 15432
POSTGRESQL.CONF
And my postgresql.conf file is set to listen on all IP's:
#listen_addresses = '*' # what IP address(es) to listen on;
PGADMIN ERROR
So, what am I missing here when I try to connect as a guest via PGAdmin to the host and I get the following message, which indicates it sees it but something is not letting me through?:
An error has occurred:
Error connecting to the server: server closed the connection unexpectedly
This probably means the server terminated abnormally before or while processing the request.
PGADMIN CONNECTION INFO
Host: localhost
Port: 15432
Service: <blank>
Maintenance DB: postgres
Username: Myuser (verified)
Password: ****** (verified)
Store password:
Colour: <blank>
Group: Servers
In your Vagrant config add a IP (if not set) eg:
config.vm.network :forwarded_port, host: 15432, guest: 5432
config.vm.network :private_network, ip: "192.168.111.222"
Now from PGAdmin in Windows connect to host 192.168.111.222, port 5432.
Worked for me although I'm not know why.. :P
I dont know what you mean with in pg_hba.conf but in vagrant enviroment you should use ip like 10.0.2.2/24 instead your machine network address.
I had the same problem in linux, and i think in windows this can happen too. In my postgresql.conf the port variable was defined in two places. First with 5432 value, and second with 5435 value.
Running telnet vm_ip 5435, i was able to connect to the server running in the vm.
In my case my vm was running with public_network option, so i have a external ip. In this case, you don't need to forward a port, once you will access the vm,for example, with 192.168.60.15:5435 address.