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