How can I connect to postgres in docker from a host machine?
docker-compose.yml
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
docker-compose ps
Name Command State Ports
-----------------------------------------------------------------------------------------------------
appname_app_1 /bin/sh -c /app/start.sh Up 0.0.0.0:8060->80/tcp
appname_appdb_1 docker-entrypoint.sh /usr/ ... Up 22/tcp, 0.0.0.0:5432->5432/tcp
From container I can connect successfully. Both from app container and db container.
List of dbs and users from running psql inside container:
# 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)
DB image is not official postgres image. But Dockerfile in GitHub seem looking fine.
cat /var/lib/postgresql/data/pg_hba.conf from DB container:
# 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
I tried both users with no luck
$ 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
Looks like on my host machine there is already PSQL running on that port. How can I check it?