Ticket #16455: 16455-v9.diff

File 16455-v9.diff, 11.6 KB (added by Flavio Curella, 12 years ago)

checks if a template database exists

  • django/contrib/gis/db/backends/postgis/creation.py

    diff --git a/django/contrib/gis/db/backends/postgis/creation.py b/django/contrib/gis/db/backends/postgis/creation.py
    index bad22be..fc8a242 100644
    a b  
    11from django.conf import settings
     2from django.core.exceptions import ImproperlyConfigured
    23from django.db.backends.postgresql_psycopg2.creation import DatabaseCreation
    34
    45class PostGISCreation(DatabaseCreation):
    class PostGISCreation(DatabaseCreation):  
    3839                                  style.SQL_FIELD(qn(f.column)) +
    3940                                  style.SQL_KEYWORD(' SET NOT NULL') + ';')
    4041
    41 
    4242            if f.spatial_index:
    4343                # Spatial indexes created the same way for both Geometry and
    44                 # Geography columns
     44                # Geography columns.
     45                # PostGIS 2.0 does not support GIST_GEOMETRY_OPS. So, on 1.5
     46                # we use GIST_GEOMETRY_OPS, on 2.0 we use either "nd" ops
     47                # which are fast on multidimensional cases, or just plain
     48                # gist index for the 2d case.
    4549                if f.geography:
    4650                    index_opts = ''
     51                elif self.connection.ops.spatial_version >= (2, 0):
     52                    if f.dim > 2:
     53                        index_opts = ' ' + style.SQL_KEYWORD('gist_geometry_ops_nd')
     54                    else:
     55                        index_opts = ''
    4756                else:
    4857                    index_opts = ' ' + style.SQL_KEYWORD(self.geom_index_opts)
    4958                output.append(style.SQL_KEYWORD('CREATE INDEX ') +
    class PostGISCreation(DatabaseCreation):  
    5665        return output
    5766
    5867    def sql_table_creation_suffix(self):
    59         qn = self.connection.ops.quote_name
    60         return ' TEMPLATE %s' % qn(getattr(settings, 'POSTGIS_TEMPLATE', 'template_postgis'))
     68        cursor = self.connection.cursor()
     69        cursor.execute('select datname from pg_database;')
     70        db_names = [row[0] for row in cursor.fetchall()]
     71        postgis_template = getattr(settings, 'POSTGIS_TEMPLATE', 'template_postgis')
     72
     73        if postgis_template in db_names:
     74            qn = self.connection.ops.quote_name
     75            return ' TEMPLATE %s' % qn(postgis_template)
     76        elif self.connection.ops.spatial_version < (2, 0):
     77            raise ImproperlyConfigured("Template database '%s' does not exist." % postgis_template)
     78        else:
     79            return ''
  • docs/ref/contrib/gis/install.txt

    diff --git a/docs/ref/contrib/gis/install.txt b/docs/ref/contrib/gis/install.txt
    index 72bd72a..70b1085 100644
    a b supported versions, and any notes for each of the supported database backends:  
    6363==================  ==============================  ==================  ==========================================================
    6464Database            Library Requirements            Supported Versions  Notes
    6565==================  ==============================  ==================  ==========================================================
    66 PostgreSQL          GEOS, PROJ.4, PostGIS           8.1+                Requires PostGIS.
     66PostgreSQL          GEOS, GDAL, PROJ.4, PostGIS     8.2+                Requires PostGIS.
    6767MySQL               GEOS                            5.x                 Not OGC-compliant; limited functionality.
    6868Oracle              GEOS                            10.2, 11            XE not supported; not tested with 9.
    6969SQLite              GEOS, GDAL, PROJ.4, SpatiaLite  3.6.+               Requires SpatiaLite 2.3+, pysqlite2 2.5+, and Django 1.1.
    Program Description Required  
    8181========================  ====================================  ================================  ==========================
    8282:ref:`GEOS <ref-geos>`    Geometry Engine Open Source           Yes                               3.3, 3.2, 3.1, 3.0
    8383`PROJ.4`_                 Cartographic Projections library      Yes (PostgreSQL and SQLite only)  4.7, 4.6, 4.5, 4.4
    84 :ref:`GDAL <ref-gdal>`    Geospatial Data Abstraction Library   No (but, required for SQLite)     1.9, 1.8, 1.7, 1.6, 1.5
     84:ref:`GDAL <ref-gdal>`    Geospatial Data Abstraction Library   No (but, required for SQLite
     85                                                                and PostgreSQL)                   1.9, 1.8, 1.7, 1.6, 1.5
    8586:ref:`GeoIP <ref-geoip>`  IP-based geolocation library          No                                1.4
    86 `PostGIS`__               Spatial extensions for PostgreSQL     Yes (PostgreSQL only)             1.5, 1.4, 1.3
     87`PostGIS`__               Spatial extensions for PostgreSQL     Yes (PostgreSQL only)             2.0, 1.5, 1.4, 1.3
    8788`SpatiaLite`__            Spatial extensions for SQLite         Yes (SQLite only)                 3.0, 2.4, 2.3
    8889========================  ====================================  ================================  ==========================
    8990
    internal geometry representation used by GeoDjango (it's behind the "lazy"  
    140141geometries).  Specifically, the C API library is called (e.g., ``libgeos_c.so``)
    141142directly from Python using ctypes.
    142143
    143 First, download GEOS 3.2 from the refractions Web site and untar the source
     144First, download GEOS 3.3.5 from the refractions Web site and untar the source
    144145archive::
    145146
    146     $ wget http://download.osgeo.org/geos/geos-3.3.0.tar.bz2
    147     $ tar xjf geos-3.3.0.tar.bz2
     147    $ wget http://download.osgeo.org/geos/geos-3.3.5.tar.bz2
     148    $ tar xjf geos-3.3.5.tar.bz2
    148149
    149150Next, change into the directory where GEOS was unpacked, run the configure
    150151script, compile, and install::
    151152
    152     $ cd geos-3.3.0
     153    $ cd geos-3.3.5
    153154    $ ./configure
    154155    $ make
    155156    $ sudo make install
    Finally, configure, make and install PROJ.4::  
    221222    $ sudo make install
    222223    $ cd ..
    223224
    224 .. _postgis:
    225 
    226 PostGIS
    227 -------
    228 
    229 `PostGIS`__ adds geographic object support to PostgreSQL, turning it
    230 into a spatial database. :ref:`geosbuild` and :ref:`proj4` should be
    231 installed prior to building PostGIS.
    232 
    233 .. note::
    234 
    235     The `psycopg2`_ module is required for use as the database adaptor
    236     when using GeoDjango with PostGIS.
    237 
    238 .. _psycopg2: http://initd.org/psycopg/
    239 
    240 First download the source archive, and extract::
    241 
    242     $ wget http://postgis.refractions.net/download/postgis-1.5.2.tar.gz
    243     $ tar xzf postgis-1.5.2.tar.gz
    244     $ cd postgis-1.5.2
    245 
    246 Next, configure, make and install PostGIS::
    247 
    248     $ ./configure
    249 
    250 Finally, make and install::
    251 
    252     $ make
    253     $ sudo make install
    254     $ cd ..
    255 
    256 .. note::
    257 
    258     GeoDjango does not automatically create a spatial database.  Please
    259     consult the section on :ref:`spatialdb_template` for more information.
    260 
    261 __ http://postgis.refractions.net/
    262 
    263225.. _gdalbuild:
    264226
    265227GDAL
    file:  
    359321
    360322    SetEnv GDAL_DATA /usr/local/share
    361323
     324.. _postgis:
     325
     326PostGIS
     327-------
     328
     329`PostGIS`__ adds geographic object support to PostgreSQL, turning it
     330into a spatial database. :ref:`geosbuild`, :ref:`proj4` and
     331:ref:`gdalbuild` should be installed prior to building PostGIS. You
     332might also need additional libraries, see `PostGIS requirements`_.
     333
     334.. note::
     335
     336    The `psycopg2`_ module is required for use as the database adaptor
     337    when using GeoDjango with PostGIS.
     338
     339.. _psycopg2: http://initd.org/psycopg/
     340.. _PostGIS requirements: http://www.postgis.org/documentation/manual-2.0/postgis_installation.html#id2711662
     341
     342First download the source archive, and extract::
     343
     344    $ wget http://postgis.refractions.net/download/postgis-2.0.1.tar.gz
     345    $ tar xzf postgis-2.0.1.tar.gz
     346    $ cd postgis-2.0.1
     347
     348Next, configure, make and install PostGIS::
     349
     350    $ ./configure
     351
     352Finally, make and install::
     353
     354    $ make
     355    $ sudo make install
     356    $ cd ..
     357
     358.. note::
     359
     360    GeoDjango does not automatically create a spatial database.  Please consult
     361    the section on :ref:`spatialdb_template91` or
     362    :ref:`spatialdb_template_earlier` for more information.
     363
     364__ http://postgis.refractions.net/
     365
    362366.. _spatialite:
    363367
    364368SpatiaLite
    After SQLite has been built with the R*Tree module enabled, get the latest  
    421425SpatiaLite library source and tools bundle from the `download page`__::
    422426
    423427    $ wget http://www.gaia-gis.it/gaia-sins/libspatialite-sources/libspatialite-amalgamation-2.3.1.tar.gz
    424     $ wget http://www.gaia-gis.it/gaia-sins/libspatialite-sources/spatialite-tools-2.3.1.tar.gz
     428    $ wget http://www.gaia-gis.it/gaia-sins/spatialite-tools-sources/spatialite-tools-2.3.1.tar.gz
    425429    $ tar xzf libspatialite-amalgamation-2.3.1.tar.gz
    426430    $ tar xzf spatialite-tools-2.3.1.tar.gz
    427431
    to build and install::  
    502506Post-installation
    503507=================
    504508
    505 .. _spatialdb_template:
     509.. _spatialdb_template91:
     510
     511Creating a spatial database with PostGIS 2.0 and PostgreSQL 9.1
     512---------------------------------------------------------------
     513
     514PostGIS 2 includes an extension for Postgres 9.1 that can be used to enable
     515spatial functionality::
     516
     517    $ createdb  <db name>
     518    $ psql <db name>
     519    > CREATE EXTENSION postgis;
     520    > CREATE EXTENSION postgis_topology;
     521
     522.. _spatialdb_template_earlier:
     523
     524Creating a spatial database template for earlier versions
     525---------------------------------------------------------
    506526
    507 Creating a spatial database template for PostGIS
    508 ------------------------------------------------
     527If you have an earlier version of PostGIS or PostgreSQL, the CREATE
     528EXTENSION isn't available and you need to create the spatial database
     529using the following instructions.
    509530
    510531Creating a spatial database with PostGIS is different than normal because
    511532additional SQL must be loaded to enable spatial functionality.  Because of
    user. For example, you can use the following to become the ``postgres`` user::  
    535556Once you're a database super user, then you may execute the following commands
    536557to create a PostGIS spatial database template::
    537558
    538     $ POSTGIS_SQL_PATH=`pg_config --sharedir`/contrib/postgis-1.5
     559    $ POSTGIS_SQL_PATH=`pg_config --sharedir`/contrib/postgis-2.0
    539560    # Creating the template spatial database.
    540561    $ createdb -E UTF8 template_postgis
    541562    $ createlang -d template_postgis plpgsql # Adding PLPGSQL language support.
    http://www.gaia-gis.it/spatialite-2.4.0/ for 2.4)::  
    605626
    606627Then, use the ``spatialite`` command to initialize a spatial database::
    607628
    608    $ spatialite geodjango.db < init_spatialite-2.X.sql
     629   $ spatialite geodjango.db < init_spatialite-2.3.sql
    609630
    610631.. note::
    611632
    Afterwards, the ``/etc/init.d/postgresql-8.3`` script should be used to manage  
    10801101the starting and stopping of PostgreSQL.
    10811102
    10821103In addition, the SQL files for PostGIS are placed in a different location on
    1083 Debian 5.0 . Thus when :ref:`spatialdb_template` either:
     1104Debian 5.0 . Thus when :ref:`spatialdb_template_earlier` either:
    10841105
    10851106* Create a symbolic link to these files:
    10861107
    may be executed from the SQL Shell as the ``postgres`` user::  
    12661287.. [#] GeoDjango uses the :func:`~ctypes.util.find_library` routine from
    12671288       :mod:`ctypes.util` to locate shared libraries.
    12681289.. [#] The ``psycopg2`` Windows installers are packaged and maintained by
    1269        `Jason Erickson <http://www.stickpeople.com/projects/python/win-psycopg/>`_.
     1290       `Jason Erickson <http://www.stickpeople.com/projects/python/win-psycopg/>`_.
     1291 No newline at end of file
  • docs/releases/1.5.txt

    diff --git a/docs/releases/1.5.txt b/docs/releases/1.5.txt
    index 3a9b2d8..19353f6 100644
    a b on the form.  
    252252Miscellaneous
    253253~~~~~~~~~~~~~
    254254
     255* GeoDjango added support for PostGIS 2.0
     256
    255257* GeoDjango dropped support for GDAL < 1.5
    256258
    257259* :func:`~django.utils.http.int_to_base36` properly raises a :exc:`TypeError`
Back to Top