GEOS、PROJ 和 GDAL 应在构建 PostGIS 之前安装。你可能还需要额外的库,参见 PostGIS 要求 。
当使用 GeoDjango 和 PostGIS 时,需要使用 psycopg2 模块作为数据库适配器。
在 Debian/Ubuntu 上,建议你安装以下软件包:postgresql-x.x、postgresql-x.x-postgis、postgresql-server-dev-x.x、python-psycopg2(x.x 与你要安装的 PostgreSQL 版本匹配)。或者,你也可以 从源码编译 。如果你使用的是 macOS 或 Windows,请参考特定平台的说明。
PostGIS 2 包含了一个 PostgreSQL 的扩展,用来实现空间功能:
$ createdb <db name>
$ psql <db name>
> CREATE EXTENSION postgis;
数据库用户必须是超级用户,才能运行 CREATE EXTENSION postgis;。该命令是在 migrate 过程中运行的。另一种方法是在项目中使用迁移操作:
from django.contrib.postgres.operations import CreateExtension
from django.db import migrations
class Migration(migrations.Migration):
operations = [
CreateExtension('postgis'),
...
]
如果打算在 PostGIS 3+ 上使用 PostGIS 栅格功能,还应该激活 postgis_raster 扩展。你可以使用 CreateExtension 迁移操作来安装扩展,或者直接运行 CREATE EXTENSION postgis_raster;。
GeoDjango 目前没有利用任何 PostGIS 拓扑功能。如果你打算在某些时候使用这些功能,你也可以通过发出 CREATE EXTENSION postgis_topology; 来安装 postgis_topology 扩展。
To administer the database, you can either use the pgAdmin III program
() or the 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:
postgres# CREATE USER geodjango PASSWORD 'my_passwd';
postgres# CREATE DATABASE geodjango OWNER geodjango;
1月 12, 2022