SaltyCrane Blog — Notes on JavaScript and web development

psycopg2 "could not connect to server" error on Ubuntu

While trying to run python manage.py syncdb with Django 1.2.1, psycopg2 2.2.1 and PostgreSQL 8.4 on Ubuntu Lucid, I got the following error saying I couldn't connect to the database.

$ python manage.py syncdb
Traceback (most recent call last):
  File "manage.py", line 11, in <module>
    execute_manager(settings)
  File "/home/saltycrane/lib/python-environments/saltycrane121/lib/python2.6/site-packages/django/core/management/__init__.py", line 438, in execute_manager
    utility.execute()
  File "/home/saltycrane/lib/python-environments/saltycrane121/lib/python2.6/site-packages/django/core/management/__init__.py", line 379, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/saltycrane/lib/python-environments/saltycrane121/lib/python2.6/site-packages/django/core/management/base.py", line 191, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/home/saltycrane/lib/python-environments/saltycrane121/lib/python2.6/site-packages/django/core/management/base.py", line 218, in execute
    output = self.handle(*args, **options)
  File "/home/saltycrane/lib/python-environments/saltycrane121/lib/python2.6/site-packages/django/core/management/base.py", line 347, in handle
    return self.handle_noargs(**options)
  File "/home/saltycrane/lib/python-environments/saltycrane121/lib/python2.6/site-packages/django/core/management/commands/syncdb.py", line 52, in handle_noargs
    cursor = connection.cursor()
  File "/home/saltycrane/lib/python-environments/saltycrane121/lib/python2.6/site-packages/django/db/backends/__init__.py", line 75, in cursor
    cursor = self._cursor()
  File "/home/saltycrane/lib/python-environments/saltycrane121/lib/python2.6/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 136, in _cursor
    self.connection = Database.connect(**conn_params)
psycopg2.OperationalError: could not connect to server: No such file or directory
        Is the server running locally and accepting
        connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

I tried connecting via psql and that worked. I looked in /var/run/postgresql/ and found:

$ ls -lA /var/run/postgresql
total 8
srwxrwxrwx 1 postgres postgres  0 2010 06/08 21:29 .s.PGSQL.5433
-rw------- 1 postgres postgres 34 2010 06/08 21:29 .s.PGSQL.5433.lock
-rw------- 1 postgres postgres  5 2010 06/08 21:29 8.4-main.pid

So it turns out my Postgres server is running on port 5433, but Django (psycopg2) expects to find it at 5432 by default. So I changed the port number to 5433 in my settings.py and it worked.

Note: I looks like the reason my Postgres server was running on port 5433 instead of 5432 is because I previously had PostgreSQL 8.3 installed on my machine and there were configuration files left in /etc/postgresql/8.3/ The port setting is configured in /etc/postgresql/8.x/main/postgresql.conf.

Comments


#1 LaundroMat commented on :

Thanks! I was tearing my hair out over this one.


#2 Mike commented on :

Thank you, this helped me as well. I removed the leftover config files from a previous installation and it started on the expected port.


#3 Michal commented on :

Thank You for the clue.