Could not connect postgresql database

174 views
Skip to first unread message

SSozuer

unread,
Aug 23, 2011, 9:05:39 AM8/23/11
to Django users
Hi guys,
When i was checking the database connection,i am taking that error:

File "<console>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/
__init__.py", line 250, in cursor
cursor = self.make_debug_cursor(self._cursor())
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/
postgresql_psycopg2/base.py", line 140, in _cursor
self.connection = Database.connect(**conn_params)
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"?

Here is my pg_hba.conf file settings:

# TYPE DATABASE USER CIDR-ADDRESS METHOD

# "local" is for Unix domain socket connections only
local all postgres trust
# IPv4 local connections:
host all postgres 127.0.0.1/32 trust
# IPv6 local connections:
host all all ::1/128 trust

any help would be great, thanks
Selcuk

SSozuer

unread,
Aug 23, 2011, 9:25:13 AM8/23/11
to Django users
In addition i can connect database with pgAdmin III without any
error...

Simon Riggs

unread,
Aug 23, 2011, 11:06:16 AM8/23/11
to django-users
On Tue, Aug 23, 2011 at 2:05 PM, SSozuer <selcuk...@gmail.com> wrote:

>        Is the server running locally and accepting
>        connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.
> 5432"?

Try connecting on the local system. I would guess you can't. It looks
like you have a problem with the socket directory. Permissions?

> Here is my pg_hba.conf file  settings:

That looks correct.

--
 Simon Riggs                   http://www.2ndQuadrant.com/
 PostgreSQL Development, 24x7 Support, Training & Services

SSozuer

unread,
Aug 23, 2011, 12:01:35 PM8/23/11
to Django users
l checked the /var/run/postgresql/... directory. But there is no
directory called that. I'll reinstall postgresql from package, the
current was installed from source code
thanks for advice

Selcuk

On Aug 23, 6:06 pm, Simon Riggs <si...@2ndQuadrant.com> wrote:

Landy Chapman

unread,
Aug 23, 2011, 12:45:11 PM8/23/11
to Django users

> > On Tue, Aug 23, 2011 at 2:05 PM, SSozuer <selcuk.soz...@gmail.com> wrote:
> > >        Is the server running locally and accepting
> > >        connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.
> > > 5432"?
>
> > Try connecting on the local system. I would guess you can't. It looks
> > like you have a problem with the socket directory. Permissions?

remember to edit postgresql.conf -- make sure to change
listen_addresses = '*'
port = 5432

Landy Chapman

unread,
Aug 23, 2011, 1:03:35 PM8/23/11
to Django users
Also did the the server actually start? On debian,
/etc/init.d/postgresql start

SSozuer

unread,
Aug 23, 2011, 1:09:33 PM8/23/11
to Django users
Settings in postgresql.conf are ok. And also postgresql is running
without no problem. As i said, i can connect database with pgAdmin
with no error.
Selcuk

Subhranath Chunder

unread,
Aug 23, 2011, 1:12:30 PM8/23/11
to django...@googlegroups.com
You may try to test the connection using the command line interface 'psql'.


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.




--
Thanks,
Subhranath Chunder.

SSozuer

unread,
Aug 23, 2011, 1:53:26 PM8/23/11
to Django users
i am taking error below:

psql: 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"?

but there is no such a .s.PGSQL.5432 file. I really dont understand
what is going on

Selcuk

Subhranath Chunder

unread,
Aug 23, 2011, 2:09:15 PM8/23/11
to django...@googlegroups.com
Seems to me like it has something to do with pg_hba.conf settings.

Maybe you can try out by commenting out the existing entries in it, and inserting one specific to your requirement.
You should probably try it out. And don't forget to restart the db server everytime you make a change.

Landy Chapman

unread,
Aug 23, 2011, 2:19:34 PM8/23/11
to Django users
I apologize.. What user are you running psql as? Try this:
cd /tmp
su postgres
createdb conntest
psql -d conntest


That should since you can connect from pgadmin. If it doesn't. run
(as root)
/etc/init.d/postgresql force-restart

and try the above again.

On the django side, I would check settings.py for something like this:

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2', # Add
'postgr...
'NAME': 'conntest', # Or path to database
file if using sqlite3.
'USER': 'postgres', # Not used with
sqlite3.
'PASSWORD': 'mypgpass', # Not used with
sqlite3.
'HOST': '', # Set to empty string for
localhost. Not used with sqlite3.
'PORT':'5432', # Set to empty string for
default. Not used with sqlite3.

SSozuer

unread,
Aug 23, 2011, 2:19:36 PM8/23/11
to Django users
nothing changed...
there are lots of questions about this problem on net , but there is
no point solution

SSozuer

unread,
Aug 23, 2011, 3:06:06 PM8/23/11
to Django users
Guys,
When i command ;
postgres@laptop:~$ /usr/local/pgsql/bin/createdb test2db

