MikeV
MikeV

Reputation: 657

Docker container to local osx postgres

I am trying to connect to a local postgres database (running in osx) from a docker container. I am running docker for mac. I've tried using the --add-host option but have not gotten it to work yet. Am I missing a config to make the connection? Below is an example of what I am attempting.

docker run --add-host=localbox:192.168.59.3 -it postgres /bin/bash

connection attempts

root@1893226613e9:/# psql -h localbox -U test_user
psql: could not connect to server: Connection timed out
    Is the server running on host "localbox" (192.168.59.3) and accepting
    TCP/IP connections on port 5432?


root@1893226613e9:/# ping localbox 
PING localbox (192.168.59.3): 56 data bytes 
^C--- localbox ping statistics --- 
7 packets transmitted, 0 packets received, 100% packet loss

Upvotes: 1

Views: 607

Answers (1)

MikeV
MikeV

Reputation: 657

Okay the comment from @warmoverflow lead me in the right direction. Here is what I had to do to get everything talking.

Initially I was using the wrong ip for --add-host. I had to use the following.

docker run --add-host=localbox:192.168.99.1 -it postgres /bin/bash

I got 192.168.99.1 by looking for the address virtualbox was using.

$ ifconfig
vboxnet0: flags=8943<UP,BROADCAST,RUNNING,PROMISC,SIMPLEX,MULTICAST> mtu 1500
        ether 0a:00:27:00:00:00
        inet 192.168.99.1 netmask 0xffffff00 broadcast 192.168.99.255

This got ping to work but then I was receiving a quick connection refused.

psql: could not connect to server: Connection refused
    Is the server running on host "192.168.99.1" and accepting
    TCP/IP connections on port 5432?

Then I had to enable postgres to accept remote connections. I did this by adding to the configs below.

/usr/local/var/postgres/postgresql.conf

listen_addresses = '*'

/usr/local/var/postgres/pg_hba.conf

host    all             all             192.168.99.100/32       trust

Upvotes: 2

Related Questions