| 1 | The [http://wiki.postgresql.org/wiki/Apt PostgreSQL Apt Repository] now hosts installs of PostGIS, pgRouting, pgpointcloud in addition to PostgreSQL. |
| 2 | |
| 3 | The 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 | |
| 5 | Run these in terminal: |
| 6 | |
| 7 | == Verify what version of Ubuntu you are running == |
| 8 | {{{ |
| 9 | sudo lsb_release -a |
| 10 | }}} |
| 11 | |
| 12 | |
| 13 | == Add Respository to sources.list == |
| 14 | |
| 15 | You'll need to replace the nickname below with what you are running. |
| 16 | |
| 17 | |
| 18 | {{{ |
| 19 | # prereqs needed for accept certs |
| 20 | apt install ca-certificates gnupg |
| 21 | }}} |
| 22 | |
| 23 | |
| 24 | === Adds pgdg keys and main repo |
| 25 | {{{ |
| 26 | curl https://www.postgresql.org/media/keys/ACCC4CF8.asc | gpg --dearmor | sudo tee /etc/apt/trusted.gpg.d/apt.postgresql.org.gpg >/dev/null |
| 27 | sudo 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 | {{{ |
| 35 | sudo 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 |
| 39 | PostgreSQL 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 |
| 43 | cat << EOF >> /etc/apt/preferences.d/pgdg.pref |
| 44 | Package: * |
| 45 | Pin: release o=apt.postgresql.org |
| 46 | Pin-Priority: 500 |
| 47 | EOF |
| 48 | }}} |
| 49 | |
| 50 | |
| 51 | |
| 52 | === Installing |
| 53 | 1. Then update your catalog and installed software |
| 54 | {{{ |
| 55 | sudo apt update |
| 56 | sudo apt upgrade |
| 57 | }}} |
| 58 | |
| 59 | 2. Install PostgreSQL replacing 14 with the version you want |
| 60 | {{{ |
| 61 | sudo apt install postgresql-14 |
| 62 | }}} |
| 63 | |
| 64 | Apt system allows for installing multiple versions of PostgreSQL, so you could also install an additional, such as |
| 65 | {{{ |
| 66 | sudo apt install postgresql-15 |
| 67 | }}} |
| 68 | |
| 69 | 3. You can check which clusters you are running using the pg_lsclusters commandline tool that is also installed |
| 70 | |
| 71 | {{{ |
| 72 | pg_lsclusters |
| 73 | }}} |
| 74 | |
| 75 | 4. To see what extensions are available |
| 76 | |
| 77 | {{{ |
| 78 | |
| 79 | apt search postgresql-14 | grep postgis |
| 80 | }}} |
| 81 | |
| 82 | |
| 83 | |
| 84 | == Install PostGIS, pgRouting == |
| 85 | The 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 | {{{ |
| 89 | sudo apt install postgresql-14-postgis-3 |
| 90 | |
| 91 | #to get the commandline tools shp2pgsql, raster2pgsql you need to do this |
| 92 | sudo apt install postgis |
| 93 | }}} |
| 94 | |
| 95 | == To get pgRouting == |
| 96 | |
| 97 | 1. Check versions available |
| 98 | {{{ apt search postgresql-14 | grep pgrouting }}} |
| 99 | {{{ |
| 100 | # Install pgRouting |
| 101 | sudo apt install postgresql-14-pgrouting |
| 102 | |
| 103 | }}} |
| 104 | |
| 105 | 2. 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 | {{{ |
| 108 | apt install osm2pgrouting |
| 109 | }}} |
| 110 | |
| 111 | == Enable PostGIS and pgRouting in database == |
| 112 | |
| 113 | Never install PostGIS or pgrouting in the postgres database, create a user database |
| 114 | You can also enable the PostGIS extension here (or with the GUI as described below): |
| 115 | |
| 116 | {{{ |
| 117 | sudo -u postgres psql |
| 118 | }}} |
| 119 | |
| 120 | {{{ |
| 121 | CREATE DATABASE gisdb; |
| 122 | ALTER DATABASE gisdb SET search_path=public,postgis,contrib; |
| 123 | \connect gisdb; |
| 124 | |
| 125 | CREATE SCHEMA postgis; |
| 126 | |
| 127 | CREATE EXTENSION postgis SCHEMA postgis; |
| 128 | SELECT postgis_full_version(); |
| 129 | }}} |
| 130 | |
| 131 | should 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 | {{{ |
| 145 | CREATE EXTENSION postgis_sfcgal SCHEMA postgis; |
| 146 | SELECT postgis_full_version(); |
| 147 | }}} |
| 148 | |
| 149 | should give you: |
| 150 | |
| 151 | |
| 152 | {{{ |
| 153 | postgis_full_version |
| 154 | |
| 155 | POSTGIS="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 | {{{ |
| 163 | CREATE EXTENSION pgrouting SCHEMA postgis; |
| 164 | SELECT * FROM pgr_version(); |
| 165 | }}} |
| 166 | |
| 167 | Depending on the version, newer versions only output a version number |
| 168 | as below. Older versions output a whole table |
| 169 | |
| 170 | {{{ |
| 171 | pgr_version |
| 172 | ------------- |
| 173 | 3.3.0 |
| 174 | |
| 175 | }}} |
| 176 | |
| 177 | To get a full detail table on newer versions of pgrouting, use pgr_full_version(); |
| 178 | |
| 179 | |
| 180 | Exit the psql console: |
| 181 | |
| 182 | {{{ |
| 183 | \q |
| 184 | }}} |
| 185 | |
| 186 | === Installing ogr_fdw === |
| 187 | |
| 188 | ogr_fdw is a spatial foreign data wrapper that uses GDAL under the hood. |
| 189 | To install binaries: |
| 190 | |
| 191 | {{{ |
| 192 | sudo apt install postgresql-14-ogr-fdw |
| 193 | |
| 194 | }}} |
| 195 | |
| 196 | Once binaries are installed, you can enable in your database and link to external files as follows |
| 197 | |
| 198 | {{{ |
| 199 | |
| 200 | \connect gisdb; |
| 201 | CREATE EXTENSION ogr_fdw SCHEMA postgis; |
| 202 | CREATE SCHEMA IF NOT EXISTS staging ; |
| 203 | |
| 204 | -- see a list of data sources supported |
| 205 | SELECT * |
| 206 | FROM 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 |
| 210 | CREATE 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 |
| 218 | IMPORT FOREIGN SCHEMA ogr_all |
| 219 | FROM SERVER svr_shp INTO staging; |
| 220 | |
| 221 | }}} |
| 222 | |
| 223 | More examples of loading you can find at https://github.com/pramsey/pgsql-ogr-fdw |