there is no problem and ".s.PGSQL.5432" file appeared in tmp directory
which is ok according my postgresql.conf settings.But when i command;

postgres@laptop:~$ psql test2db
psql: 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"?

and also when i tried to connect to database with django same error
above occured.
I think, there is a problem related to enviroment variables. But i
dont know how to handle it.

thanks again
Selcuk

bik...@gmail.com

unread,
Aug 23, 2011, 3:09:11 PM8/23/11
to django...@googlegroups.com
I might be completely stating the obvious, or something mentioned already, but ok, you create the database, but is the deamon running..?

Sent from my BlackBerry® from Vodafone

bik...@gmail.com

unread,
Aug 23, 2011, 3:12:19 PM8/23/11
to django...@googlegroups.com
Also, might be worth looking at the steps of this:

Installing PostgreSQL on Ubuntu 11.04 using PPA: http://bit.ly/mTZcp4

Maybe you'll see something in the steps that will help you?


Sent from my BlackBerry® from Vodafone

-----Original Message-----
From: SSozuer <selcuk...@gmail.com>
Sender: django...@googlegroups.com
Date: Tue, 23 Aug 2011 12:06:06
To: Django users<django...@googlegroups.com>
Reply-To: django...@googlegroups.com
Subject: Re: Could not connect postgresql database

Landy Chapman

unread,
Aug 23, 2011, 3:12:26 PM8/23/11
to Django users

> there are lots of questions about this problem on net , but there is
> no point solution

There's no FixItNow(tm) text that works for all use cases. Sometimes
over-familiarity will cause maddening (though simple-to-fix)
problems. My basic point is, since nothing you did solved the
problem, help us be systematic. We need to examine things step by
step. So please do what we ask and post the results. I'm not talking
down to you; this is basic troubleshooting. I don't even know what OS
you are running. (Linux, I presume?)

Your already posted pg_hba.conf file looks fine.
If you want help you must provide ALL information we ask for, or we'll
be shooting in the dark.
Are you running linux? What distro?
Are django, postgresql and pgadmin all running on the same machine?

Answer the above, and post your postgresql.conf file. Then I/we can
be more specific in narrowing down this problem.

Message has been deleted

SSozuer

unread,
Aug 23, 2011, 3:43:22 PM8/23/11
to Django users
You're so right, sorry about that.
OS: ubuntu 11.04
Database: postgresql 8.4.8
Django Version: 1.3
All of them are running on same machine.

I install the current postgresql database version from source code. I
think i missed something related to enviroment variables.

I think, i just need to "PATH=/usr/local/pgsql/bin:$PATH" statement
to affects all users.
So there is only one question right now
How can i add that statement my shell start-up file to affects all
users?

thanks
Selcuk


On Aug 23, 10:20 pm, Landy Chapman <landy.chapm...@gmail.com> wrote:
> Ignore my long-winded rant/reply above.
>
> the command should be
>
> psql -d test2db
>
> Cheers

Landy Chapman

unread,
Aug 23, 2011, 3:50:35 PM8/23/11
to Django users
I reinstalled postgresql after deleting config files. in
postgresql.conf I changed this line:
# listen = "localhost"
to read:

listen = "*"

Also make sure this line is uncommented:
port = 5432

Then I restarted the server.
/etc/init.d/postgresql restart

One way to see if postgresql is running would be:
lsof | grep "postgres"


> createdb conntest
I asked you to create a database because if the server ISNT running
you would not be able to.

> psql -d conntest
As another suggested, this will try to use the postgres client. If
all that worked, I would've asked you to run

psql -h 127.0.0.1 -d conntest

Which, in a DEFAULT debian lenny install result in:
"psql: FATAL: password authentication failed for user "postgres"

I set a password for postgres:
su
passwd postgres

Next I change pg_hba.conf to "trust" for localhost, restarted the
server, and
psql -h 127.0.0.1 -d conntest

does work. The last step would be to use my local IP address
psql -h 192.168.1.211 -d conntest

This would make sure remote machines could connect also.

SSozuer

unread,
Aug 23, 2011, 7:12:22 PM8/23/11
to Django users
I solved the problem. I just added,
LD_LIBRARY_PATH=/usr/local/pgsql/lib
PATH=/usr/local/pgsql/bin:$PATH

lines to my .bashrc startup shell. After that there is no error.

Selcuk

Landy Chapman

unread,
Aug 23, 2011, 10:27:18 PM8/23/11
to django...@googlegroups.com
Great job. You could've told everyone that you had a working install
and decided to break it & install from source code. but.... oh well.

OSS truly is the ultimate, when you break it you get to keep both pieces :-;


For the record: why did you install from source instead of using
Ubuntu packages ? A learning exercise? Bug-fix? Unwanted defaults?
This might help someone in the future.


Cheers/Excelsior!

Reply all
Reply to author
Forward
0 new messages