I have installed vagrant and virtual box on my laptop to finish a class I am working on. I have created a database called tournament. I can connect to it directly, create tables, insert data, etc.

However, I am trying to write a python script to connect to it and having great difficulty.

Here is my greatly simplified code:

import psycopg2


Here is what I am getting:

enter image description here

Any help would be greatly appreciated. I am really frustrated.

  • 1
    Is the server running on localhost? Are you connecting from inside your VM or from your machine? If you say "connect directly" how do you do that? – Tom V - try topanswers.xyz Aug 18 '17 at 8:14

Since you did not specify the host argument of che connect call, you are connecting via a unix named pipe instead of TCP/IP, so the error message might be slightly incorrect. BTW, the database should have opened a pipe named /var/run/postgresql/.s.PGSQL.5432. Can you check if it exists using command ls -la /var/run/postgresql/.s*? You might find there a pipe with a different number, or you might find and empty directory. In the first case, just add port=number (using the right number) to the connect call; in the latter case, find the correct directory for your postgresql and add host=path (using the right path) to the connect call.

In order to find the correct path, connect via psql and give command \conninfo. In will display the correct path, as in:

postgres=# \conninfo
You are connected to database "postgres" as user "postgres" via socket in "/var/run/postgresql" at port "5432".

P.S. It might also be a problem of the syntax you used. It should be psycopg2.connect(dbname="tournament")

| improve this answer | |
  • 1
    The error message mentions ::1 and, which indicates that it is attempting to connect over TCP, not over unix named pipe. Which makes sense, as the rest of the error message indicates he is using Windows, and Windows does not even have unix named pipes. – jjanes Aug 20 '17 at 23:11
  • @jjanes You are right: he uses python on Windows, while postgresql is probably running on Linux in a VM. In this case, either he should add host=ipaddress-of-VM or the database is not listening on TCP. – eppesuig Aug 21 '17 at 9:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.