I have Postgresql on a server in a docker container. How can I connect to it from the outside, that is, from my local computer? What setting should I apply to allow that?
In case, it is a django backend application, you can do something like this.
docker exec -it container_id python manage.py dbshell
docker ps -a
to get container ids then
docker exec -it psql -U -W
For some reason 5432 port seems protected. I changed my port config from 5432:5432
to 5416:5432
and the following command worked to connect to your postgres database from outside its docker container:
psql -h localhost -p 5416 -U <my-user> -d <my-database>
To connect from the localhost you need to add '--net host':
docker run --name some-postgres --net host -e POSTGRES_PASSWORD=mysecretpassword -d -p 5432:5432 postgres
You can access the server directly without using exec from your localhost, by using:
psql -h localhost -p 5432 -U postgres
There are good answers here but If you like to have some interface for postgres database management, you can install pgAdmin on your local computer and connect to the remote machine using its IP and the postgres exposed port (by default 5432).
I know this is late, if you used docker-compose like @Martin
These are the snippets that helped me connect to psql inside the container
docker-compose run db bash
root@de96f9358b70:/# psql -h db -U root -d postgres_db
I cannot comment because I don't have 50 reputation. So hope this helps.