1、安裝sql
sudo apt install postgis postgresql-11-postgis-3
這裏須要根據服務器版本選擇對應的包,如服務器是最新的12則:數據庫
sudo apt install postgis postgresql-12-postgis-3
2、建立數據庫mygis使之具有Postgis功能服務器
使你的OS登錄用戶(這裏mymotif),成爲postgers數據庫用戶並具有建庫權限:數據結構
sudo -u postgres psql -c "CREATE USER mymotif WITH PASSWORD 'wxwpxh' CREATEDB ;"
創建mygis數據庫:函數
mymotif@DESKTOP-PRH2A6F:~$ createdb mygis
安裝postgis以及其它的一些擴展:post
mymotif@DESKTOP-PRH2A6F:~$ sudo -i -u postgres psql mygis psql (12.0 (Ubuntu 12.0-2.pgdg18.04+1), 服務器 11.5 (Ubuntu 11.5-3.pgdg18.04+1)) 輸入 "help" 來獲取幫助信息. mygis=# CREATE EXTENSION postgis; CREATE EXTENSION mygis=# CREATE EXTENSION postgis_topology; CREATE EXTENSION mygis=# CREATE EXTENSION postgis_sfcgal; CREATE EXTENSION mygis=# CREATE EXTENSION fuzzystrmatch; CREATE EXTENSION mygis=# CREATE EXTENSION address_standardizer; CREATE EXTENSION mygis=# CREATE EXTENSION address_standardizer_data_us; CREATE EXTENSION mygis=# CREATE EXTENSION postgis_tiger_geocoder; CREATE EXTENSION mygis=#
受權測試
mygis=# GRANT ALL PRIVILEGES ON DATABASE mygis TO mymotif; GRANT mygis=# GRANT ALL ON schema topology TO mymotif; GRANT mygis=# \q
以mymotif用戶鏈接mygis,驗證:postgresql
mymotif@DESKTOP-PRH2A6F:~$ psql mygis psql (12.0 (Ubuntu 12.0-2.pgdg18.04+1), 服務器 11.5 (Ubuntu 11.5-3.pgdg18.04+1)) 輸入 "help" 來獲取幫助信息. rm(st_geometryfromtext('POINT(116.4680989087 39.9486998528)',4326),3857));7961543 39.9486461337)',4326),3857),st_transfor st_distance ------------------ 34.5933990100333 (1 行記錄) 337,116.4680989087 39.9486998528,116.4682182670 39.9483181633,116.4679312706 39.9482801227))',4326),4527));43 39.94864613 st_area ------------------ 1101.47601544425 mygis=> select area(circle'((0,0),1)'); area ------------------ 3.14159265358979 mygis=> SELECT st_distance(st_transform(st_geometryfromtext('POINT(116.4677961543 39.9486461337)',4326),4527),st_transform(st_geometryfromtext('POINT(116.4680989087 39.9486998528)',4326),4527)); st_distance ------------------ 26.5520226666734 mygis=> SELECT ST_SetSRID(ST_Point(-87.71,43.741),4326),ST_GeomFromText('POINT(-87.71 43.741)',4326); st_setsrid | st_geomfromtext ----------------------------------------------------+---------------------------------------------------- 0101000020E61000003D0AD7A370ED55C0022B8716D9DE4540 | 0101000020E61000003D0AD7A370ED55C0022B8716D9DE4540 (1 行記錄) mygis=> SELECT PostGIS_Full_Version();
出現:code
postgis_full_version ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- POSTGIS="3.0.0 r17983" [EXTENSION] PGSQL="110" GEOS="3.7.1-CAPI-1.11.1 27a5e771" SFCGAL="1.3.5" PROJ="Rel. 4.9.3, 15 August 2016" LIBXML="2.9.4" LIBJSON="0.12.1" LIBPROTOBUF="1.2.1" WAGYU="0.4.3 (Internal)" TOPOLOGY (1 行記錄) (END)
這樣mygis已經具有PostGis功能,便可以使用PostGis的數據結構、函數、過程等等orm
3、測試(也是以mymotif用戶鏈接mygis)
一、創建表格(用到了PostGis中point類型)。
mygis=>CREATE TABLE location_city(name varchar(80), location point);
二、插入
mygis=> INSERT INTO location_city VALUES ('San Francisco', '(-194.0, 53.0)'), ('New York', '(-184.0, 43.0)'), ('北京', '(-94.0, 133.0)'), ('Los Angeles', '(-297.0, 63.0)'), ('Chicago', '(-94.0, 283.0)');
三、select
mygis=> SELECT * FROM location_city; name | location ---------------+----------- San Francisco | (-194,53) New York | (-184,43) 北京 | (-94,133) Los Angeles | (-297,63) Chicago | (-94,283) (5 行記錄)