2

I have set up a temporary Postgres database for test purposes on my computer using Docker and the following commands:

1)

sudo docker run --name some-postgres6 -e POSTGRES_PASSWORD=mysecretpassword -p 5430:5432 postgres:9.1 -d postgres

2)

sudo docker run -it --rm --link some-postgres6:postgres postgres psql -h postgres -U postgres

I want to connect to the database using Python :

from sqlalchemy.engine import create_engine
import psycopg2

engine = create_engine('postgresql+psycopg2://postgres:mysecretpassword@localhost/mydb?port=5432') ### IMPORTANT!!!
connection = engine.connect()

Then I'm getting this error:

psycopg2.OperationalError: could not connect to server: Connection refused (0x0000274D/10061)
    Is the server running on host "localhost" (::1) and accepting
    TCP/IP connections on port 5432?
could not connect to server: Connection refused (0x0000274D/10061)
    Is the server running on host "localhost" (127.0.0.1) and accepting
    TCP/IP connections on port 5432?

This same piece of code used to work on my other machine where I was running Ubuntu. I guess this has to do with windows. I am running Windows 10 Home (not experienced with Windows) and using Docker toolbox.

4
  • Can you connect from psql? Commented Feb 13, 2018 at 17:12
  • Connecting from psql works Commented Feb 14, 2018 at 9:55
  • That being Windows, have you looked at your firewall / antivirus logs? They can be discerning enough to allow psql.exe to connect, but block python.exe. Commented Feb 14, 2018 at 17:16
  • nope, the antivirus was not blocking anything Commented Feb 15, 2018 at 15:17

2 Answers 2

2

I finally figured out the issue. It was "localhost", there was nothing running on 127.0.0.1 .

I had to change it to the IP of the docker machine. This information is displayed if you open the Docker Quickstart Terminal. It shows something like "docker is configured to use the default machine with IP 192.168.XX.XXX"

Another way to find this IP is to open Resource Monitor, go to the Network tab, then check the TCP connections. There should be docker.exe running.The IP shown in the Remote Address column is the one that will work.

Finally the correct command :

engine = create_engine('postgresql+psycopg2://postgres:[email protected]/mydb?port=5430')
Sign up to request clarification or add additional context in comments.

Comments

0

Not sure if this is just a typo in your example, but you are mapping your postgres container in your first code snippet to port 5430. If this is correct you will have to change your connection string to the following:

engine = create_engine('postgresql+psycopg2://postgres:mysecretpassword@localhost/mydb?port=5430') ### IMPORTANT!!!

2 Comments

Same error as in "Is the server running on host "localhost" (127.0.0.1) and accepting TCP/IP connections on port 5432?" or "Is the server running on host "localhost" (127.0.0.1) and accepting TCP/IP connections on port 5430?"
it was a typo, I changed it to 5430, still the same error."" Is the server running on host "localhost" (127.0.0.1) and accepting TCP/IP connections on port 5430""

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.