title: Postgres in Docker port matching
body: <p>I need to run several equals Postgres containers on different ports via docker-compose. My problem is that I do not understand how to connect from my terminal to container's psql.</p>
  <p>First of all, I should say that my local Postgres is off, so 5432 is free. Also I have <code>listen_addresses = '*'</code> in my containers.</p>
  <p>Here is list of my attempts (to make it easier, I've posted examples for one Postgres container):</p>
  <ol>
  <li>Run without port specifying</li>
  </ol>
  <p>docker-compose.yml:</p>
  <pre><code>    version: &quot;3&quot;
      services:
          postgres:
              build: 
                  context: ../../
                  dockerfile: db-load-test/sharding-benchmark/Dockerfile
              environment: 
                  - POSTGRES_PASSWORD=postgres
  </code></pre>
  <p>check port:</p>
  <pre><code>$ docker-compose ps
  
              Name                           Command              State    Ports  
  --------------------------------------------------------------------------------
  sharding-benchmark_postgres_1   docker-entrypoint.sh postgres   Up      5432/tcp
  </code></pre>
  <p>try to connect:</p>
  <pre><code>$ psql -p 5432 -U postgres
  psql: could not connect to server: No such file or directory
          Is the server running locally and accepting
          connections on Unix domain socket &quot;/var/run/postgresql/.s.PGSQL.5432&quot;?
  </code></pre>
  <pre><code>$ psql -h localhost -p 5432 -U postgres
  psql: could not connect to server: Connection refused
          Is the server running on host &quot;localhost&quot; (127.0.0.1) and accepting
          TCP/IP connections on port 5432?
  </code></pre>
  <pre><code>$ psql -h 0.0.0.0 -p 5432 -U postgres
  psql: could not connect to server: Connection refused
          Is the server running on host &quot;0.0.0.0&quot; and accepting
          TCP/IP connections on port 5432?
  </code></pre>
  <ol start="2">
  <li>Specify port and expose it in .yaml (I've changed 5432 to 5433 to make it more expressive)</li>
  </ol>
  <pre><code>$ docker-compose ps
              Name                           Command              State                          Ports                        
  ----------------------------------------------------------------------------------------------------------------------------
  sharding-benchmark_postgres_1   docker-entrypoint.sh postgres   Up      5432/tcp, 0.0.0.0:49162-&gt;5433/tcp,:::49162-&gt;5433/tcp
  </code></pre>
  <p>Similar attempts to connect was unsuccessful. Also it's unclear to my why Postgres still listen 5432:</p>
  <pre><code>postgres_1  | 2021-08-02 12:49:01.394 UTC [1] LOG:  listening on IPv4 address &quot;0.0.0.0&quot;, port 5432
  postgres_1  | 2021-08-02 12:49:01.394 UTC [1] LOG:  listening on IPv6 address &quot;::&quot;, port 5432
  postgres_1  | 2021-08-02 12:49:01.397 UTC [1] LOG:  listening on Unix socket &quot;/var/run/postgresql/.s.PGSQL.5432&quot;
  </code></pre>
  <ol start="3">
  <li>Use docker directly</li>
  </ol>
  <p><code>docker run --rm -it -e POSTGRES_PASSWORD=postgres -p 5432:5432/tcp postgres:11</code>
  It works with <code>psql -h localhost -p 5432 -U postgres</code>, but if I use docker run with <code>5432:5433</code> it breaks, which is unclear to me.</p>