postgis.txt 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. Post-installation
  21. =================
  22. .. _spatialdb_template:
  23. Creating a spatial database
  24. ---------------------------
  25. PostGIS includes an extension for PostgreSQL that's used to enable spatial
  26. functionality:
  27. .. code-block:: shell
  28. $ createdb <db name>
  29. $ psql <db name>
  30. > CREATE EXTENSION postgis;
  31. The database user must be a superuser in order to run
  32. ``CREATE EXTENSION postgis;``. The command is run during the :djadmin:`migrate`
  33. process. An alternative is to use a migration operation in your project::
  34. from django.contrib.postgres.operations import CreateExtension
  35. from django.db import migrations
  36. class Migration(migrations.Migration):
  37. operations = [CreateExtension("postgis"), ...]
  38. If you plan to use PostGIS raster functionality, you should also activate the
  39. ``postgis_raster`` extension. You can install the extension using the
  40. :class:`~django.contrib.postgres.operations.CreateExtension` migration
  41. operation, or directly by running ``CREATE EXTENSION postgis_raster;``.
  42. GeoDjango does not currently leverage any `PostGIS topology functionality`__.
  43. If you plan to use those features at some point, you can also install the
  44. ``postgis_topology`` extension by issuing ``CREATE EXTENSION
  45. postgis_topology;``.
  46. __ https://postgis.net/docs/Topology.html
  47. Managing the database
  48. ---------------------
  49. To administer the database, you can either use the pgAdmin III program
  50. (:menuselection:`Start --> PostgreSQL X --> pgAdmin III`) or the SQL Shell
  51. (:menuselection:`Start --> PostgreSQL X --> SQL Shell`). For example, to create
  52. a ``geodjango`` spatial database and user, the following may be executed from
  53. the SQL Shell as the ``postgres`` user:
  54. .. code-block:: psql
  55. postgres# CREATE USER geodjango PASSWORD 'my_passwd';
  56. postgres# CREATE DATABASE geodjango OWNER geodjango;