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?
相关问题
- Docker task in Azure devops won't accept "$(pw
- Unable to run mariadb when mount volume
- Django distinct is not working
- PostgreSQL: left outer join syntax
- Unspecified error (0x80004005) while running a Doc
You can run Postgres this way (map a port):
So now you have mapped the port 5432 of your container to port 5432 of your server.
-p <host_port>:<container_port>
.So now your postgres is accessible from yourpublic-server-ip:5432
To test: Run the postgres database (command above)
Go inside your container and create a database:
Go to your localhost (where you have some tool or the psql client).
(password mysecretpassword)
So you're accessing the database (which is running in docker on a server) from your localhost.
In this post it's expained in detail.
I tried to connect from localhost (mac) to a postgres container. I changed the port in the docker-compose file from 5432 to 3306 and started the container. No idea why I did it :|
Then I tried to connect to postgres via PSequel and adminer and the connection could not be established.
After switching back to port 5432 all works fine.
This was my experience I wanted to share. Perhaps someone can make use of it.
I already had running postgres on host machine and didn't want to allow connections from network, so I did run temporary postgres instance in container and created database in just two lines:
You can also access through docker exec command also.
I managed to get it run on linux
1 first run the docker postgres, make sure the port is published, I use alpine because it's lightweight.
sudo docker run --rm -P -p 127.0.0.1:5432:5432 -e POSTGRES_PASSWORD="1234" --name pg postgres:alpine
2 use another terminal, access the database from the host using the postgres uri
psql postgresql://postgres:1234@localhost:5432/postgres
for mac users, replace psql with pgcli
In case, it is a django backend application, you can do something like this.