Dockerizing PostgreSQL - psql Connection refused

To get IP address of your running container you can:

docker inspect pg_test | grep IPAddress

and then use it instead of 'localhost'.

Where pg_test is container name received from

docker ps

If you add the --publish option to the docker run command

docker run --rm -P --publish 127.0.0.1:5432:5432 --name pg_test eg_postgresql 

when you run the docker file, then the following will work (note the port is now 5432)

psql -h localhost -p 5432 -d docker -U docker --password

Running this on my mac worked for me:

 $ boot2docker ip
 The VM's Host only interface IP address is: 192.168.59.103

And then connect along the lines of:

 $ psql -h 192.168.59.103 -p 49159 -d docker -U docker --password

It's less than ideal to have to do this all, but the instructions at https://docs.docker.com/installation/mac/ indicate it is the correct solution if you want to connect directly from you mac.


Solution for me was simply using host.docker.internal instead of localhost in your connection string.

https://github.com/npgsql/Npgsql.EntityFrameworkCore.PostgreSQL/issues/225