“Django/docs/3.0.x/ref/contrib/gis/install/postgis”的版本间差异
来自菜鸟教程
Django/docs/3.0.x/ref/contrib/gis/install/postgis
(autoload) |
小 (Page commit) |
||
第1行: | 第1行: | ||
+ | {{DISPLAYTITLE:安装 PostGIS — Django 文档}} | ||
<div id="installing-postgis" class="section"> | <div id="installing-postgis" class="section"> | ||
− | = | + | = 安装 PostGIS = |
− | [https://postgis.net/ PostGIS] | + | [https://postgis.net/ PostGIS] 为PostgreSQL 增加了地理对象支持,把它变成了一个空间数据库。 [[../geolibs#geosbuild|GEOS]]、[[../geolibs#proj4|PROJ.4]] 和 [[../geolibs#gdalbuild|GDAL]] 应该在构建 PostGIS 之前安装。 您可能还需要其他库,请参阅 [https://postgis.net/docs/postgis_installation.html#install_requirements PostGIS 要求] 。 |
− | |||
− | [[../geolibs#gdalbuild| | ||
− | |||
− | + | 当使用带有 PostGIS 的 GeoDjango 时,需要 [https://www.psycopg.org/ psycopg2] 模块用作数据库适配器。 | |
− | |||
− | + | 在 Debian/Ubuntu 上,建议您安装以下软件包:postgresql-xx、postgresql-xx-postgis、postgresql-server-dev-xx、python-psycopg2(xx 与您要安装的 PostgreSQL 版本匹配)。 或者,您可以 [https://postgis.net/docs/postgis_installation.html#install_short_version 从源代码] 构建。 如果您使用的是 [[../index#macos|macOS]] 或 [[../index#windows|Windows]],请查阅特定于平台的说明。 | |
− | |||
− | |||
− | |||
− | |||
<div id="post-installation" class="section"> | <div id="post-installation" class="section"> | ||
− | == | + | == 安装后 == |
<div id="creating-a-spatial-database" class="section"> | <div id="creating-a-spatial-database" class="section"> | ||
<span id="spatialdb-template"></span> | <span id="spatialdb-template"></span> | ||
− | === | + | === 创建空间数据库 === |
− | PostGIS 2 | + | PostGIS 2 包括用于启用空间功能的 PostgreSQL 扩展: |
− | |||
<div class="highlight-default notranslate"> | <div class="highlight-default notranslate"> | ||
第33行: | 第25行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python">$ createdb <db name> |
− | $ psql | + | $ psql <db name> |
− | + | > CREATE EXTENSION postgis;</syntaxhighlight> | |
</div> | </div> | ||
</div> | </div> | ||
− | + | 数据库用户必须是超级用户才能运行 <code>CREATE EXTENSION postgis;</code>。 该命令在 [[#id1|:djadmin:`migrate`]] 过程中运行。 另一种方法是在您的项目中使用迁移操作: | |
− | <code>CREATE EXTENSION postgis;</code> | ||
− | |||
<div class="highlight-default notranslate"> | <div class="highlight-default notranslate"> | ||
第48行: | 第38行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python">from django.contrib.postgres.operations import CreateExtension |
from django.db import migrations | from django.db import migrations | ||
第56行: | 第46行: | ||
CreateExtension('postgis'), | CreateExtension('postgis'), | ||
... | ... | ||
− | ]</ | + | ]</syntaxhighlight> |
</div> | </div> | ||
</div> | </div> | ||
− | GeoDjango | + | GeoDjango 目前没有利用任何 [https://postgis.net/docs/Topology.html PostGIS 拓扑功能] 。 如果您打算在某个时候使用这些功能,您还可以通过发出 <code>CREATE EXTENSION postgis_topology;</code> 来安装 <code>postgis_topology</code> 扩展。 |
− | |||
− | <code>postgis_topology</code> | ||
第71行: | 第59行: | ||
=== 管理数据库 === | === 管理数据库 === | ||
− | + | 要管理数据库,您可以使用 pgAdmin III 程序 (<span class="menuselection">Start ‣ PostgreSQL 9.x ‣ pgAdmin III</span>) 或 SQL Shell (<span class="menuselection">Start ‣ PostgreSQL 9.x ‣ SQL Shell</span> )。 例如,要创建 <code>geodjango</code> 空间数据库和用户,可以作为 <code>postgres</code> 用户从 SQL Shell 执行以下操作: | |
− | (<span class="menuselection">Start ‣ PostgreSQL 9.x ‣ pgAdmin III</span>) | ||
− | SQL Shell (<span class="menuselection">Start ‣ PostgreSQL 9.x ‣ SQL Shell</span> | ||
− | |||
− | |||
<div class="highlight-default notranslate"> | <div class="highlight-default notranslate"> | ||
第81行: | 第65行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python">postgres# CREATE USER geodjango PASSWORD 'my_passwd'; |
− | postgres# CREATE DATABASE geodjango OWNER geodjango;</ | + | postgres# CREATE DATABASE geodjango OWNER geodjango;</syntaxhighlight> |
</div> | </div> | ||
第93行: | 第77行: | ||
</div> | </div> | ||
+ | <div class="clearer"> | ||
− | [[Category:Django 3.0.x | + | |
+ | |||
+ | </div> | ||
+ | |||
+ | [[Category:Django 3.0.x 文档]] |
2021年10月31日 (日) 04:09的最新版本
安装 PostGIS
PostGIS 为PostgreSQL 增加了地理对象支持,把它变成了一个空间数据库。 GEOS、PROJ.4 和 GDAL 应该在构建 PostGIS 之前安装。 您可能还需要其他库,请参阅 PostGIS 要求 。
当使用带有 PostGIS 的 GeoDjango 时,需要 psycopg2 模块用作数据库适配器。
在 Debian/Ubuntu 上,建议您安装以下软件包:postgresql-xx、postgresql-xx-postgis、postgresql-server-dev-xx、python-psycopg2(xx 与您要安装的 PostgreSQL 版本匹配)。 或者,您可以 从源代码 构建。 如果您使用的是 macOS 或 Windows,请查阅特定于平台的说明。
安装后
创建空间数据库
PostGIS 2 包括用于启用空间功能的 PostgreSQL 扩展:
$ createdb <db name>
$ psql <db name>
> CREATE EXTENSION postgis;
数据库用户必须是超级用户才能运行 CREATE EXTENSION postgis;
。 该命令在 :djadmin:`migrate` 过程中运行。 另一种方法是在您的项目中使用迁移操作:
from django.contrib.postgres.operations import CreateExtension
from django.db import migrations
class Migration(migrations.Migration):
operations = [
CreateExtension('postgis'),
...
]
GeoDjango 目前没有利用任何 PostGIS 拓扑功能 。 如果您打算在某个时候使用这些功能,您还可以通过发出 CREATE EXTENSION postgis_topology;
来安装 postgis_topology
扩展。
管理数据库
要管理数据库,您可以使用 pgAdmin III 程序 (geodjango
空间数据库和用户,可以作为 postgres
用户从 SQL Shell 执行以下操作:
postgres# CREATE USER geodjango PASSWORD 'my_passwd';
postgres# CREATE DATABASE geodjango OWNER geodjango;