title: Connecting from psycopg2 on local machine to PostgreSQL db on Docker
body: <p>I have used the following commands to create a Docker image with Postgres running on it: </p>
  
  <p><code>docker pull postgres
  docker run --name test-db -e POSTGRES_PASSWORD=my_secret_password -d postgres</code></p>
  
  <p>I then created a table called test and inserted some random data into a couple of rows. 
  I am now trying to make a connection to this database table through psycopg2 in Python on my local machine.
  I used the command <code>docker-machine ip default</code> to find out the IP address of the machine as 192.168.99.100 and am using the following to try and connect:</p>
  
  <pre><code>conn = psycopg2.connect("dbname='test-db' user='postgres' host='192.168.99.100' password='my_secret_password' port='5432'")
  </code></pre>
  
  <p>This is not working with the error message of "OperationalError: could not connect to server: Connection refused (0x0000274D/10061)"</p>
  
  <p>.
  .</p>
  
  <p>Everything seems to be in order so I can't think why this would be refused.
  According to the documentation for this postgres image, (at <a href="https://hub.docker.com/_/postgres/" rel="nofollow">https://hub.docker.com/_/postgres/</a>) this image includes EXPOSE 5432 (the postgres port) and the default username is postgres.</p>
  
  <p>I also tried to get the IP address of the image itself with <code>docker inspect test-db | grep IPAddress | awk 'print{$2}' | tr -d '",'</code> that I found on SA to a slightly related article, but that IP address didn't work either.</p>