Django vs Postgres Connection

415 views
Skip to first unread message

armandoperico

unread,
Dec 17, 2010, 3:02:56 PM12/17/10
to Django users
Hi,
this is the first time i try to deploy a django project and i'm not
being able to solve a problem.

On my local machine everything is running fine, but when i try to
deploy to a running server with apache (mod_wsgi), i'm getting the
following error (database related):

Caught OperationalError while rendering: could not connect to server:
Permission denied
Is the server running on host "127.0.0.1" and accepting
TCP/IP connections on port 5432?

The fact is that when i try to connect to the database using psql -
U... is working fine, so postgree is working ok, it seems to be with
django :S

Exception Location: /usr/lib/python2.6/site-packages/django/db/
backends/postgresql_psycopg2/base.py in _cursor, line 136

this is my django version (just in case): 1.2.3

Anyone here has faced this problem ?
have any ideas?

Thanks

Christophe Pettus

unread,
Dec 17, 2010, 3:25:43 PM12/17/10
to django...@googlegroups.com

On Dec 17, 2010, at 12:02 PM, armandoperico wrote:
> On my local machine everything is running fine, but when i try to
> deploy to a running server with apache (mod_wsgi), i'm getting the
> following error (database related):
>
> Caught OperationalError while rendering: could not connect to server:
> Permission denied
> Is the server running on host "127.0.0.1" and accepting
> TCP/IP connections on port 5432?

What's your database configuration on the Apache machine?

--
-- Christophe Pettus
x...@thebuild.com

Servais Nabil

unread,
Dec 17, 2010, 3:30:29 PM12/17/10
to django...@googlegroups.com

Hello, I think postgresql is not running or doesnt use the 5432 port. Check your processus.

> --
> 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.
>

Mathieu Leduc-Hamel

unread,
Dec 17, 2010, 3:38:02 PM12/17/10
to django...@googlegroups.com
check if postgresql is listening to 127.0.0.1, 0.0.0.0 or nothing in postgrsql

--
Mathieu Leduc-Hamel

James DeMichele

unread,
Dec 17, 2010, 3:26:27 PM12/17/10
to django...@googlegroups.com
This sounds more like a Postgresql settings problem than a Django/Python
specific problem.

I'd suggest taking a look at your pg_hba.conf file. It can be finicky, so
you probably want to reference this if you haven't already:

http://www.postgresql.org/docs/8.4/static/auth-pg-hba-conf.html

-Jamie

Thanks

--

armandoperico

unread,
Dec 17, 2010, 3:49:48 PM12/17/10
to Django users
on settings.py..

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2', # Add
'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'dbname', # Or path to database
file if using sqlite3.
'USER': 'myuser', # Not used with
sqlite3.
'PASSWORD': 'mypass', # Not used with
sqlite3.
'HOST': '127.0.0.1', # Set to empty
string for localhost. Not used with sqlite3.
'PORT': '5432', # Set to empty string for
default. Not used with sqlite3.

armandoperico

unread,
Dec 17, 2010, 3:51:23 PM12/17/10
to Django users
on my pg_hba.conf i right now have this:

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

Anurag Chourasia

unread,
Dec 17, 2010, 3:51:22 PM12/17/10
to django...@googlegroups.com
You might want to check your wsgi script.

Looks like the environment variables that you have in there are wrong and not allowing the import of settings.py

Regards,
Anurag

armandoperico

unread,
Dec 17, 2010, 4:00:21 PM12/17/10
to Django users
Below follows my wsgi script.
But i don't think it's not being able to read the settings.py because
it is actually complaining about the info that is inside there. (the
DB infos)
:S

---------wsgi
-------------------------------------------------------------------------------------------
import os
import sys

sys.path.append('*******/wsgi/src/mysite/')
sys.path.append('*/wsgi/')

os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
---------wsgi
-------------------------------------------------------------------------------------------

On Dec 17, 9:51 pm, Anurag Chourasia <anurag.choura...@gmail.com>
wrote:
> You might want to check your wsgi script.
>
> Looks like the environment variables that you have in there are wrong and
> not allowing the import of settings.py
>
> Regards,
> Anurag
>
> > django-users...@googlegroups.com<django-users%2Bunsubscribe@google groups.com>
> > .

Anurag Chourasia

unread,
Dec 17, 2010, 4:04:48 PM12/17/10
to django...@googlegroups.com
On this server, are you able to connect to the PostGre database using the command line?

psql
 -U myuser -h 127.0.0.1 -p 5432 -d dbname


Regards,
Anurag

--
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.

armandoperico

unread,
Dec 17, 2010, 4:15:31 PM12/17/10
to Django users
yes.

On Dec 17, 10:04 pm, Anurag Chourasia <anurag.choura...@gmail.com>
wrote:
>  On this server, are you able to connect to the PostGre database using the
> command line?
>
> psql -U myuser -h 127.0.0.1 -p 5432 -d dbname
>
> Regards,
> Anurag
>
> > django-users...@googlegroups.com<django-users%2Bunsubscribe@google groups.com>
> > .

