Connecting to Postgresql in a docker container from outside

后端 未结 14 1819
太阳男子
太阳男子 2020-11-29 14:42

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?

相关标签:
14条回答
  • 2020-11-29 15:06

    In case, it is a django backend application, you can do something like this.

    docker exec -it container_id python manage.py dbshell
    
    0 讨论(0)
  • 2020-11-29 15:06

    docker ps -a to get container ids then docker exec -it psql -U -W

    0 讨论(0)
  • 2020-11-29 15:07

    For some reason 5432 port seems protected. I changed my port config from 5432:5432to 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>
    
    0 讨论(0)
  • 2020-11-29 15:09

    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
    
    0 讨论(0)
  • 2020-11-29 15:11

    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).

    0 讨论(0)
  • 2020-11-29 15:16

    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.

    0 讨论(0)
提交回复
热议问题