CentOS 7 yum 安裝 postgis 和使用簡單查詢
阿新 • • 發佈:2019-04-13
本文安裝環境是 CentOS7.6。安裝版本是 postgresql-11 。推薦關閉SELinux和防火牆開啟相應埠。
安裝 postgresql-server。postgesql-server 官方安裝教程 。
#設定安裝源 [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版本資訊。
[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。新增測算表和值。
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