Changes between Initial Version and Version 1 of UsersWikiPostGIS3UbuntuPGSQLApt


Ignore:
Timestamp:
May 29, 2022, 1:03:06 PM (2 years ago)
Author:
robe
Comment:

Legend:

Unmodified
Added
Removed
Modified
  • UsersWikiPostGIS3UbuntuPGSQLApt

    v1 v1  
     1The [http://wiki.postgresql.org/wiki/Apt PostgreSQL Apt Repository]  now hosts installs of PostGIS, pgRouting, pgpointcloud in addition to PostgreSQL.
     2
     3The following describes how to install PostgreSQL 14, PostGIS 3.2, pgRouting 3.4 on an Ubuntu/Debian system. It is assumed to also work on Linux Mint, Lubuntu, and Xubuntu.
     4
     5Run these in terminal:
     6
     7== Verify what version of Ubuntu you are running ==
     8{{{
     9sudo lsb_release -a
     10}}}
     11
     12
     13== Add Respository to sources.list ==
     14
     15You'll need to replace the nickname below with what you are running.
     16
     17
     18{{{
     19# prereqs needed for accept certs
     20apt install ca-certificates gnupg
     21}}}
     22
     23
     24=== Adds pgdg keys and main repo
     25{{{
     26curl https://www.postgresql.org/media/keys/ACCC4CF8.asc | gpg --dearmor | sudo tee /etc/apt/trusted.gpg.d/apt.postgresql.org.gpg >/dev/null
     27sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
     28
     29}}}
     30
     31=== Testing Beta Packages
     32# If you want to try beta packages like not yet released PG15 and PostGIS 3.3 versions, you can do
     33
     34{{{
     35sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg-testing main 15" > /etc/apt/sources.list.d/pgdg-testing.list'
     36}}}
     37
     38=== Allow dependencies to be installed
     39PostgreSQL relies on libpq and other things, sometimes your upstream packaging will get in the way.  To ensure PostgreSQL apt repo takes, precendence, create a file as follows
     40
     41{{{
     42# to allow dependencies to be installed
     43cat << EOF >> /etc/apt/preferences.d/pgdg.pref
     44Package: *
     45Pin: release o=apt.postgresql.org
     46Pin-Priority: 500
     47EOF
     48}}}
     49
     50
     51
     52=== Installing
     531. Then update your catalog and installed software
     54{{{
     55sudo apt update
     56sudo apt upgrade
     57}}}
     58
     592. Install PostgreSQL replacing 14 with the version you want
     60{{{
     61sudo apt install postgresql-14
     62}}}
     63
     64Apt system allows for installing multiple versions of PostgreSQL, so you could also install an additional, such as
     65{{{
     66sudo apt install postgresql-15
     67}}}
     68
     693. You can check which clusters you are running using the pg_lsclusters commandline tool that is also installed
     70
     71{{{
     72pg_lsclusters
     73}}}
     74
     754. To see what extensions are available
     76
     77{{{
     78
     79apt search postgresql-14 | grep postgis
     80}}}
     81
     82
     83
     84== Install PostGIS, pgRouting ==
     85The following will install PostGIS and pgRouting
     86(the -3 always refers to latest 3 version available. So at this time that would be 3.2.1)
     87
     88{{{
     89sudo apt install postgresql-14-postgis-3
     90
     91#to get the commandline tools shp2pgsql, raster2pgsql you need to do this
     92sudo apt install postgis
     93}}}
     94
     95== To get pgRouting ==
     96
     971. Check versions available
     98{{{ apt search postgresql-14 | grep pgrouting }}}
     99{{{
     100# Install pgRouting
     101sudo apt install postgresql-14-pgrouting
     102
     103}}}
     104
     1052. A companion package for pgrouting is the osm2pgrouting which allows you to load osm data in a routable format. To install do the following
     106
     107{{{
     108apt install osm2pgrouting
     109}}}
     110
     111== Enable PostGIS and pgRouting in database ==
     112
     113Never install PostGIS or pgrouting in the postgres database, create a user database
     114You can also enable the PostGIS extension here (or with the GUI as described below):
     115
     116{{{
     117sudo -u postgres psql
     118}}}
     119
     120{{{
     121CREATE DATABASE gisdb;
     122ALTER DATABASE gisdb SET search_path=public,postgis,contrib;
     123\connect gisdb;
     124
     125CREATE SCHEMA postgis;
     126
     127CREATE EXTENSION postgis SCHEMA postgis;
     128SELECT postgis_full_version();
     129}}}
     130
     131should give you output something like this:
     132
     133
     134{{{
     135                                                                                           postgis_full_version
     136 POSTGIS="3.2.1" [EXTENSION] PGSQL="140" GEOS="3.10.2-CAPI-1.16.0" PROJ="8.2.1" LIBXML="2.9.13" LIBJSON="0.15" LIBPROTOBUF="1.3.3" WAGYU="0.5.0 (Internal)"
     137(1 row)
     138
     139}}}
     140
     141=== Installing postgis_sfcgal, if you need advanced 3D support ===
     142
     143
     144{{{
     145CREATE EXTENSION postgis_sfcgal SCHEMA postgis;
     146SELECT postgis_full_version();
     147}}}
     148
     149should give you:
     150
     151
     152{{{
     153                                                                                                   postgis_full_version
     154
     155POSTGIS="3.2.1" [EXTENSION] PGSQL="140" GEOS="3.10.2-CAPI-1.16.0" SFCGAL="SFCGAL 1.4.1, CGAL 5.3.1, BOOST 1.74.0" PROJ="8.2.1" LIBXML="2.9.13" LIBJSON="0.15" LIBPROTOBUF="1.3.3" WAGYU="0.5.0 (Internal)"
     156
     157}}}
     158
     159
     160=== Install pgRouting ===
     161
     162{{{
     163CREATE  EXTENSION pgrouting SCHEMA postgis;
     164SELECT * FROM pgr_version();
     165}}}
     166
     167Depending on the version, newer versions only output a version number
     168as below.  Older versions output a whole table
     169
     170{{{
     171 pgr_version
     172-------------
     173 3.3.0
     174
     175}}}
     176
     177To get a full detail table on newer versions of pgrouting, use pgr_full_version();
     178
     179
     180Exit the psql console:
     181
     182{{{
     183\q
     184}}}
     185
     186=== Installing ogr_fdw ===
     187
     188ogr_fdw is a spatial foreign data wrapper that uses GDAL under the hood.
     189To install binaries:
     190
     191{{{
     192sudo apt install postgresql-14-ogr-fdw
     193
     194}}}
     195
     196Once binaries are installed, you can enable in your database and link to external files as follows
     197
     198{{{
     199
     200\connect gisdb;
     201CREATE EXTENSION ogr_fdw SCHEMA postgis;
     202CREATE SCHEMA IF NOT EXISTS staging ;
     203
     204-- see a list of data sources supported
     205SELECT *
     206FROM unnest(ogr_fdw_drivers());
     207
     208-- here I assume you have a file in root of /gis_data folder that postgres process has read rights to
     209-- any file that gdal can read will do e.g shape file, CSV, etc
     210CREATE SERVER svr_shp
     211  FOREIGN DATA WRAPPER ogr_fdw
     212  OPTIONS (
     213    datasource '/gis_data',
     214    format 'ESRI Shapefile' );
     215
     216
     217-- this will link in all your shapefile tables in folder gis_data as foreign tables
     218IMPORT FOREIGN SCHEMA ogr_all
     219FROM SERVER svr_shp INTO staging;
     220
     221}}}
     222
     223More examples of loading you can find at https://github.com/pramsey/pgsql-ogr-fdw