本文安裝環境是 CentOS7.6。安裝版本是 postgresql-11 。推薦關閉SELinux和防火牆打開相應端口。linux
安裝 postgresql-server。postgesql-server 官方安裝教程 。sql
#設置安裝源 [root@promote ~]# yum install https://download.postgresql.org/pub/repos/yum/11/redhat/rhel-7-ppc64le/pgdg-centos11-11-2.noarch.rpm #安裝postgresql11-server [root@promote ~]# yum install postgresql11-server postgresql11-client postgresql11-libs postgresql11-contrib postgresql11-devel #初始化數據庫 [root@promote ~]# /usr/pgsql-11/bin/postgresql-11-setup initdb #添加開機啓動 [root@promote ~]# systemctl enable postgresql-11
安裝 postgis 。參考postgis 官方安裝教程Red Hat / Centos / Scientific Linux 選項。數據庫
#安裝epel-release [root@promote ~]# yum -y install epel-release #安裝postgis [root@promote ~]# sudo yum install postgis25_11 postgis25_11-devel postgis25_11-client postgis25_11-debuginfo postgis25_11-utils #安裝ogr_fdw11擴展 [root@promote ~]# yum install ogr_fdw11 ogr_fdw11-debuginfo pgrouting_11 pgrouting_11-debuginfo #可選添加開機啓動 [root@promote ~]# systemctl enable postgresql-11 #啓動和查看postgresql-11服務 [root@promote ~]# systemctl start postgresql-11 [root@promote ~]# systemctl status postgresql-11 [root@promote ~]# systemctl list-unit-files | grep postgresql postgresql-11.service enabled #查看開機啓動 [root@promote ~]# systemctl list-unit-files | grep postgresql postgresql-11.service enabled [root@promote ~]# #查看數據庫服務端口開啓狀況 [root@promote ~]# netstat -ntlp | grep post tcp 0 0 127.0.0.1:5432 0.0.0.0:* LISTEN 10592/postmaster tcp6 0 0 ::1:5432 :::* LISTEN 10592/postmaster [root@promote ~]#
建立用戶,建立數據庫gistest,切換用戶到postgres,鏈接和切換數據庫,查看postgis版本信息。centos
[root@promote ~]# su - postgres #非第一次登陸 [root@promote ~]# su - postgres 上一次登陸:五 4月 12 16:51:07 CST 2019pts/1 上 -bash-4.2$ psql psql (11.2) 輸入 "help" 來獲取幫助信息. postgres=# #安裝EXTENSION postgres=# CREATE EXTENSION postgis; CREATE EXTENSION postgres=# CREATE EXTENSION postgis_topology; CREATE EXTENSION postgres=# CREATE EXTENSION adminpack; CREATE EXTENSION postgres=# CREATE EXTENSION postgis_sfcgal; CREATE EXTENSION postgres=# CREATE EXTENSION fuzzystrmatch; CREATE EXTENSION postgres=# CREATE EXTENSION address_standardizer; CREATE EXTENSION postgres=# CREATE EXTENSION address_standardizer_data_us; CREATE EXTENSION postgres=# CREATE EXTENSION postgis_tiger_geocoder; CREATE EXTENSION postgres=# postgres=# \dx 已安裝擴展列表 名稱 | 版本 | 架構模式 | 描述 ------------------------------+-------+------------+--------------------------------------------------------------------------------------------------------------------- address_standardizer | 2.5.2 | public | Used to parse an address into constituent elements. Generally used to support geocoding address normalization step. address_standardizer_data_us | 2.5.2 | public | Address Standardizer US dataset example adminpack | 2.0 | pg_catalog | administrative functions for PostgreSQL fuzzystrmatch | 1.1 | public | determine similarities and distance between strings plpgsql | 1.0 | pg_catalog | PL/pgSQL procedural language postgis | 2.5.2 | public | PostGIS geometry, geography, and raster spatial types and functions postgis_sfcgal | 2.5.2 | public | PostGIS SFCGAL functions postgis_tiger_geocoder | 2.5.2 | tiger | PostGIS tiger geocoder and reverse geocoder postgis_topology | 2.5.2 | topology | PostGIS topology spatial types and functions (9 行記錄) postgres=# \d 關聯列表 架構模式 | 名稱 | 類型 | 擁有者 ----------+--------------------------+--------+---------- public | city | 數據表 | postgres public | geography_columns | 視圖 | postgres public | geometry_columns | 視圖 | postgres public | raster_columns | 視圖 | postgres public | raster_overviews | 視圖 | postgres public | spatial_ref_sys | 數據表 | postgres public | us_gaz | 數據表 | postgres public | us_gaz_id_seq | 序列數 | postgres public | us_lex | 數據表 | postgres public | us_lex_id_seq | 序列數 | postgres public | us_rules | 數據表 | postgres public | us_rules_id_seq | 序列數 | postgres tiger | addr | 數據表 | postgres tiger | addr_gid_seq | 序列數 | postgres tiger | addrfeat | 數據表 | postgres tiger | addrfeat_gid_seq | 序列數 | postgres tiger | bg | 數據表 | postgres tiger | bg_gid_seq | 序列數 | postgres tiger | county | 數據表 | postgres tiger | county_gid_seq | 序列數 | postgres tiger | county_lookup | 數據表 | postgres tiger | countysub_lookup | 數據表 | postgres tiger | cousub | 數據表 | postgres tiger | cousub_gid_seq | 序列數 | postgres tiger | direction_lookup | 數據表 | postgres tiger | edges | 數據表 | postgres tiger | edges_gid_seq | 序列數 | postgres tiger | faces | 數據表 | postgres tiger | faces_gid_seq | 序列數 | postgres tiger | featnames | 數據表 | postgres tiger | featnames_gid_seq | 序列數 | postgres tiger | geocode_settings | 數據表 | postgres tiger | geocode_settings_default | 數據表 | postgres tiger | loader_lookuptables | 數據表 | postgres tiger | loader_platform | 數據表 | postgres tiger | loader_variables | 數據表 | postgres tiger | pagc_gaz | 數據表 | postgres tiger | pagc_gaz_id_seq | 序列數 | postgres tiger | pagc_lex | 數據表 | postgres tiger | pagc_lex_id_seq | 序列數 | postgres tiger | pagc_rules | 數據表 | postgres tiger | pagc_rules_id_seq | 序列數 | postgres tiger | place | 數據表 | postgres tiger | place_gid_seq | 序列數 | postgres tiger | place_lookup | 數據表 | postgres tiger | secondary_unit_lookup | 數據表 | postgres tiger | state | 數據表 | postgres tiger | state_gid_seq | 序列數 | postgres tiger | state_lookup | 數據表 | postgres tiger | street_type_lookup | 數據表 | postgres tiger | tabblock | 數據表 | postgres tiger | tabblock_gid_seq | 序列數 | postgres tiger | tract | 數據表 | postgres tiger | tract_gid_seq | 序列數 | postgres tiger | zcta5 | 數據表 | postgres tiger | zcta5_gid_seq | 序列數 | postgres tiger | zip_lookup | 數據表 | postgres tiger | zip_lookup_all | 數據表 | postgres tiger | zip_lookup_base | 數據表 | postgres tiger | zip_state | 數據表 | postgres tiger | zip_state_loc | 數據表 | postgres topology | layer | 數據表 | postgres topology | topology | 數據表 | postgres topology | topology_id_seq | 序列數 | postgres (64 行記錄) postgres=# postgres=# create database gistest; postgres=# \c gistest #\x 開啓或關閉擴展顯示 gistest=# \x 擴展顯示已關閉。 gistest=# \d 關聯列表 架構模式 | 名稱 | 類型 | 擁有者 ----------+-------------------+--------+---------- public | geography_columns | 視圖 | postgres public | geometry_columns | 視圖 | postgres public | raster_columns | 視圖 | postgres public | raster_overviews | 視圖 | postgres public | spatial_ref_sys | 數據表 | postgres topology | layer | 數據表 | postgres topology | topology | 數據表 | postgres topology | topology_id_seq | 序列數 | postgres (8 行記錄) gistest=# \l 數據庫列表 名稱 | 擁有者 | 字元編碼 | 校對規則 | Ctype | 存取權限 -----------+----------+----------+-------------+-------------+----------------------- gistest | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | 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 (4 行記錄) #查看版本信息 postgres=# gistest=# \q -bash-4.2$ psql psql (11.2) 輸入 "help" 來獲取幫助信息. postgres=# select postgis_full_version(); postgis_full_version -------------------------------------------------------------------------------------------------------------------------------------------------------- ---------------------------------------------------------------- POSTGIS="2.5.2 r17328" [EXTENSION] PGSQL="110" GEOS="3.7.1-CAPI-1.11.1 27a5e771" SFCGAL="1.2.2" PROJ="Rel. 4.9.3, 15 August 2016" GDAL="GDAL 1.11.4, released 2016/01/25" LIBXML="2.9.1" LIBJSON="0.11" TOPOLOGY RASTER (1 行記錄) postgres=# exit -bash-4.2$ exit 登出 [root@promote ~]#
鏈接postgres數據庫,建立測試表city。添加測算表和值。bash
postgres=# \c postgres postgres=# \x 擴展顯示已打開。 #建立測試表 postgres=# CREATE TABLE city (id int4, name varchar(50)); CREATE TABLE postgres=# SELECT AddGeometryColumn ('city', 'the_geom', 4326, 'POINT', 2); -[ RECORD 1 ]-----+-------------------------------------------------- addgeometrycolumn | public.city.the_geom SRID:4326 TYPE:POINT DIMS:2 postgres=# select * from city; (0 行記錄) postgres=# INSERT INTO city (id, the_geom, name) VALUES (1,ST_GeomFromText('POINT(-0.1257 51.508)',4326),'London, England'); INSERT 0 1 postgres=# INSERT INTO city (id, the_geom, name) VALUES (2,ST_GeomFromText('POINT(-81.233 42.983)',4326),'London, Ontario'); INSERT 0 1 postgres=# INSERT INTO city (id, the_geom, name) VALUES (3,ST_GeomFromText('POINT(27.91162491 -33.01529)',4326),'East London,SA'); INSERT 0 1 postgres=#
使用簡單查詢和空間查詢。架構
postgres=# SELECT * FROM city; -[ RECORD 1 ]------------------------------------------------ id | 1 name | London, England the_geom | 0101000020E6100000BBB88D06F016C0BF1B2FDD2406C14940 -[ RECORD 2 ]------------------------------------------------ id | 2 name | London, Ontario the_geom | 0101000020E6100000F4FDD478E94E54C0E7FBA9F1D27D4540 -[ RECORD 3 ]------------------------------------------------ id | 3 name | East London,SA the_geom | 0101000020E610000040AB064060E93B4059FAD005F58140C0 postgres=# SELECT id, ST_AsText(the_geom), ST_AsEwkt(the_geom), ST_X(the_geom), ST_Y(the_geom) FROM city; -[ RECORD 1 ]------------------------------------- id | 1 st_astext | POINT(-0.1257 51.508) st_asewkt | SRID=4326;POINT(-0.1257 51.508) st_x | -0.1257 st_y | 51.508 -[ RECORD 2 ]------------------------------------- id | 2 st_astext | POINT(-81.233 42.983) st_asewkt | SRID=4326;POINT(-81.233 42.983) st_x | -81.233 st_y | 42.983 -[ RECORD 3 ]------------------------------------- id | 3 st_astext | POINT(27.91162491 -33.01529) st_asewkt | SRID=4326;POINT(27.91162491 -33.01529) st_x | 27.91162491 st_y | -33.01529 postgres=# SELECT p1.name,p2.name,ST_Distance_Sphere(p1.the_geom,p2.the_geom) FROM city AS p1, city AS p2 WHERE p1.id > p2.id; 警告: ST_Distance_Sphere signature was deprecated in 2.2.0. Please use ST_DistanceSphere -[ RECORD 1 ]------+----------------- name | London, Ontario name | London, England st_distance_sphere | 5875787.03777356 -[ RECORD 2 ]------+----------------- name | East London,SA name | London, England st_distance_sphere | 9789680.59961472 -[ RECORD 3 ]------+----------------- name | East London,SA name | London, Ontario st_distance_sphere | 13892208.6782928 postgres=# postgres=# SELECT p1.name,p2.name,ST_DistanceSphere(p1.the_geom,p2.the_geom) FROM city AS p1, city AS p2 WHERE p1.id > p2.id; 警告: ST_Distance_Sphere signature was deprecated in 2.2.0. Please use ST_DistanceSphere -[ RECORD 1 ]------+----------------- name | London, Ontario name | London, England st_distance_sphere | 5875787.03777356 -[ RECORD 2 ]------+----------------- name | East London,SA name | London, England st_distance_sphere | 9789680.59961472 -[ RECORD 3 ]------+----------------- name | East London,SA name | London, Ontario st_distance_sphere | 13892208.6782928 postgres=# SELECT p1.name,p2.name,ST_DistanceSphere(p1.the_geom,p2.the_geom) FROM city AS p1, city AS p2 WHERE p1.id > p2.id; -[ RECORD 1 ]-----+----------------- name | London, Ontario name | London, England st_distancesphere | 5875787.03777356 -[ RECORD 2 ]-----+----------------- name | East London,SA name | London, England st_distancesphere | 9789680.59961472 -[ RECORD 3 ]-----+----------------- name | East London,SA name | London, Ontario st_distancesphere | 13892208.6782928 postgres=#