前言
PostgreSQL 是一種對象-關係型數據庫管理系統(ORDBMS),用來存儲並管理數據。而PostGIS是在PostgreSQL上增加了存儲管理空間數據的能力,使其具有地理信息系統特有的數據模式與存儲過程函數,相當於Oracle Spatial和SQLite Spatial。因此,在這裏我們需要先安裝PostgreSQL,再安裝PostGis插件
安裝PostgreSQL 9.4+PostGis
-
安裝所需的依賴庫 (如果安裝報需要相關依賴軟件包,執行:yum clean all)
sudo rpm -ivh http://mirrors.aliyun.com/epel/7/x86_64/e/epel-release-7-8.noarch.rpm -
安裝PostgreSQL 9.4
yum install http://yum.postgresql.org/9.4/redhat/rhel-6-x86_64/pgdg-redhat94-9.4-2.noarch.rpm
yum install postgresql94-server postgresql94-contrib -
安裝PostGis
yum install postgis2_94 postgis2_94-client
注:postgis2_95-client中包含了PostGis的命名行工具,如:shp2pgsql,pgsql2shp,raster2pgsql等 -
安裝ogrfdw(OGR Foreign Data Wrapper)
yum install ogr_fdw94更多ogr_fdw信息,請參見這裏
-
安裝pgRouting
yum install pgrouting_94 -
初始化數據庫
/usr/pgsql-9.4/bin/postgresql94-setup initdb -
啓動服務並設置爲開機啓動
systemctl enable postgresql-9.4
systemctl start postgresql-9.4 -
開放防火牆端口
vim /etc/sysconfig/iptables
增加:
-A INPUT -m state –state NEW -m tcp -p tcp –dport 5432 -j ACCEPT
-A INPUT -m state –state NEW -m tcp -p tcp –dport 80 -j ACCEPT -
重啓防火牆使配置生效
systemctl restart iptables.service
操作PostgreSQL
-
以PostgreSQL默認用戶登錄
su - postgres -base-4.2$
注:PostgreSQL安裝好之後會自動新建一個名爲postgres的Linux用戶(等同於role),同時還會新建一個名稱同樣爲postgres的數據庫
-
連接到PostgreSQL終端,默認使用postgres數據庫
-base-4.2$ psql postgres=#
注:postgres是數據庫名
-
在postgres用戶下新建一個數據庫
su - postgres -base-4.2$ psql -d spatial_testdb spatial_testdb=#
-
列出當前數據名和連接的信息
spatial_testdb=# \conninfo You are connected to database "spatial_testdb" as user "postgres" via socket in "/var/run/postgresql" at port "5432".
-
在testdb數據底下安裝PostGis擴展
# Enable PostGIS (includes raster) spatial_testdb=# CREATE EXTENSION postgis; # Enable Topology spatial_testdb=# CREATE EXTENSION postgis_topology; # enable ogrfdw spatial_testdb=# CREATE EXTENSION ogr_fdw;
其他的一些擴展:
# Enable PostGIS Advanced 3D and other geoprocessing algorithms sfcgal not available with all distributions CREATE EXTENSION postgis_sfcgal; # fuzzy matching needed for Tiger CREATE EXTENSION fuzzystrmatch; # rule based standardizer CREATE EXTENSION address_standardizer; # example rule data set CREATE EXTENSION address_standardizer_data_us; # Enable US Tiger Geocoder CREATE EXTENSION postgis_tiger_geocoder;
-
驗證PostGis是否安裝成功
SELECT postgis_full_version(); -
創建空間數據表
(1)先建立一個常規的表存儲城市信息(cities)spatial_testdb=# CREATE TABLE cities(id varchar(20),name varchar(50));
(2)然後添加一個空間列,用於存儲城市的位置。習慣上這個列叫做 “the_geom”。它記錄了數據的類型(點、線、面)、有幾維(這裏是二維)以及空間座標系統。這裏使用 EPSG:4326 座標系統:
spatial_testdb=# SELECT AddGeometryColumn ('cities', 'the_geom', 4326, 'POINT', 2);
-
爲空間表插入數據
spatial_testdb=# INSERT INTO cities(id, the_geom, name) VALUES (1,ST_GeomFromText('POINT(-0.1257 51.508)',4326),'London, England'); spatial_testdb=# INSERT INTO cities (id, the_geom, name) VALUES (2,ST_GeomFromText('POINT(-81.233 42.983)',4326),'London, Ontario'); spatial_testdb=# INSERT INTO cities (id, the_geom, name) VALUES (3,ST_GeomFromText('POINT(27.91162491 -33.01529)',4326),'East London,SA');
-
簡單查詢
標準的 SQL 操作都可以用於 PostGISspatial_testdb=# SELECT * FROM cities;
注:這裏的座標是無法閱讀的 16 進制格式。要以 WKT 文本顯示,使用 ST_AsText(the_geom) 或 ST_AsEwkt(the_geom) 函數。也可以使用 ST_X(the_geom) 和 ST_Y(the_geom) 顯示一個維度的座標:
spatial_testdb=# SELECT id, ST_AsText(the_geom), ST_AsEwkt(the_geom), ST_X(the_geom), ST_Y(the_geom) FROM cities;
-
空間查詢
這裏回答一個具體的問題:以米爲單位並假設地球是完美橢球,上面三個城市相互的距離是多少?spatial_testdb=# SELECT p1.name,p2.name,ST_Distance_Sphere(p1.the_geom,p2.the_geom) FROM cities AS p1, cities AS p2 WHERE p1.id > p2.id;
-
爲postgres用戶登錄設置密碼
vim /var/lib/pgsql/9.4/data/pg_hba.confhost all all 127.0.0.1/32 ident host all all ::1/128 ident
將默認無密碼的驗證方式改爲有密碼的驗證方式:
host all all 127.0.0.1/32 md5 host all all ::1/128 md5
su - postgres -base-4.2$ psql postgres=# \password postgres #爲postgres用戶設置密碼
-
重啓PostgresSQL,讓新的驗證方法生效
systemctl restart postgresql-9.4這樣的話重新用postgres用戶登錄時需要輸入密碼
其他一些命令
-
\q
退出,返回到“-base-4.2$”,如再需要退出”-base-4.2$”用exit -
\l
列出所有數據庫 -
\d
列出當前數據庫的所有表 -
\d tablename
列出某一張表的結構 -
\c databasename
從當前數據庫轉換到其他數據庫 -
\du
列出所有的用戶
本文轉載於: predict_wise原創博客(http://blog.csdn.net/predict_wise/article/details/53106558)