in my docker-compose i have such config:
services:
postgres_db:
image: postgres:14.1-alpine
restart: always
cap_add:
- SYS_NICE
volumes:
- "./setup.sql:/docker-entrypoint-initdb.d/setup.sql"
ports:
- "9906:5432"
environment:
<<: *common-variables
POSTGRES_USER: postgres
POSTGRES_HOST: localhost
from what i understand after running docker-compose up
i should get postgres server listening on postgresql://postgres:postgres@localhost:9906
however in the logs what i see is this
hess-postgres_db-1 | PostgreSQL Database directory appears to contain a database; Skipping initialization
chess-postgres_db-1 |
chess-postgres_db-1 | 2023-04-13 21:31:17.726 UTC [1] LOG: starting PostgreSQL 14.1 on aarch64-unknown-linux-musl, compiled by gcc (Alpine 10.3.1_git20211027) 10.3.1 20211027, 64-bit
chess-postgres_db-1 | 2023-04-13 21:31:17.726 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
chess-postgres_db-1 | 2023-04-13 21:31:17.726 UTC [1] LOG: listening on IPv6 address "::", port 5432
chess-postgres_db-1 | 2023-04-13 21:31:17.730 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
chess-postgres_db-1 | 2023-04-13 21:31:17.734 UTC [21] LOG: database system was shut down at 2023-04-13 21:31:12 UTC
chess-postgres_db-1 | 2023-04-13 21:31:17.738 UTC [1] LOG: database system is ready to accept connections
postgres still listens on 5432 which is the port that my local postgres is also using. when my app calls localhost:9906
absolutely nothing is there
How can i spin up the container and get the db under postgresql://postgres:postgres@localhost:9906
?
2
Answers
That is correct. It seems for Postgres, that it is running on 5432 but inside of the docker-container.
With
9906:5432
you setup a rule which redirects all traffic of your docker-host (normally your PC) on port 9906 to the postgres-containers port 5432.This will only work, when you try to call your database from your localhost (not from a docker container).
If you want to change that port, then search for "postgres change port". Here is one result: https://portal.perforce.com/s/article/691
But … I think you it is not a good idea to change that port. With docker, all is perfect isolated and you can have multiple postgres-instances on the same port. They are isolated.
I guess your app is also a docker-container. From there you can access your database just with
postgresql://postgres_db:5432
.And you have to remove the
POSTGRES_HOST: localhost
line. The default-setting is fine.This depends on your network mode in docker compose.
If your app also in this docker compose then you can directly use postgres_db:5432 like in the example below
But if your app outside of network postgres_db – it can access the db like that
BTW
network mode: host
in docker compose will ignore host and port mapping. Postgres will be available from initial postgres port "5432" on docker-compose hostAnd still there possible of troubles if your app in some network which can’t access docker network by some reason (like app on windows host, when docker inside of the WSL) then here are exists other workarounds