skip to Main Content

I am trying to run databes on Docker container and connect it with my code.

This is my connection string:

engine = create_engine('postgres+psycopg2://name:password@hostname:port/database_name')

As host_name I use IP address of my Linux where I installed docker container. I also checked if port on my machine is same in docker container. Ports are set to 5101:5101.

After trying to connect this is error that is shown:

sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) connection to server at "IP of machine", port 5101 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections?

Here is the Docker command I used:

docker run --name rule-creator-dat -e POSTGRES_USER=admin -e POSTGRES_PASSWORD=secret -p 10.10.50.8:5101:5101 -v /data:/var/lib/postgresql/data -d postgres

(IP address is just example of my server)

Does somebody know what is the problem?

2

Answers


  1. Chosen as BEST ANSWER

    It looks like that when ports are mapped in docker file port inside of the docker for PostgreSQL has to be 5432. So the mapping should look -p 5101:5432.


  2. If you’re having trouble connecting SQL Alchemy to a PostgreSQL server running inside a Docker container and getting a "connection refused" error, there are a few things you can check.
    First, make sure that the Docker container and PostgreSQL service are running properly. Verify that the port mapping between the container and your computer is set up correctly. Double-check the IP address you’re using to connect to the container, and ensure that there are no firewall restrictions blocking the connection. Lastly, confirm that the PostgreSQL server inside the container is configured to accept remote connections. If you’re still having issues, try connecting to the server using other tools to narrow down the problem.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search