Connecting to Postgresql in a docker container from outside

后端 未结 14 1818
太阳男子
太阳男子 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 14:53

    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:

    # Run PostgreSQL
    docker run --name postgres-container -e POSTGRES_PASSWORD=password -it -p 5433:5432 postgres
    
    # Create database
    docker exec -it postgres-container createdb -U postgres my-db
    
    0 讨论(0)
  • 2020-11-29 14:54

    I am using django with postgres in Docker containers. in the docker-compose file, add the following:

    db:
        image: postgres:10-alpine
        environment:
            - POSTGRES_DB=app
            - POSTGRES_USER=postgres
            - POSTGRES_PASSWORD=supersecretpassword
        **ports:
            - "6543:5432"**
    

    which will add accessible port by your local machine. for myself, I connected DBeaver to it. this will prevent port clashes between your app request and local machine request. at first, I got a message saying that the port 5432 is in use (which is by django app) so I couldn't access by pgAdmin or DBeaver.

    0 讨论(0)
  • 2020-11-29 14:54

    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.

      db:
        image: postgres
        ports:
          - 5432:5432
        restart: always
        volumes:
          - "db_sql:/var/lib/mysql"
        environment:
          POSTGRES_USER: root
          POSTGRES_PASSWORD: password
          POSTGRES_DB: postgres_db
    

    This was my experience I wanted to share. Perhaps someone can make use of it.

    0 讨论(0)
  • 2020-11-29 14:56

    You can also access through docker exec command by:

    $ docker exec -it postgres-container bash
    
    # su postgres
    
    $ psql
    

    Or

    $ docker exec -it postgres-container psql -U postgres
    
    0 讨论(0)
  • 2020-11-29 14:57

    I managed to get it run on linux

    1. 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. using 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

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

    I'm assuming that you want to be able to view data present in your container everytime you connect to it from outside. To do this, you will have to persist data on the postgres image.

    If you dont have persistant data, you will have to repeat everything you did the first time.
    Steps 3, 5, 6, 7, and 8 answer your question directly.

    Here is the detailed overview of the entire process I followed on Windows 10 powershell (commands are the same in Linux and macOS as well):

    Step 1: Start powershell in non-admin mode

    Step 2: Download postgres docker image:
    docker pull postgres:latest

    Step 3: Start docker container in detached mode and persist data on postgres image by creating a volume and binding it to a destination
    (Note: by default 5432 is the default port that is used; but state it explicitly to prevent connection errors from clients like pgadmin, dbeaver, etc.)
    docker run --name postgres-test -e POSTGRES_PASSWORD=password -p 5432:5432 -v postgres-data:/var/lib/postgresql/data -d postgres:latest

    Step 4: Check status of running containers
    docker ps -a

    Step 5: Go inside container_name in interactive mode
    (Note: commands like ls, pwd, etc. can be executed here if you've checked linux containers during installation)
    docker exec -it postgres-test psql -U postgres

    Step 6: Create sample data. At this point, you can play with psql commands in the following manner:

    # CREATE DATABASE test;
    # \c test
    # CREATE TABLE test_table(something int);
    # INSERT INTO test_table VALUES (123);
    # SELECT * FROM test_table;
    # \q
    

    Step 7: Open a database client application like pgadmin or dbeaver and enter the below in the connection fields:

    Host: localhost
    Database: test
    User: postgres
    Password: password
    

    Step 8: Enter the query select * from test_table in the query editor and you should be able to see the output 123

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