armandoperico

unread,
Dec 17, 2010, 4:17:15 PM12/17/10
to Django users
could it be something related with permissions ?!
because i have to change everything to "apache" - chown -R apache ../
myproject_folder/

which would be the right permissions to have ?

thanks a lot

On Dec 17, 10:04 pm, Anurag Chourasia <anurag.choura...@gmail.com>
wrote:
>  On this server, are you able to connect to the PostGre database using the
> command line?
>
> psql -U myuser -h 127.0.0.1 -p 5432 -d dbname
>
> Regards,
> Anurag
>
> > django-users...@googlegroups.com<django-users%2Bunsubscribe@google groups.com>
> > .

Anurag Chourasia

unread,
Dec 17, 2010, 4:17:38 PM12/17/10
to django...@googlegroups.com
And did you had to provide a password for establishing that connection? Or the .pgpass file took care of it? 

If you had to provide a password then probably that's your clue. You might be required to specify the pgpass in mod wsgi script.

To unsubscribe from this group, send email to django-users...@googlegroups.com.

armandoperico

unread,
Dec 17, 2010, 4:28:06 PM12/17/10
to Django users
yes... no passwords.

On Dec 17, 10:17 pm, Anurag Chourasia <anurag.choura...@gmail.com>
wrote:
> And did you had to provide a password for establishing that connection? Or
> the .pgpass file took care of it?
>
> If you had to provide a password then probably that's your clue. You might
> be required to specify the pgpass in mod wsgi script.
>

armandoperico

unread,
Dec 17, 2010, 4:50:57 PM12/17/10
to Django users
sorry,
how should do to specify the password on the wsgi file ?
this would be the best i have so far.

thanks


On Dec 17, 10:17 pm, Anurag Chourasia <anurag.choura...@gmail.com>
wrote:
> And did you had to provide a password for establishing that connection? Or
> the .pgpass file took care of it?
>
> If you had to provide a password then probably that's your clue. You might
> be required to specify the pgpass in mod wsgi script.
>

Anurag Chourasia

unread,
Dec 17, 2010, 6:30:03 PM12/17/10
to django...@googlegroups.com
Hi Armando,

To specify the Postgre password, you should set the environment variable pointing to the pgpass file ...... basically add this line to your wsgi script (replacing with the actual location of your pgpass file of course)

os.environ['PGPASSFILE']='/home/ap2/.pgpass'

Let me know if it works.

Regards,
Anurag

To unsubscribe from this group, send email to django-users...@googlegroups.com.

CLIFFORD ILKAY

unread,
Dec 17, 2010, 10:54:51 PM12/17/10
to django...@googlegroups.com
On 12/17/2010 03:49 PM, armandoperico wrote:
> on settings.py..
>
> DATABASES = {
> 'default': {
> 'ENGINE': 'django.db.backends.postgresql_psycopg2', # Add
> 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
> 'NAME': 'dbname', # Or path to database
> file if using sqlite3.
> 'USER': 'myuser', # Not used with
> sqlite3.
> 'PASSWORD': 'mypass', # Not used with
> sqlite3.
> 'HOST': '127.0.0.1', # Set to empty
> string for localhost. Not used with sqlite3.
> 'PORT': '5432', # Set to empty string for
> default. Not used with sqlite3.
> }
> }

Either get rid of the 127.0.0.1 and 5432 as per the comments in the code
above or change listen_address="*" in postgresql.conf and restart PG.
The former will make a socket connection. You're trying to make a TCP/IP
connection to PostgreSQL as you have it above now and PG is configured
by default to only listen to localhost, not an IP address, even if that
IP address is 127.0.0.1. You'll most likely find
listen_address="localhost" on your machine right now.
--
Regards,

Clifford Ilkay
Dinamis
1419-3266 Yonge St.
Toronto, ON
Canada M4N 3P6

<http://dinamis.com>
+1 416-410-3326

armandoperico

unread,
Dec 18, 2010, 7:10:56 AM12/18/10
to Django users
Yes!
now it worked, but i also had to remove the HOST and the PORT value
from the settings.py (suggested by CLIFFORD). Otherwise it was
randomly rendering or given another strange error.
like this though if running fine!

Thank you very much!

On Dec 18, 12:30 am, Anurag Chourasia <anurag.choura...@gmail.com>
wrote:
> Hi Armando,
>
> To specify the Postgre password, you should set the environment variable
> pointing to the pgpass file ...... basically add this line to your wsgi
> script (replacing with the actual location of your pgpass file of course)
>
> *os.environ['PGPASSFILE']='/home/ap2/.pgpass'*
>
> Let me know if it works.
>
> Regards,
> Anurag
>

armandoperico

unread,
Dec 18, 2010, 7:12:33 AM12/18/10
to Django users

Yes!
you were right, but i also had to add os.environ['PGPASSFILE']='/home/
ap2/.pgpass to my wsgi file.

Thank you very much

"PROBLEM SOLVED"
Reply all
Reply to author
Forward
0 new messages