title: Docker-compose Postgres connection refused
body: <p>I'm running Postgres DB with pg-admin and GO on the docker-compose.</p>
  <p><em><strong>Problem: I can connect from <code>pg-admin</code> to <code>Postgres</code>. But cannot establish a connection from <code>Go</code>.</strong></em></p>
  <p>I tried different combinations of authentication string but it does not work. String format same as here <a href="https://github.com/karlkeefer/pngr" rel="nofollow noreferrer">https://github.com/karlkeefer/pngr</a> - but different container name - <code>database</code></p>
  <p><strong>(ERROR) Connection URl:</strong></p>
  <pre><code>backend_1         | 2021/08/08 14:24:40 DB connection: database://main:fugZwypczB94m0LP7CcH@postgres:5432/temp_db?sslmode=disable
  backend_1         | 2021/08/08 14:24:40 Unalble to open DB connection: dial tcp 127.0.0.1:5432: connect: connection refused
  </code></pre>
  <p><em>(URI generation same as here <a href="https://github.com/karlkeefer/pngr" rel="nofollow noreferrer">https://github.com/karlkeefer/pngr</a>)</em></p>
  <p><strong>Docker:</strong></p>
  <pre><code>version: '3.8'
  services:
    backend:
      restart: always
      build: 
        context: backend
        target: dev
      volumes:
        - ./backend:/root
      ports:
        - &quot;5000:5000&quot;
      env_file: .env
      depends_on: 
        - database
    database:
      build: database
      restart: always
      environment:
        POSTGRES_DB: ${POSTGRES_DB}
        POSTGRES_USER: ${POSTGRES_USER}
        POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
        PGDATA: /var/lib/postgresql/data
      volumes:
        - ./database/data:/var/lib/postgresql/data
        - ./logs/databse:/var/log/postgresql
        - ./database/migrations:/docker-entrypoint-initdb.d/migrations
      ports:
        - &quot;5432:5432&quot;
    database-admin:
      image: dpage/pgadmin4:5.5
      restart: always
      environment:
        PGADMIN_DEFAULT_EMAIL: ${PG_ADMIN_EMAIL}
        PGADMIN_DEFAULT_PASSWORD: ${PG_ADMIN_PASSWORD}
        PGADMIN_LISTEN_PORT: 80
      ports:
        - &quot;8080:80&quot;
      volumes:
        - ./database/admin:/var/lib/pgadmin
      links:
        - &quot;database:pgsql-server&quot;
      depends_on: 
        - database
  volumes:
    database:
    database-admin:
  </code></pre>
  <p><strong>Environment:</strong></p>
  <pre><code>POSTGRES_HOST=postgres
  POSTGRES_PORT=5432
  POSTGRES_DB=temp_db
  POSTGRES_USER=main
  POSTGRES_PASSWORD=fugZwypczB94m0LP7CcH
  PG_ADMIN_EMAIL=admin@temp.com
  PG_ADMIN_PASSWORD=ayzi2ta8f1TnX3vKQSN1
  PG_ADMIN_PORT=80
  </code></pre>
  <p><strong>GO Code:</strong></p>
  <pre><code>db, err = sqlx.Open(&quot;postgres&quot;, str)
  </code></pre>
  <p><em><strong>str</strong></em></p>
  <pre><code>func buildConnectionString() string {
      user := os.Getenv(&quot;POSTGRES_USER&quot;)
      pass := os.Getenv(&quot;POSTGRES_PASSWORD&quot;)
      if user == &quot;&quot; || pass == &quot;&quot; {
          log.Fatalln(&quot;You must include POSTGRES_USER and POSTGRES_PASSWORD environment variables&quot;)
      }
      host := os.Getenv(&quot;POSTGRES_HOST&quot;)
      port := os.Getenv(&quot;POSTGRES_PORT&quot;)
      dbname := os.Getenv(&quot;POSTGRES_DB&quot;)
      if host == &quot;&quot; || port == &quot;&quot; || dbname == &quot;&quot; {
          log.Fatalln(&quot;You must include POSTGRES_HOST, POSTGRES_PORT, and POSTGRES_DB environment variables&quot;)
      }
  
      str := fmt.Sprintf(&quot;database://%s:%s@%s:%s/%s?sslmode=disable&quot;, user, pass, host, port, dbname)
  
      log.Println(&quot;DB connection: &quot; + str)
  
      return str
  }
  </code></pre>
  <p>Thanks in advance!</p>