Connect to database in docker container on remote host with pgadmin3 - postgresql

I'm trying to connect to a database running in a docker container on a remote host. I configured SSH-Tunnel in pgadmin3 with ip of the host and identity file. On Settings tab I inserted 172.18.0.2 (the container IP) as host. It is not possible to connect. pgadmin turns grey for a while and presents me a beautiful error message:
SSH error: Error when starting up SSH session with error code -8 [Unable to exchange encryption keys]
Do I miss something? Is it possible to connect to the container?

When I upgraded pgAdmin 1.20.0 to 1.22.1, I started getting the same error. pgAdmin 4 doesn't have support for any SSH tunnel either. So, the only option you have right now is .. revert back to 1.20.0. Unfortunately, if you're on PG9.5 or better, pgAdmin 1.20.0 doesn't support it.

If you require access to a Postgres 9.5 database, you can manually create the SSH tunnel, and then connect using pgAdmin3 by setting the host to localhost. On Linux or Mac, you can use the following: ssh -L 5432:<pg-host>:5432 <jump-host-ip-or-dns>. It doesn't seem likely that pgAdmin3 will receive any updates with the direction pgAdmin4 is heading.

Related

Connection problem with postgresql and docker

I'm running a composition of docker containers of a preexisting project on Ubuntu 20. One container for apache2, one for postresql. I can connect to the webserver through my browser and the webserver container establishes a connection to the postgresql container - so far everything works.
The problem is: I can't establish a connection from my host machine to the postgresql database using php. Whenever I try, I get an password authentication failed for user .. error.
I know that the php code, the username and the password are correct, as all three work when I execute them from within the webserver container.
$db_connection = pg_connect("host=db dbname=MYDB user=MYUSER password=MYPASS");
I configured /etc/hosts on my host to resolve "db" to 127.0.0.1 and also tried using host=localhost. Both didn't help.
Am I getting something wrong about how docker works? I found a couple of OSX solutions which were talking about docker containers running in virtual machines and therefore not being accessible from the host... Which is confusing, as apache2 is reachable from my host.
I did my research on stackoverflow tried solutions from e.g. Connecting to Postgresql in a docker container from outside but they didn't work for me.

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

Accessing WSL postgresql server from Windows PGadmin

I am running postgresql in WSL Ubuntu on windows. Everything is up to speed, my data is loaded and I wish to access the database through some graphical interface. I was thinking pgadmin4.
Is it possible to accomplish this through a windows install of pgadmin4? I installed pgadmin4 on windows and tried to connect the traditional way in the GUI through localhost but am not getting a connection. I figure there may be a special method here.
For everyone else stumbling across this: The best way to do this (that I know of) is to SSH into your local WSL and then configure the SSH in pgAdmin to that.
I have yet to figure out, how to use this remotely.
As long as postgres is running within your wsl2 instance (check with sudo service postgresql status) then within PgAdmin 4 (running in windows), all you need to do is click to "Register" a new server.
Then, while entering the Connection data, set host to localhost and port to 5432 (unless you specified unique port within your postgres instance when creating in wsl2.
See this answer for more detail
There is no special way needed, you should be able to get a connection. Just like your dev http ports are exposed to your browser on Windows, your db port should too.
I had to manually add localhost to pg admin though which is a bit weird.
Make sure your db service is up and running on Ubuntu, sometimes the db service is killed for no reason.
To see if your PostgresSQL service is up or not:
sudo service postgresql status
If it's not, start the service:
sudo service postgresql start

pgAdmin4: Unable to connect to Amazon EC2 via SSH Tunnel

I have Amazon EC2 instance running Ubuntu. I have installed and configured PostgreSQL.
Contents of the file /etc/postgresql/9.3/main/pg_hba.conf:
local all all md5
host all all 0.0.0.0/0 md5
Also in postgresql.conf
I have set listen_addresses='*'.
The test command below is successfully starting psql console.
psql -U postgres testdb
Now I am trying to connect pgAdmin4 from MacOS.
I have created a SSH tunnel with following command:
ssh -i ~/.ssh/test.pem -fN -L 5433:localhost:5432 ubuntu#mytestdomain.com
Now I have following details in pgAdmin:
When I save, I get this output:
Unable to connect to server: server closed the connection unexpectedly. This probably means the server terminated abnormally before or while processing the request.
What am I doing wrong ?
Here is the solution,
install pgadmin 4 into your system. and configure the below-added configurations. if the below configurations do not work then please check that DB user permissions on AWS. because of the restriction on IP level so it may not be able to access.

SSH-Tunnel support workaround for pgAdmin4?

After installing Vagrant/VirtualBox and connecting to the database via pgAdmin 3 using ssh tunnel, I am warned that it is not compatible with pgAdmin 3. I can still connect but after some time pgAdmin will seemingly randomly shut down. pgAdmin 4 does not have built-in support for ssh tunnel (and won't until version 2.0 comes out).
Is there a command line workaround to create an ssh tunnel in pgAdmin4?