postgis.txt 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. ==================
  2. Installing PostGIS
  3. ==================
  4. `PostGIS`__ adds geographic object support to PostgreSQL, turning it
  5. into a spatial database. :ref:`geosbuild`, :ref:`proj4` and
  6. :ref:`gdalbuild` should be installed prior to building PostGIS. You
  7. might also need additional libraries, see `PostGIS requirements`_.
  8. .. note::
  9. The `psycopg2`_ module is required for use as the database adapter
  10. when using GeoDjango with PostGIS.
  11. .. _psycopg2: http://initd.org/psycopg/
  12. .. _PostGIS requirements: http://www.postgis.org/documentation/manual-2.0/postgis_installation.html#id554707
  13. On Debian/Ubuntu, you are advised to install the following packages:
  14. postgresql-x.x, postgresql-x.x-postgis, postgresql-server-dev-x.x,
  15. python-psycopg2 (x.x matching the PostgreSQL version you want to install).
  16. Please also consult platform-specific instructions if you are on :ref:`macosx`
  17. or :ref:`windows`.
  18. Building from source
  19. ====================
  20. First download the source archive, and extract::
  21. $ wget http://download.osgeo.org/postgis/source/postgis-2.0.3.tar.gz
  22. $ tar xzf postgis-2.0.3.tar.gz
  23. $ cd postgis-2.0.3
  24. Next, configure, make and install PostGIS::
  25. $ ./configure
  26. Finally, make and install::
  27. $ make
  28. $ sudo make install
  29. $ cd ..
  30. .. note::
  31. GeoDjango does not automatically create a spatial database. Please consult
  32. the section on :ref:`spatialdb_template91` or
  33. :ref:`spatialdb_template_earlier` for more information.
  34. __ http://postgis.net/
  35. Post-installation
  36. =================
  37. .. _spatialdb_template:
  38. .. _spatialdb_template91:
  39. Creating a spatial database with PostGIS 2.0 and PostgreSQL 9.1+
  40. ----------------------------------------------------------------
  41. PostGIS 2 includes an extension for Postgres 9.1+ that can be used to enable
  42. spatial functionality::
  43. $ createdb <db name>
  44. $ psql <db name>
  45. > CREATE EXTENSION postgis;
  46. > CREATE EXTENSION postgis_topology;
  47. No PostGIS topology functionalities are yet available from GeoDjango, so the
  48. creation of the ``postgis_topology`` extension is entirely optional.
  49. .. versionchanged:: 1.8
  50. The ``CREATE EXTENSION postgis`` command is now automatically run during
  51. the :djadmin:`migrate` process. You can still create it manually if you
  52. wish.
  53. .. _spatialdb_template_earlier:
  54. Creating a spatial database template for earlier versions
  55. ---------------------------------------------------------
  56. If you have an earlier version of PostGIS or PostgreSQL, the CREATE
  57. EXTENSION isn't available and you need to create the spatial database
  58. using the following instructions.
  59. Creating a spatial database with PostGIS is different than normal because
  60. additional SQL must be loaded to enable spatial functionality. Because of
  61. the steps in this process, it's better to create a database template that
  62. can be reused later.
  63. First, you need to be able to execute the commands as a privileged database
  64. user. For example, you can use the following to become the ``postgres`` user::
  65. $ sudo su - postgres
  66. .. note::
  67. The location *and* name of the PostGIS SQL files (e.g., from
  68. ``POSTGIS_SQL_PATH`` below) depends on the version of PostGIS.
  69. Version 1.5 uses ``<sharedir>/contrib/postgis-1.5/postgis.sql``.
  70. To complicate matters, Debian/Ubuntu distributions have their own separate
  71. directory naming system that might change with time. In this case, use the
  72. :download:`create_template_postgis-debian.sh` script.
  73. The example below assumes PostGIS 1.5, thus you may need to modify
  74. ``POSTGIS_SQL_PATH`` and the name of the SQL file for the specific
  75. version of PostGIS you are using.
  76. Once you're a database super user, then you may execute the following commands
  77. to create a PostGIS spatial database template::
  78. $ POSTGIS_SQL_PATH=`pg_config --sharedir`/contrib/postgis-2.0
  79. # Creating the template spatial database.
  80. $ createdb -E UTF8 template_postgis
  81. $ createlang -d template_postgis plpgsql # Adding PLPGSQL language support.
  82. # Allows non-superusers the ability to create from this template
  83. $ psql -d postgres -c "UPDATE pg_database SET datistemplate='true' WHERE datname='template_postgis';"
  84. # Loading the PostGIS SQL routines
  85. $ psql -d template_postgis -f $POSTGIS_SQL_PATH/postgis.sql
  86. $ psql -d template_postgis -f $POSTGIS_SQL_PATH/spatial_ref_sys.sql
  87. # Enabling users to alter spatial tables.
  88. $ psql -d template_postgis -c "GRANT ALL ON geometry_columns TO PUBLIC;"
  89. $ psql -d template_postgis -c "GRANT ALL ON geography_columns TO PUBLIC;"
  90. $ psql -d template_postgis -c "GRANT ALL ON spatial_ref_sys TO PUBLIC;"
  91. These commands may be placed in a shell script for later use; for convenience
  92. the following scripts are available:
  93. =============== =============================================
  94. PostGIS version Bash shell script
  95. =============== =============================================
  96. 1.5 :download:`create_template_postgis-1.5.sh`
  97. Debian/Ubuntu :download:`create_template_postgis-debian.sh`
  98. =============== =============================================
  99. Afterwards, you may create a spatial database by simply specifying
  100. ``template_postgis`` as the template to use (via the ``-T`` option)::
  101. $ createdb -T template_postgis <db name>
  102. .. note::
  103. While the ``createdb`` command does not require database super-user privileges,
  104. it must be executed by a database user that has permissions to create databases.
  105. You can create such a user with the following command::
  106. $ createuser --createdb <user>
  107. PostgreSQL's createdb fails
  108. ---------------------------
  109. When the PostgreSQL cluster uses a non-UTF8 encoding, the
  110. :file:`create_template_postgis-*.sh` script will fail when executing
  111. ``createdb``::
  112. createdb: database creation failed: ERROR: new encoding (UTF8) is incompatible
  113. with the encoding of the template database (SQL_ASCII)
  114. The `current workaround`__ is to re-create the cluster using UTF8 (back up any
  115. databases before dropping the cluster).
  116. __ http://jacobian.org/writing/pg-encoding-ubuntu/
  117. Managing the database
  118. ---------------------
  119. To administer the database, you can either use the pgAdmin III program
  120. (:menuselection:`Start --> PostgreSQL 9.x --> pgAdmin III`) or the
  121. SQL Shell (:menuselection:`Start --> PostgreSQL 9.x --> SQL Shell`).
  122. For example, to create a ``geodjango`` spatial database and user, the following
  123. may be executed from the SQL Shell as the ``postgres`` user::
  124. postgres# CREATE USER geodjango PASSWORD 'my_passwd';
  125. postgres# CREATE DATABASE geodjango OWNER geodjango TEMPLATE template_postgis ENCODING 'utf8';