Django/docs/3.0.x/ref/contrib/gis/install/postgis

来自菜鸟教程
Django/docs/3.0.x/ref/contrib/gis/install/postgis /
机器人讨论 | 贡献2020年12月2日 (三) 08:15的版本 (autoload)
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
跳转至:导航、​搜索

安装PostGIS

PostGIS adds geographic object support to PostgreSQL, turning it into a spatial database. GEOS, PROJ.4 and GDAL should be installed prior to building PostGIS. You might also need additional libraries, see PostGIS requirements.

The psycopg2 module is required for use as the database adapter when using GeoDjango with PostGIS.

On Debian/Ubuntu, you are advised to install the following packages: postgresql-x.x, postgresql-x.x-postgis, postgresql-server-dev-x.x, 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 macOS or Windows.

Post-installation

Creating a spatial database

PostGIS 2 includes an extension for PostgreSQL that's used to enable spatial functionality:

$ createdb  <db name>
$ psql <db name>
> CREATE EXTENSION postgis;

The database user must be a superuser in order to run CREATE EXTENSION postgis;. The command is run during the migrate process. An alternative is to use a migration operation in your project:

from django.contrib.postgres.operations import CreateExtension
from django.db import migrations

class Migration(migrations.Migration):

    operations = [
        CreateExtension('postgis'),
        ...
    ]

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;.


管理数据库

To administer the database, you can either use the pgAdmin III program (Start ‣ PostgreSQL 9.x ‣ pgAdmin III) or the SQL Shell (Start ‣ PostgreSQL 9.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:

postgres# CREATE USER geodjango PASSWORD 'my_passwd';
postgres# CREATE DATABASE geodjango OWNER geodjango;