Django PostGis BUG?

37 views
Skip to first unread message

christop...@gmail.com

unread,
Jul 13, 2016, 12:16:02 PM7/13/16
to Django users

I'm trying to create my database tables (python manage.py migrate) using Postgis. However it fails because the driver try to connect do default database "postgres" which is not my default name. I'm using django==1.8 and psycopg2==2.6.2 

If i look the documentation of psycopg (http://initd.org/psycopg/docs/module.html) The basic connection parameters are:

dbname  the database name (only in the dsn string)
database  the database name (only as keyword argument)
user  user name used to authenticate
password  password used to authenticate
host  database host address (defaults to UNIX socket if not provided)
port  connection port number (defaults to 5432 if not provided)

Here is my database connection: 

DATABASES = {
 'default': {
 'ENGINE': 'django.contrib.gis.db.backends.postgis', 
 'NAME': '<mydatabase>',
 'USER': '<myuser>',
 'PASSWORD': '<mypassword>',
 'HOST':  bdsn15qbq8xaqoa-postgresql.services.clever-cloud.com'
 'PORT': '5432', 
 'URI':'postgis://myuser:mypassword@bdsn15qbq8xaqoa-postgresql.services.clever-cloud.com:5432/bdsn15qbq8xaqoa'
}
}

I have added some trace to the connexion and as we can see, the driver uses postgres as name.

[('dbname', '**postgres**'), ('user', 'myuser'), ('password', 'mypassword'), ('host', 'bdsn15qbq8xaqoa-postgresql.services.clever-cloud.com'), ('port', '5432’)]
After some research, i have edited the file :

 vi django/db/backends/postgresql_psycopg2/base.py


Function : 


def get_connection_params(self):

        settings_dict = self.settings_dict

        # None may be used to connect to the default 'postgres' db

        if settings_dict['NAME'] == '':

            from django.core.exceptions import ImproperlyConfigured

            raise ImproperlyConfigured(

                "settings.DATABASES is improperly configured. "

                "Please supply the NAME value.")

        conn_params = {

            'database': settings_dict['NAME'] or 'postgres',

        }

        print settings_dict

        conn_params.update(settings_dict['OPTIONS'])

        conn_params.pop('isolation_level', None)

        if settings_dict['USER']:

            conn_params['user'] = settings_dict['USER']

        if settings_dict['PASSWORD']:

            conn_params['password'] = force_str(settings_dict['PASSWORD'])

        if settings_dict['HOST']:

            conn_params['host'] = settings_dict['HOST']

        if settings_dict['PORT']:

            conn_params['port'] = settings_dict['PORT']

        return conn_params


AND ADDED : 


   if settings_dict['DBNAME']:

            conn_params['dbname'] = settings_dict['DBNAME']


Now if i relaunch the database creation : python manage.py migrate it is working fine using the good connection URI. 

[('dbname', '**mydatabase**'), ('user', 'myuser'), ('password', 'mypassword'), ('host', 'bdsn15qbq8xaqoa-postgresql.services.clever-cloud.com'), ('port', '5432’) 
It is a bug ? 

Thanks a lot 
Christophe

Jani Tiainen

unread,
Jul 15, 2016, 6:26:27 AM7/15/16
to django...@googlegroups.com
Hi,

Could you verify what your database settings really are for example printing them out at the end of settings.py. (Or in any view import your settings to see that values really are what you expect.)

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/d57803ab-eeb1-4f5a-a0ca-2725730178a7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Jani Tiainen

- Well planned is half done, and a half done has been sufficient before...

Tim Graham

unread,
Jul 15, 2016, 8:56:27 AM7/15/16
to Django users
This query is also duplicated on Trac: https://code.djangoproject.com/ticket/26893 (closed as "worksforme").

For future reference, please ask "is it a bug?" questions on django-users and wait for confirmation that it is a bug before creating a Trac ticket.
Reply all
Reply to author
Forward
0 new messages