1. ==================
    
  2. Installing PostGIS
    
  3. ==================
    
  4. 
    
  5. `PostGIS`_ adds geographic object support to PostgreSQL, turning it
    
  6. into a spatial database. :ref:`geosbuild`, :ref:`proj4` and
    
  7. :ref:`gdalbuild` should be installed prior to building PostGIS. You
    
  8. might also need additional libraries, see `PostGIS requirements`_.
    
  9. 
    
  10. The `psycopg2`_ module is required for use as the database adapter when using
    
  11. GeoDjango with PostGIS.
    
  12. 
    
  13. On Debian/Ubuntu, you are advised to install the following packages:
    
  14. ``postgresql-x``, ``postgresql-x-postgis-3``, ``postgresql-server-dev-x``,
    
  15. and ``python3-psycopg2`` (x matching the PostgreSQL version you want to
    
  16. install). Alternately, you can `build from source`_. Consult the
    
  17. platform-specific instructions if you are on :ref:`macos` or :ref:`windows`.
    
  18. 
    
  19. .. _PostGIS: https://postgis.net/
    
  20. .. _psycopg2: https://www.psycopg.org/
    
  21. .. _PostGIS requirements: https://postgis.net/docs/postgis_installation.html#install_requirements
    
  22. .. _build from source: https://postgis.net/docs/postgis_installation.html#install_short_version
    
  23. 
    
  24. Post-installation
    
  25. =================
    
  26. 
    
  27. .. _spatialdb_template:
    
  28. 
    
  29. Creating a spatial database
    
  30. ---------------------------
    
  31. 
    
  32. PostGIS 2 includes an extension for PostgreSQL that's used to enable spatial
    
  33. functionality::
    
  34. 
    
  35.     $ createdb  <db name>
    
  36.     $ psql <db name>
    
  37.     > CREATE EXTENSION postgis;
    
  38. 
    
  39. The database user must be a superuser in order to run
    
  40. ``CREATE EXTENSION postgis;``. The command is run during the :djadmin:`migrate`
    
  41. process. An alternative is to use a migration operation in your project::
    
  42. 
    
  43.     from django.contrib.postgres.operations import CreateExtension
    
  44.     from django.db import migrations
    
  45. 
    
  46.     class Migration(migrations.Migration):
    
  47. 
    
  48.         operations = [
    
  49.             CreateExtension('postgis'),
    
  50.             ...
    
  51.         ]
    
  52. 
    
  53. If you plan to use PostGIS raster functionality on PostGIS 3+, you should also
    
  54. activate the ``postgis_raster`` extension. You can install the extension using
    
  55. the :class:`~django.contrib.postgres.operations.CreateExtension` migration
    
  56. operation, or directly by running ``CREATE EXTENSION postgis_raster;``.
    
  57. 
    
  58. GeoDjango does not currently leverage any `PostGIS topology functionality`__.
    
  59. If you plan to use those features at some point, you can also install the
    
  60. ``postgis_topology`` extension by issuing ``CREATE EXTENSION
    
  61. postgis_topology;``.
    
  62. 
    
  63. __ https://postgis.net/docs/Topology.html
    
  64. 
    
  65. Managing the database
    
  66. ---------------------
    
  67. 
    
  68. To administer the database, you can either use the pgAdmin III program
    
  69. (:menuselection:`Start --> PostgreSQL X --> pgAdmin III`) or the SQL Shell
    
  70. (:menuselection:`Start --> PostgreSQL X --> SQL Shell`). For example, to create
    
  71. a ``geodjango`` spatial database and user, the following may be executed from
    
  72. the SQL Shell as the ``postgres`` user::
    
  73. 
    
  74.     postgres# CREATE USER geodjango PASSWORD 'my_passwd';
    
  75.     postgres# CREATE DATABASE geodjango OWNER geodjango;