本文介紹在RedHat7環境下安裝使用PostGIS的流程。html
這個比較簡單,直接使用yum安裝便可。sql
$ sudo yum install -y postgresql-server postgresql-devel libxml2 libxml2-devel
順便安裝postgresql-devel、libxml2-devel,後邊編譯安裝PostGIS會用到。數據庫
postgresql.x86_64 9.2.13-1.1 postgresql-devel.x86_64 9.2.13-1.1 postgresql-libs.x86_64 9.2.13-1.1 postgresql-server.x86_64 9.2.13-1.1 libxml2 2.9.1-6 libxml2-devel.x86_64 2.9.1-6
而後切換到postgres帳戶。bash
$ sudo su postgres postgres $
確認PostgreSQL數據目錄。架構
postgres $ cat /var/lib/pgsql/.bash_profile [ -f /etc/profile ] && source /etc/profile PGDATA=/var/lib/pgsql/data export PGDATA
執行初始化操做。函數
postgres $ initdb
目錄/var/lib/pgsql/data下存儲了PostgreSQL的全部數據文件和配置。post
使用pg_ctl啓動PostgreSQL。測試
postgres $ pg_ctl start
使用psql客戶端鏈接。優化
postgres $ psql psql (9.2.13) 輸入 "help" 來獲取幫助信息. postgres=# \l 資料庫列表 名稱 | 擁有者 | 字元編碼 | 校對規則 | Ctype | 存取權限 ------------------+----------+----------+-------------+-------------+----------------------- postgres | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | template0 | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres template1 | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres
準備gdal、proj、geos和postgis的源碼包,postgis版本注意和postgresql保持兼容。阿里雲
兼容信息能夠查看: http://trac.osgeo.org/postgis/wiki/UsersWikiPostgreSQLPostGIS
$ wget http://download.osgeo.org/gdal/2.2.3/gdal-2.2.3.tar.gz $ wget http://download.osgeo.org/proj/proj-4.8.0.tar.gz $ wget http://download.osgeo.org/geos/geos-3.3.3.tar.bz2 $ wget http://download.osgeo.org/postgis/source/postgis-2.2.6.tar.gz
依次解壓、編譯、安裝以上軟件包。
$ tar xf gdal-2.2.3.tar.gz && cd gdal-2.2.3 && ./configure --prefix=/usr/local/gdal && make && sudo make install $ tar xf proj-4.8.0.tar.gz && cd proj-4.8.0 && ./configure --prefix=/usr/local/proj && make && sudo make install $ tar xf geos-3.3.3.tar.bz2 && cd geos-3.3.3 && ./configure --prefix=/usr/local/geos && make && sudo make install $ tar xf postgis-2.2.6.tar.gz && cd postgis-2.2.6 && ./configure -prefix=/usr/local/postgis --with-geosconfig=/usr/local/geos/bin/geos-config --with-projdir=/usr/local/proj --with-gdalconfig=/usr/local/gdal/bin/gdal-config && make && sudo make install
將gdal、proj、geos的lib目錄添加到ldconfig。
$ sudo cat /etc/ld.so.conf include ld.so.conf.d/*.conf /usr/local/gdal/lib/ /usr/local/proj/lib/ /usr/local/geos/lib/ $ sudo ldconfig
# 建立無空間特性數據庫 postgres $ createdb template_postgis # 建立相關空間數據庫相關的函數,類型,操做符等 postgres $ psql -f /usr/share/pgsql/contrib/postgis-2.2/postgis.sql -d template_postgis postgres $ psql -f /usr/share/pgsql/contrib/postgis-2.2/rtpostgis.sql -d template_postgis # 驗證空間數據庫版本 postgres $ psql template_postgis psql (9.2.13) 輸入 "help" 來獲取幫助信息. template_postgis=# select postgis_full_version(); postgis_full_version --------------------------------------------------------------------------------------------------------------------------------------------- POSTGIS="2.2.6 r16006" GEOS="3.3.3-CAPI-1.7.4" PROJ="Rel. 4.8.0, 6 March 2012" GDAL="GDAL 2.2.3, released 2017/11/20" LIBXML="2.9.1" RASTER (1 行記錄) template_postgis=# \d 關聯列表 架構模式 | 名稱 | 型別 | 擁有者 ----------+-------------------+--------+---------- public | geography_columns | 視觀表 | postgres public | geometry_columns | 視觀表 | postgres public | raster_columns | 視觀表 | postgres public | raster_overviews | 視觀表 | postgres public | spatial_ref_sys | 資料表 | postgres (5 行記錄)
postgres $ createdb -T template_postgis new_database
測試點(0, 0)是否在指定的多邊形內。
new_database=# select ST_Within(ST_GeomFromText('POINT(0 0)', 4326), ST_GeomFromText('POLYGON((1 1, 1 -1, -1 -1, -1 1, 1 1))', 4326)) ; st_within ----------- t (1 行記錄)
詳細語法規則能夠參考PostGis使用手冊:http://www.postgres.cn/docs/PostGis-2.2.0dev_Manual.pdf