“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 =
+
= 安装 PostGIS =
  
[https://postgis.net/ PostGIS] adds geographic object support to PostgreSQL, turning it
+
[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 要求]
into a spatial database. [[../geolibs#geosbuild|<span class="std std-ref">GEOS</span>]], [[../geolibs#proj4|<span class="std std-ref">PROJ.4</span>]] and
 
[[../geolibs#gdalbuild|<span class="std std-ref">GDAL</span>]] should be installed prior to building PostGIS. You
 
might also need additional libraries, see [https://postgis.net/docs/postgis_installation.html#install_requirements PostGIS requirements].
 
  
The [https://www.psycopg.org/ psycopg2] module is required for use as the database adapter when using
+
当使用带有 PostGIS 的 GeoDjango 时,需要 [https://www.psycopg.org/ psycopg2] 模块用作数据库适配器。
GeoDjango with PostGIS.
 
  
On Debian/Ubuntu, you are advised to install the following packages:
+
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]],请查阅特定于平台的说明。
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 [https://postgis.net/docs/postgis_installation.html#install_short_version build from source]. Consult the platform-specific
 
instructions if you are on [[../index#macos|<span class="std std-ref">macOS</span>]] or [[../index#windows|<span class="std std-ref">Windows</span>]].
 
  
 
<div id="post-installation" class="section">
 
<div id="post-installation" class="section">
  
== Post-installation ==
+
== 安装后 ==
  
 
<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>
=== Creating a spatial database ===
+
=== 创建空间数据库 ===
  
PostGIS 2 includes an extension for PostgreSQL that's used to enable spatial
+
PostGIS 2 包括用于启用空间功能的 PostgreSQL 扩展:
functionality:
 
  
 
<div class="highlight-default notranslate">
 
<div class="highlight-default notranslate">
第33行: 第25行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>$ createdb  &lt;db name&gt;
+
<syntaxhighlight lang="python">$ createdb  <db name>
$ psql &lt;db name&gt;
+
$ psql <db name>
&gt; CREATE EXTENSION postgis;</pre>
+
> CREATE EXTENSION postgis;</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
The database user must be a superuser in order to run
+
数据库用户必须是超级用户才能运行 <code>CREATE EXTENSION postgis;</code>。 该命令在 [[#id1|:djadmin:`migrate`]] 过程中运行。 另一种方法是在您的项目中使用迁移操作:
<code>CREATE EXTENSION postgis;</code>. The command is run during the [[../../../../django-admin#django-admin-migrate|<code>migrate</code>]]
 
process. An alternative is to use a migration operation in your project:
 
  
 
<div class="highlight-default notranslate">
 
<div class="highlight-default notranslate">
第48行: 第38行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>from django.contrib.postgres.operations import CreateExtension
+
<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'),
 
         ...
 
         ...
     ]</pre>
+
     ]</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
GeoDjango does not currently leverage any [https://postgis.net/docs/Topology.html PostGIS topology functionality].
+
GeoDjango 目前没有利用任何 [https://postgis.net/docs/Topology.html PostGIS 拓扑功能] 。 如果您打算在某个时候使用这些功能,您还可以通过发出 <code>CREATE EXTENSION postgis_topology;</code> 来安装 <code>postgis_topology</code> 扩展。
If you plan to use those features at some point, you can also install the
 
<code>postgis_topology</code> extension by issuing <code>CREATE EXTENSION postgis_topology;</code>.
 
  
  
第71行: 第59行:
 
=== 管理数据库 ===
 
=== 管理数据库 ===
  
To administer the database, you can either use the pgAdmin III program
+
要管理数据库,您可以使用 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>) or the
 
SQL Shell (<span class="menuselection">Start ‣ PostgreSQL 9.x ‣ SQL Shell</span>).
 
For example, to create a <code>geodjango</code> spatial database and user, the following
 
may be executed from the SQL Shell as the <code>postgres</code> user:
 
  
 
<div class="highlight-default notranslate">
 
<div class="highlight-default notranslate">
第81行: 第65行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>postgres# CREATE USER geodjango PASSWORD 'my_passwd';
+
<syntaxhighlight lang="python">postgres# CREATE USER geodjango PASSWORD 'my_passwd';
postgres# CREATE DATABASE geodjango OWNER geodjango;</pre>
+
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 增加了地理对象支持,把它变成了一个空间数据库。 GEOSPROJ.4GDAL 应该在构建 PostGIS 之前安装。 您可能还需要其他库,请参阅 PostGIS 要求

当使用带有 PostGIS 的 GeoDjango 时,需要 psycopg2 模块用作数据库适配器。

在 Debian/Ubuntu 上,建议您安装以下软件包:postgresql-xx、postgresql-xx-postgis、postgresql-server-dev-xx、python-psycopg2(xx 与您要安装的 PostgreSQL 版本匹配)。 或者,您可以 从源代码 构建。 如果您使用的是 macOSWindows,请查阅特定于平台的说明。

安装后

创建空间数据库

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 程序 (Start ‣ PostgreSQL 9.x ‣ pgAdmin III) 或 SQL Shell (Start ‣ PostgreSQL 9.x ‣ SQL Shell )。 例如,要创建 geodjango 空间数据库和用户,可以作为 postgres 用户从 SQL Shell 执行以下操作:

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