title: Can not connect to a postgres database in a docker container from my local machine
body: <p>I have a strange problem with docker and postgres. I can not connect from my local machine to psql inside the docker container.</p>
  <p>First of all I created my container with this command: <br />
  <code>docker run -d -p 5432:5432 --name my-postgres -e POSTGRES_PASSWORD=mysecretpassword postgres</code></p>
  <p>After that I executed the container with following command: <br />
  <code>docker exec -it my-postgres bash</code></p>
  <p>Inside the container I started psql with the postgres user: <br />
  <code>psql -U postgres</code> <br />
  And I created a new database: <br />
  <code>CREATE DATABASE mytestdb;</code></p>
  <p>Everything works inside the container, but if I want to connect to it from my machine it does only show the database from my machine. I tried this to connect to postgres: <br />
  <code>psql -h localhost -p 5432 -U postgres</code></p>
  <p>Which should me show something like this:</p>
  <pre><code>                                 List of databases
     Name    |  Owner   | Encoding |  Collate   |   Ctype    |   Access privileges   
  -----------+----------+----------+------------+------------+-----------------------
   mytestdb  | postgres | UTF8     | en_US.utf8 | en_US.utf8 | 
   postgres  | postgres | UTF8     | en_US.utf8 | en_US.utf8 | 
   template0 | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =c/postgres          +
             |          |          |            |            | postgres=CTc/postgres
   template1 | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =c/postgres          +
             |          |          |            |            | postgres=CTc/postgres
  (4 rows)
  </code></pre>
  <p>But instead I get this:</p>
  <pre><code>                                          List of databases
        Name       |      Owner      | Encoding | Collate | Ctype   |        Access privileges          
  -----------------+-----------------+----------+---------+---------+----------------------------------
   user            | user            | UTF8     | C       | C       | 
   postgres        | user            | UTF8     | C       | C       | 
   template0       | user            | UTF8     | C       | C       | =c/user                 +
                   |                 |          |         |         | user =CTc/user
   template1       | user            | UTF8     | C       | C       | =c/user                 +
                   |                 |          |         |         | user =CTc/user
  (4 rows)
  
  (END)
  </code></pre>
  <p>In every tutorial, I saw they did it, but for me, it doesn't work. So please help me.</p>