Can't connect to database by pgcli

It works for me. Below, I am using host.docker.internal to connect to the host’s localhost from within the container in which I install pgcli, to allow it to connect to the database container like if I were connecting from the host directly. (I hope Docker is not being smart about that.)

But otherwise it should work the same:

docker run -it --rm python /bin/bash

root@fea386fdb294:/# pip install pgcli
<log removed>

root@fea386fdb294:/# pgcli -h host.docker.internal -p 5432 -u root -d ny_taxi
Password for root:
Server: PostgreSQL 13.8 (Debian 13.8-1.pgdg110+1)
Version: 3.4.1
Home: http://pgcli.com

root@host:ny_taxi> \q
Goodbye!

root@fea386fdb294:/# exit

Rather than using host.docker.internal, I could also have used --network="host" along with locahost, like:

docker run -it --rm --network="host" python /bin/bash
root@docker-desktop:/# pip install pgcli
<log removed>

root@docker-desktop:/# pgcli -h localhost -p 5432 -u root -d ny_taxi
Password for root: 
Server: PostgreSQL 13.8 (Debian 13.8-1.pgdg110+1)
Version: 3.4.1
Home: http://pgcli.com
root@localhost:ny_taxi> \q
Goodbye!

root@docker-desktop:/# exit

For the latter, using the wrong password gives me the same error like you saw when using pgcli as installed on the host:

root@docker-desktop:/# pgcli -h localhost -p 5432 -u root -d ny_taxi
Password for root: 
could not connect to server: Connection refused
	Is the server running on host "localhost" (::1) and accepting
	TCP/IP connections on port 5432?
FATAL:  password authentication failed for user "root"

For the first option, on macOS with the latest Docker Desktop, the error is a bit different:

root@fea386fdb294:/# pgcli -h host.docker.internal -p 5432 -u root -d ny_taxi
Password for root: 
FATAL:  password authentication failed for user "root"

Regardless, both work fine when using the proper credentials. So, I’d say that using pgcli installed on the host (rather than a temporary container just for my testing), should work when providing the correct password.

Aside, it seems you somehow copied a multi-line command into a single-line command, but still have the \ line continuation characters in there, which are now escaping the spaces that follow those. Should not cause your problem though.

1 Like