title: Connect to postgres in docker container from host machine
body: <p>How can I connect to postgres in docker from a host machine?</p>
  
  <p>docker-compose.yml</p>
  
  <pre><code>version: '2'
  
  networks:
      database:
          driver: bridge
  services:
      app:
          build:
              context: .
              dockerfile: Application.Dockerfile
          env_file:
              - docker/Application/env_files/main.env
          ports:
              - "8060:80"
          networks:
             - database
          depends_on:
              - appdb
  
      appdb:
          image: postdock/postgres:1.9-postgres-extended95-repmgr32
          environment:
              POSTGRES_PASSWORD: app_pass
              POSTGRES_USER: www-data
              POSTGRES_DB: app_db
              CLUSTER_NODE_NETWORK_NAME: appdb
              NODE_ID: 1
              NODE_NAME: node1
          ports:
              - "5432:5432"
          networks:
              database:
                  aliases:
                      - database
  </code></pre>
  
  <p>docker-compose ps</p>
  
  <pre><code>           Name                          Command               State               Ports
  -----------------------------------------------------------------------------------------------------
  appname_app_1     /bin/sh -c /app/start.sh         Up      0.0.0.0:8060-&gt;80/tcp
  appname_appdb_1   docker-entrypoint.sh /usr/ ...   Up      22/tcp, 0.0.0.0:5432-&gt;5432/tcp
  </code></pre>
  
  <p>From container I can connect successfully. Both from app container and db container.</p>
  
  <p>List of dbs and users from running psql inside container:</p>
  
  <pre><code># psql -U postgres
  psql (9.5.13)
  Type "help" for help.
  
  postgres=# \du
                                         List of roles
      Role name     |                         Attributes                         | Member of
  ------------------+------------------------------------------------------------+-----------
   postgres         | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
   replication_user | Superuser, Create role, Create DB, Replication             | {}
   www-data         | Superuser                                                  | {}
  
  postgres=# \l
                                         List of databases
        Name      |      Owner       | Encoding |  Collate   |   Ctype    |   Access privileges
  ----------------+------------------+----------+------------+------------+-----------------------
   app_db         | postgres         | UTF8     | en_US.utf8 | en_US.utf8 |
   postgres       | postgres         | UTF8     | en_US.utf8 | en_US.utf8 |
   replication_db | replication_user | 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
  (5 rows)
  </code></pre>
  
  <p>DB image is not official postgres image. But Dockerfile in GitHub seem looking fine.</p>
  
  <p>cat /var/lib/postgresql/data/pg_hba.conf from DB container:</p>
  
  <pre><code># TYPE  DATABASE        USER            ADDRESS                 METHOD
  
  # "local" is for Unix domain socket connections only
  local   all             all                                     trust
  # IPv4 local connections:
  host    all             all             127.0.0.1/32            trust
  # IPv6 local connections:
  host    all             all             ::1/128                 trust
  # Allow replication connections from localhost, by a user with the
  # replication privilege.
  #local   replication     postgres                                trust
  #host    replication     postgres        127.0.0.1/32            trust
  #host    replication     postgres        ::1/128                 trust
  
  host all all all md5
  host replication replication_user 0.0.0.0/0 md5
  </code></pre>
  
  <p>I tried both users with no luck</p>
  
  <pre><code>$ psql -U postgres -h localhost
  psql: FATAL:  role "postgres" does not exist
  $ psql -h localhost -U www-data appdb -W
  Password for user www-data:
  psql: FATAL:  role "www-data" does not exist
  </code></pre>
  
  <p>Looks like on my host machine there is already PSQL running on that port. How can I check it?</p>