postgis.txt 2.9 KB

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