I have a postgres database in docker-compose.yaml
services:
...
db:
image: postgres:12.0-alpine
volumes:
- postgres_data:/var/lib/postgresql/data/
environment:
- POSTGRES_USER=username
- POSTGRES_PASSWORD=password
- POSTGRES_DB=dbname
ports:
- "5432:5432"
expose:
- 5432
I can connect to db like that:
docker exec -ti CONTAINER_NAME-db bash
bash-5.0# psql -U username -d dbname -h db
Password for user username:
psql (12.0)
Type "help" for help.
dbname=#
How to connect to the PostgreSQL database from outside the container???
I was trying:
1) psql -U username -d dbname -h db.
NO OUTPUT
2) psql -h localhost -p 5432 -U username -d dbname
psql: error: connection to server at "localhost" (127.0.0.1), port 5432 failed: FATAL: role "username" does not exist
3) psql -h 0.0.0.0 -p 5432 -U username -d dbname
psql: error: connection to server at "0.0.0.0", port 5432 failed: FATAL: role "username" does not exist
In my situation problem deals with postgres on localhost
Solution
brew services
brew services stop postgresql@15
After these commands I can connect to DB:
psql -h localhost -p 5432 -U username -d dbname