0

I am trying to connect to a Postgres instance running in a Docker container. In the docker-compose file, the postgres service looks like this:

    flask-api-postgres:
        container_name: flask-api-postgres
        image: postgres:13.4-alpine
        env_file:
            - dev.env
        ports:
            - "5433:5433"
        networks:
            flask-network:

With docker inspect I get that the container has the address: 172.19.0.2. The API works fine, but when trying to access the database from Pgadmin with the config shown in the image (user and password are correctly set), I get the shown error. Pgadmin config

I do not know how to access the postgres instance from pgadmin.

2 Answers 2

1

One approach is you can access the postgres db docker container from pgadmin which is hosted in your host machine using 127.0.0.1 instead of 172.19.0.2

Another way is you can create another container for pgadmin. In this case, you can access your PostgreSQL using container IP (For example: 172.19.0.2). Add this to your docker-compose file

  pgadmin:
    image: dpage/pgadmin4
    depends_on:
      - flask-api-postgres
    ports:
      - "5050:80"
    environment:
      PGADMIN_DEFAULT_EMAIL: [email protected]
      PGADMIN_DEFAULT_PASSWORD: admin
    restart: unless-stopped
    networks:
        flask-network:

Make sure both are under same network.

Sign up to request clarification or add additional context in comments.

Comments

0

Please check the port you are using. The default is 5432.

See experiment:

> docker ps
CONTAINER ID   IMAGE             COMMAND                  CREATED          STATUS          PORTS                              NAMES
0c4d92a623a6   postgres:latest   "docker-entrypoint.s…"   14 minutes ago   Up 14 minutes   5432/tcp, 0.0.0.0:5433->5433/tcp   cannot-access-postgres-instance-running-in-docker-container-from-pgadmin-database-1
> docker exec -it 0c4d92a623a6 sh
# psql "host=127.0.0.1 port=5433"
psql: error: connection to server at "127.0.0.1", port 5433 failed: Connection refused
    Is the server running on that host and accepting TCP/IP connections?
# psql "host=127.0.0.1 port=5432"
psql: error: connection to server at "127.0.0.1", port 5432 failed: FATAL:  role "root" does not exist
#

4 Comments

Thanks for the response. I actually have I bridge network, but when trying to access the database via the adress 127.0.0.1, I get the following error: unable to connect to server: connection to server at "127.0.0.1", port 5433 failed: server closed the connection unexpectedly This probably means the server terminated abnormally before or while processing the request. However, the postgres instance in the container is still running flawlessly.
@AntonioMoreno I've edited my answer, please check. If doesn't work you could debug further starting from docker and going to the host machine. Check if postgres is running and what port it listens on.
I had previously tried to use port 5432 but it would not let me. I have figured out that there was already a postgres instance running there. So now, when running the containter on 5432, I am able to connect to the postgres instance from Pgadmin using 127.0.0.1 as you said. Thank you so much!
no worries @AntonioMoreno! good luck! Please accept the answer, so the question won't appear in a list of unanswered ones.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.