{"title":"安装 PostGIS","version":"5.2","locale":"zh-hans","docname":"ref/contrib/gis/install/postgis","url":"/zh-hans/5.2/ref/contrib/gis/install/postgis/","canonical":"https://djangodocs.dev/zh-hans/5.2/ref/contrib/gis/install/postgis/","summary":"GEOS、PROJ 和 GDAL 应在构建 PostGIS 之前安装。你可能还需要额外的库，参见 PostGIS 要求。","html":"<h1>安装 PostGIS<a class=\"heading-anchor\" href=\"#installing-postgis\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<p><a class=\"reference internal\" href=\"/zh-hans/5.2/ref/contrib/gis/install/geolibs/#geosbuild\"><span class=\"std std-ref\">GEOS</span></a>、<a class=\"reference internal\" href=\"/zh-hans/5.2/ref/contrib/gis/install/geolibs/#proj4\"><span class=\"std std-ref\">PROJ</span></a> 和 <a class=\"reference internal\" href=\"/zh-hans/5.2/ref/contrib/gis/install/geolibs/#gdalbuild\"><span class=\"std std-ref\">GDAL</span></a> 应在构建 <a class=\"reference external\" href=\"https://postgis.net/\">PostGIS</a> 之前安装。你可能还需要额外的库，参见 <a class=\"reference external\" href=\"https://postgis.net/docs/postgis_installation.html#install_requirements\">PostGIS 要求</a> 。</p>\n<p>使用 PostGIS 的 GeoDjango 需要 <a class=\"reference external\" href=\"https://www.psycopg.org/psycopg3/\">psycopg</a> 或 <a class=\"reference external\" href=\"https://www.psycopg.org/\">psycopg2</a> 模块作为数据库适配器。</p>\n<p>在 Debian/Ubuntu 上，建议安装以下软件包：<code class=\"docutils literal notranslate\">postgresql-x</code>、<code class=\"docutils literal notranslate\">postgresql-x-postgis-3</code>、<code class=\"docutils literal notranslate\">postgresql-server-dev-x</code> 和 <code class=\"docutils literal notranslate\">python3-psycopg3</code> （x 对应您想要安装的 PostgreSQL 版本）。或者，您可以使用 <a class=\"reference external\" href=\"https://postgis.net/docs/postgis_installation.html#install_short_version\">从源代码构建</a> 的方法。如果您使用的是 <a class=\"reference internal\" href=\"/zh-hans/5.2/ref/contrib/gis/install/#macos\"><span class=\"std std-ref\">macOS</span></a> 或 <a class=\"reference internal\" href=\"/zh-hans/5.2/ref/contrib/gis/install/#windows\"><span class=\"std std-ref\">Windows</span></a>，请参考平台特定的说明。</p>\n<section id=\"post-installation\">\n<h2>安装后<a class=\"heading-anchor\" href=\"#post-installation\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<section id=\"creating-a-spatial-database\">\n<span id=\"spatialdb-template\"></span><h3>创建一个空间数据库<a class=\"heading-anchor\" href=\"#creating-a-spatial-database\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>PostGIS 包含一个用于启用空间功能的 PostgreSQL 扩展：</p>\n<div class=\"code-block\" data-language=\"shell\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Shell code\"><code>$ createdb  &lt;db name&gt;\n$ psql &lt;db name&gt;\n&gt; CREATE EXTENSION postgis<span class=\"p\">;</span>\n</code></pre></div>\n<p>数据库用户必须是超级用户，才能运行 <code class=\"docutils literal notranslate\">CREATE EXTENSION postgis;</code>。该命令是在 <a class=\"reference internal\" href=\"/zh-hans/5.2/ref/django-admin/#django-admin-migrate\"><code class=\"xref std std-djadmin docutils literal notranslate\">migrate</code></a> 过程中运行的。另一种方法是在项目中使用迁移操作：</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span> <span class=\"nn\">django.contrib.postgres.operations</span> <span class=\"kn\">import</span> CreateExtension\n<span class=\"kn\">from</span> <span class=\"nn\">django.db</span> <span class=\"kn\">import</span> migrations\n\n\n<span class=\"k\">class</span> <span class=\"nc\">Migration</span><span class=\"p\">(</span>migrations<span class=\"o\">.</span>Migration<span class=\"p\">):</span>\n    operations <span class=\"o\">=</span> <span class=\"p\">[</span>CreateExtension<span class=\"p\">(</span><span class=\"s2\">&quot;postgis&quot;</span><span class=\"p\">),</span> <span class=\"o\">...</span><span class=\"p\">]</span>\n</code></pre></div>\n<p>如果你计划使用 PostGIS 栅格功能，还应激活 <code class=\"docutils literal notranslate\">postgis_raster</code> 扩展。你可以使用 <a class=\"reference internal\" href=\"/zh-hans/5.2/ref/contrib/postgres/operations/#django.contrib.postgres.operations.CreateExtension\" title=\"django.contrib.postgres.operations.CreateExtension\"><code class=\"xref py py-class docutils literal notranslate\">CreateExtension</code></a> 迁移操作安装该扩展，或直接运行 <code class=\"docutils literal notranslate\">CREATE EXTENSION postgis_raster;</code>。</p>\n<p>GeoDjango 目前没有利用任何 PostGIS 拓扑功能。如果你打算在某些时候使用这些功能，你也可以通过发出 <code class=\"docutils literal notranslate\">CREATE EXTENSION postgis_topology;</code> 来安装 <code class=\"docutils literal notranslate\">postgis_topology</code> 扩展。</p>\n</section>\n<section id=\"managing-the-database\">\n<h3>管理数据库<a class=\"heading-anchor\" href=\"#managing-the-database\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>要管理数据库，您可以使用 pgAdmin III 程序 (<span class=\"menuselection\">开始 ‣ PostgreSQL X ‣ pgAdmin III</span>) 或 SQL Shell (<span class=\"menuselection\">开始 ‣ PostgreSQL X ‣ SQL Shell</span>)。例如，要在 SQL Shell 中以 <code class=\"docutils literal notranslate\">postgres</code> 用户的身份创建一个名为 <code class=\"docutils literal notranslate\">geodjango</code> 的空间数据库和用户，可以执行以下命令：</p>\n<div class=\"code-block\" data-language=\"psql\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">SQL</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"SQL code\"><code>postgres<span class=\"o\">#</span> <span class=\"k\">CREATE</span> <span class=\"k\">USER</span> geodjango <span class=\"k\">PASSWORD</span> <span class=\"s1\">&#39;my_passwd&#39;</span><span class=\"p\">;</span>\n<span class=\"go\">postgres# CREATE DATABASE geodjango OWNER geodjango;</span>\n</code></pre></div>\n</section>\n</section>","rootId":"installing-postgis","toc":[{"title":"安装后","anchor":"post-installation","children":[{"title":"创建一个空间数据库","anchor":"creating-a-spatial-database","children":[]},{"title":"管理数据库","anchor":"managing-the-database","children":[]}]}],"breadcrumbs":[{"docname":"ref/index","title":"API 参考","url":"/zh-hans/5.2/ref/"},{"docname":"ref/contrib/index","title":"contrib 包","url":"/zh-hans/5.2/ref/contrib/"},{"docname":"ref/contrib/gis/index","title":"GeoDjango","url":"/zh-hans/5.2/ref/contrib/gis/"},{"docname":"ref/contrib/gis/install/index","title":"GeoDjango 安装","url":"/zh-hans/5.2/ref/contrib/gis/install/"}],"prev":{"docname":"ref/contrib/gis/install/geolibs","title":"安装地理空间库","url":"/zh-hans/5.2/ref/contrib/gis/install/geolibs/"},"next":{"docname":"ref/contrib/gis/install/spatialite","title":"安装 SpatiaLite","url":"/zh-hans/5.2/ref/contrib/gis/install/spatialite/"},"formats":{"html":"/zh-hans/5.2/ref/contrib/gis/install/postgis/","markdown":"/zh-hans/5.2/ref/contrib/gis/install/postgis.md","json":"/zh-hans/5.2/ref/contrib/gis/install/postgis.json"},"source":"https://github.com/django/django/blob/stable/5.2.x/docs/ref/contrib/gis/install/postgis.txt","official":"https://docs.djangoproject.com/zh-hans/5.2/ref/contrib/gis/install/postgis/","inVersions":["6.1","6.0","5.2","5.1","5.0","4.2","4.1","4.0","3.2","3.1","3.0","2.2","2.1","2.0"],"inLocales":["en","sv","zh-hans","ga","fr","ja","id","it","pt-br","ko","es","el","pl"]}