0

trying to connect to a Postgres database inside docker but having the following problem:

File "", line 1, in pyodbc.OperationalError: ('08001', '[08001] could not connect to server: No such file or directory\n\tIs the server running locally and accepting\n\tconnections on Unix domain socket "/tmp/.s.PGSQL.5432"?\n (101) (SQLDriverConnect)')

The connection is being established with the following:

conn_str = ("DRIVER=/usr/local/lib/psqlodbcw.so;""DATABASE=mytestdb;""UID=postgres;""PWD=mysecretpassword""SERVER=localhost;""PORT=5432;");

conn = pyodbc.connect(conn_str)
3
  • trying to connect to a Postgres database inside docker did the application that trying to connect inside a docker instance or on the host OS? also, on your connection string it says localhost (SERVER=localhost), you might want to see the ip of your docker instance, otherwise setup a NAT for the docker.. Commented Mar 25, 2019 at 1:11
  • Thanks for this, I just checked that the container IP was 172.17.0.2 and changed it on the code but still got the same issue. Commented Mar 25, 2019 at 1:22
  • is the host had the same subnet address with that IP? something like 172.17.0.1? if you have pgAdmin, you may want to connect it first. also just a side note, do you have any third party anti virus or firewall? that can also be the cause your host unable to communicate with the docker instance. Commented Mar 25, 2019 at 2:23

2 Answers 2

2

Make sure on your docker container you expose port 5432 other wise you wont be able to connect. This information can clear your thoughts. https://medium.com/@lvthillo/connect-from-local-machine-to-postgresql-docker-container-f785f00461a7

on your docker-compose.yml change it to something like this:

db:
image: postgres
ports:
  - 5432:5432

it can be 5432 or 5435 if you already have pg on your local machine.

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

2 Comments

I followed those steps (used that post as reference). I also changed the SERVER to SERVER=172.17.0.2 since that's the container IP but I still got the same error.
you need to change you config to allow remote address to connect. did you do that? also do not use IP simply use db or the name of your image.
0

Your application is trying to connect postgres via unix socket file in host /tmp/ folder.

So configure mount point for docker container in posgres docker-compose file as below:

volumes:
      - /tmp:/var/lib/postgresql

then rebuild postgres container again.

Comments

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.