{"title":"Installing PostGIS","version":"1.8","locale":"en","docname":"ref/contrib/gis/install/postgis","url":"/en/1.8/ref/contrib/gis/install/postgis/","canonical":"https://djangodocs.dev/en/1.8/ref/contrib/gis/install/postgis/","summary":"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.","html":"<h1>Installing 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 external\" href=\"http://postgis.net/\">PostGIS</a> adds geographic object support to PostgreSQL, turning it\ninto a spatial database. <a class=\"reference internal\" href=\"/en/1.8/ref/contrib/gis/install/geolibs/#geosbuild\"><span class=\"std std-ref\">GEOS</span></a>, <a class=\"reference internal\" href=\"/en/1.8/ref/contrib/gis/install/geolibs/#proj4\"><span class=\"std std-ref\">PROJ.4</span></a> and\n<a class=\"reference internal\" href=\"/en/1.8/ref/contrib/gis/install/geolibs/#gdalbuild\"><span class=\"std std-ref\">GDAL</span></a> should be installed prior to building PostGIS. You\nmight also need additional libraries, see <a class=\"reference external\" href=\"http://postgis.net/docs/manual-2.1/postgis_installation.html#install_requirements\">PostGIS requirements</a>.</p>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Note</p>\n<p>The <a class=\"reference external\" href=\"http://initd.org/psycopg/\">psycopg2</a> module is required for use as the database adapter\nwhen using GeoDjango with PostGIS.</p>\n</aside>\n<p>On Debian/Ubuntu, you are advised to install the following packages:\npostgresql-x.x, postgresql-x.x-postgis, postgresql-server-dev-x.x,\npython-psycopg2 (x.x matching the PostgreSQL version you want to install).\nPlease also consult platform-specific instructions if you are on <a class=\"reference internal\" href=\"/en/1.8/ref/contrib/gis/install/#macosx\"><span class=\"std std-ref\">Mac OS X</span></a>\nor <a class=\"reference internal\" href=\"/en/1.8/ref/contrib/gis/testing/#windows\"><span class=\"std std-ref\">Windows</span></a>.</p>\n<section id=\"building-from-source\">\n<h2>Building from source<a class=\"heading-anchor\" href=\"#building-from-source\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>First download the source archive, and extract:</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>$ wget http://download.osgeo.org/postgis/source/postgis-2.1.5.tar.gz\n$ tar xzf postgis-2.1.5.tar.gz\n$ cd postgis-2.1.5\n</code></pre></div>\n<p>Next, configure, make and install PostGIS:</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>$ ./configure\n</code></pre></div>\n<p>Finally, make and install:</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>$ make\n$ sudo make install\n$ cd ..\n</code></pre></div>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Note</p>\n<p>GeoDjango does not automatically create a spatial database.  Please consult\nthe section on <a class=\"reference internal\" href=\"#spatialdb-template91\"><span class=\"std std-ref\">Creating a spatial database with PostGIS 2.0 and PostgreSQL 9.1+</span></a> or\n<a class=\"reference internal\" href=\"#spatialdb-template-earlier\"><span class=\"std std-ref\">Creating a spatial database template for earlier versions</span></a> for more information.</p>\n</aside>\n</section>\n<section id=\"post-installation\">\n<h2>Post-installation<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-with-postgis-2-0-and-postgresql-9-1\">\n<span id=\"spatialdb-template91\"></span><span id=\"spatialdb-template\"></span><h3>Creating a spatial database with PostGIS 2.0 and PostgreSQL 9.1+<a class=\"heading-anchor\" href=\"#creating-a-spatial-database-with-postgis-2-0-and-postgresql-9-1\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>PostGIS 2 includes an extension for Postgres 9.1+ that can be used to enable\nspatial functionality:</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>$ createdb  &lt;db name&gt;\n$ psql &lt;db name&gt;\n&gt; CREATE EXTENSION postgis;\n</code></pre></div>\n<p>GeoDjango does not currently leverage any <a class=\"reference external\" href=\"http://postgis.net/docs/Topology.html\">PostGIS topology functionality</a>.\nIf you plan to use those features at some point, you can also install the\n<code class=\"docutils literal notranslate\">postgis_topology</code> extension by issuing <code class=\"docutils literal notranslate\">CREATE EXTENSION\npostgis_topology;</code>.</p>\n<aside class=\"version-note version-changed\" data-version=\"1.8\" role=\"note\">\n<p class=\"version-note-title\">Changed in Django 1.8</p><p>The <code class=\"docutils literal notranslate\">CREATE EXTENSION postgis</code> command is now automatically run during\nthe <a class=\"reference internal\" href=\"/en/1.8/ref/django-admin/#django-admin-migrate\"><code class=\"xref std std-djadmin docutils literal notranslate\">migrate</code></a> process. You can still create it manually if you\nwish.</p>\n</aside>\n</section>\n<section id=\"creating-a-spatial-database-template-for-earlier-versions\">\n<span id=\"spatialdb-template-earlier\"></span><h3>Creating a spatial database template for earlier versions<a class=\"heading-anchor\" href=\"#creating-a-spatial-database-template-for-earlier-versions\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>If you have an earlier version of PostGIS or PostgreSQL, the CREATE\nEXTENSION isn’t available and you need to create the spatial database\nusing the following instructions.</p>\n<p>Creating a spatial database with PostGIS is different than normal because\nadditional SQL must be loaded to enable spatial functionality.  Because of\nthe steps in this process, it’s better to create a database template that\ncan be reused later.</p>\n<p>First, you need to be able to execute the commands as a privileged database\nuser.  For example, you can use the following to become the <code class=\"docutils literal notranslate\">postgres</code> user:</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>$ sudo su - postgres\n</code></pre></div>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Note</p>\n<p>The location <em>and</em> name of the PostGIS SQL files (e.g., from\n<code class=\"docutils literal notranslate\">POSTGIS_SQL_PATH</code> below) depends on the version of PostGIS.\nVersion 1.5 uses <code class=\"docutils literal notranslate\">&lt;sharedir&gt;/contrib/postgis-1.5/postgis.sql</code>.</p>\n<p>To complicate matters, Debian/Ubuntu distributions have their own separate\ndirectory naming system that might change with time. In this case, use the\n<a class=\"reference download internal\" download=\"\" href=\"/en/1.8/_downloads/8674e8a0ac2379175f5872433aeb6f9c/create_template_postgis-debian.sh\"><code class=\"xref download docutils literal notranslate\">create_template_postgis-debian.sh</code></a> script.</p>\n<p>The example below assumes PostGIS 1.5, thus you may need to modify\n<code class=\"docutils literal notranslate\">POSTGIS_SQL_PATH</code> and the name of the SQL file for the specific\nversion of PostGIS you are using.</p>\n</aside>\n<p>Once you’re a database super user, then you may execute the following commands\nto create a PostGIS spatial database template:</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>$ POSTGIS_SQL_PATH=`pg_config --sharedir`/contrib/postgis-2.0\n# Creating the template spatial database.\n$ createdb -E UTF8 template_postgis\n$ createlang -d template_postgis plpgsql # Adding PLPGSQL language support.\n# Allows non-superusers the ability to create from this template\n$ psql -d postgres -c &quot;UPDATE pg_database SET datistemplate=&#39;true&#39; WHERE datname=&#39;template_postgis&#39;;&quot;\n# Loading the PostGIS SQL routines\n$ psql -d template_postgis -f $POSTGIS_SQL_PATH/postgis.sql\n$ psql -d template_postgis -f $POSTGIS_SQL_PATH/spatial_ref_sys.sql\n# Enabling users to alter spatial tables.\n$ psql -d template_postgis -c &quot;GRANT ALL ON geometry_columns TO PUBLIC;&quot;\n$ psql -d template_postgis -c &quot;GRANT ALL ON geography_columns TO PUBLIC;&quot;\n$ psql -d template_postgis -c &quot;GRANT ALL ON spatial_ref_sys TO PUBLIC;&quot;\n</code></pre></div>\n<p>These commands may be placed in a shell script for later use; for convenience\nthe following scripts are available:</p>\n<div class=\"table-scroll\" role=\"region\" tabindex=\"0\" aria-label=\"Table\"><table class=\"docutils align-default\">\n<thead>\n<tr class=\"row-odd\"><th class=\"head\"><p>PostGIS version</p></th>\n<th class=\"head\"><p>Bash shell script</p></th>\n</tr>\n</thead>\n<tbody>\n<tr class=\"row-even\"><td><p>1.5</p></td>\n<td><p><a class=\"reference download internal\" download=\"\" href=\"/en/1.8/_downloads/f0a3beb6a268a071bb65ce2e42150d97/create_template_postgis-1.5.sh\"><code class=\"xref download docutils literal notranslate\">create_template_postgis-1.5.sh</code></a></p></td>\n</tr>\n<tr class=\"row-odd\"><td><p>Debian/Ubuntu</p></td>\n<td><p><a class=\"reference download internal\" download=\"\" href=\"/en/1.8/_downloads/8674e8a0ac2379175f5872433aeb6f9c/create_template_postgis-debian.sh\"><code class=\"xref download docutils literal notranslate\">create_template_postgis-debian.sh</code></a></p></td>\n</tr>\n</tbody>\n</table>\n</div>\n<p>Afterwards, you may create a spatial database by simply specifying\n<code class=\"docutils literal notranslate\">template_postgis</code> as the template to use (via the <code class=\"docutils literal notranslate\">-T</code> option):</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>$ createdb -T template_postgis &lt;db name&gt;\n</code></pre></div>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Note</p>\n<p>While the <code class=\"docutils literal notranslate\">createdb</code> command does not require database super-user privileges,\nit must be executed by a database user that has permissions to create databases.\nYou can create such a user with the following command:</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>$ createuser --createdb &lt;user&gt;\n</code></pre></div>\n</aside>\n</section>\n<section id=\"postgresql-s-createdb-fails\">\n<h3>PostgreSQL’s createdb fails<a class=\"heading-anchor\" href=\"#postgresql-s-createdb-fails\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>When the PostgreSQL cluster uses a non-UTF8 encoding, the\n<code class=\"file docutils literal notranslate\">create_template_postgis-*.sh</code> script will fail when executing\n<code class=\"docutils literal notranslate\">createdb</code>:</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>createdb<span class=\"p\">:</span> database creation failed<span class=\"p\">:</span> ERROR<span class=\"p\">:</span>  new encoding <span class=\"p\">(</span>UTF8<span class=\"p\">)</span> <span class=\"ow\">is</span> incompatible\n  <span class=\"k\">with</span> the encoding of the template database <span class=\"p\">(</span>SQL_ASCII<span class=\"p\">)</span>\n</code></pre></div>\n<p>The <a class=\"reference external\" href=\"http://jacobian.org/writing/pg-encoding-ubuntu/\">current workaround</a> is to re-create the cluster using UTF8 (back up any\ndatabases before dropping the cluster).</p>\n</section>\n<section id=\"managing-the-database\">\n<h3>Managing the database<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>To administer the database, you can either use the pgAdmin III program\n(<span class=\"menuselection\">Start ‣ PostgreSQL 9.x ‣ pgAdmin III</span>) or the\nSQL Shell (<span class=\"menuselection\">Start ‣ PostgreSQL 9.x ‣ SQL Shell</span>).\nFor example, to create a <code class=\"docutils literal notranslate\">geodjango</code> spatial database and user, the following\nmay be executed from the SQL Shell as the <code class=\"docutils literal notranslate\">postgres</code> user:</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>postgres<span class=\"c1\"># CREATE USER geodjango PASSWORD &#39;my_passwd&#39;;</span>\npostgres<span class=\"c1\"># CREATE DATABASE geodjango OWNER geodjango TEMPLATE template_postgis ENCODING &#39;utf8&#39;;</span>\n</code></pre></div>\n</section>\n</section>","rootId":"installing-postgis","toc":[{"title":"Building from source","anchor":"building-from-source","children":[]},{"title":"Post-installation","anchor":"post-installation","children":[{"title":"Creating a spatial database with PostGIS 2.0 and PostgreSQL 9.1+","anchor":"creating-a-spatial-database-with-postgis-2-0-and-postgresql-9-1","children":[]},{"title":"Creating a spatial database template for earlier versions","anchor":"creating-a-spatial-database-template-for-earlier-versions","children":[]},{"title":"PostgreSQL’s createdb fails","anchor":"postgresql-s-createdb-fails","children":[]},{"title":"Managing the database","anchor":"managing-the-database","children":[]}]}],"breadcrumbs":[{"docname":"ref/index","title":"API Reference","url":"/en/1.8/ref/"},{"docname":"ref/contrib/index","title":"contrib packages","url":"/en/1.8/ref/contrib/"},{"docname":"ref/contrib/gis/index","title":"GeoDjango","url":"/en/1.8/ref/contrib/gis/"},{"docname":"ref/contrib/gis/install/index","title":"GeoDjango Installation","url":"/en/1.8/ref/contrib/gis/install/"}],"prev":{"docname":"ref/contrib/gis/install/geolibs","title":"Installing Geospatial libraries","url":"/en/1.8/ref/contrib/gis/install/geolibs/"},"next":{"docname":"ref/contrib/gis/install/spatialite","title":"Installing Spatialite","url":"/en/1.8/ref/contrib/gis/install/spatialite/"},"formats":{"html":"/en/1.8/ref/contrib/gis/install/postgis/","markdown":"/en/1.8/ref/contrib/gis/install/postgis.md","json":"/en/1.8/ref/contrib/gis/install/postgis.json"},"source":"https://github.com/django/django/blob/stable/1.8.x/docs/ref/contrib/gis/install/postgis.txt","official":"https://docs.djangoproject.com/en/1.8/ref/contrib/gis/install/postgis/","inVersions":["dev","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","1.11","1.10","1.9","1.8"],"inLocales":["en"]}