2
0

postgis.txt 6.6 KB

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