1. 程式人生 > >GIS on CentOS 7 之 PostgreSQL & PostGIS

GIS on CentOS 7 之 PostgreSQL & PostGIS

contains LG -h HP pre word html psql linu

PostgreSQL & PostGIS

安裝postgresql

配置好yum源之後,使用yum info postgresql可發現 postgresql的版本為9.2.23,若想安裝最新版本,可參考下面操作

https://www.postgresql.org/download/linux/redhat/

http://docs.nextgis.com/docs_ngweb/source/install-centos7.html

http://www.postgresonline.com/journal/archives/362-An-almost-idiots-guide-to-install-PostgreSQL-9.5,-PostGIS-2.2-and-pgRouting-2.1.0-with-Yum.html

sudo yum install https://download.postgresql.org/pub/repos/yum/9.5/redhat/rhel-7-x86_64/pgdg-centos95-9.5-3.noarch.rpm

# 查看上述倉庫中可用的包
yum list | grep postgresql95
# 安裝 PostgreSQL  client server 
sudo yum install postgresql95 postgresql95-server postgresql95-libs postgresql95-contrib postgresql95-devel
# 查看幫助
psql --help
rpm -qa | grep postgresql*  # 查看已安裝軟件
# 初始化數據庫,並設置隨系統啟動
sudo /usr/pgsql-9.5/bin/postgresql95-setup initdb
sudo systemctl start postgresql-9.5.service # service postgresql-9.5 start
sudo systemctl enable postgresql-9.5.service

# 編輯驗證參數(此處使用nano編輯器,可以使用 vim)
# psql 進入postgres數據庫之後,使用 show hba_file; 查看文件位置
sudo nano /var/lib/pgsql/9.5/data/pg_hba.conf 
sudo vim /var/lib/pgsql/9.5/data/pg_hba.conf 

將ident 修改為 md5

PostgreSQL ident和peer基於操作系統用戶的認證 PostgreSQL認證方法

如果是 peer,可能會出現 對等認證失敗 的錯誤

# "local" is for Unix domain socket connections only
local   all             all                                     md5
# IPv4 local connections:
host    all             all             127.0.0.1/32            md5
# IPv6 local connections:
host    all             all             ::1/128                 md5
# 可進一步修改 postgresql.conf 以監聽目標地址
su postgres   # 切換到postgres    或   sudo su postgres
psql -U postgres   #登錄數據庫,默認沒有密碼
ALTER USER postgres WITH PASSWORD ‘密碼‘; #修改密碼  sudo passwd postgres
# 或者使用 \password 命令
\password postgres 
select * from pg_shadow; # 查看數據庫信息
\q #退出
psql -V
sudo systemctl restart postgresql-9.5.service # 重啟服務,或  service postgresql-9.5 restart

# 使用postgres用戶新建一個psql數據庫用戶(ngw_admin)
# \du 命令可以查看用戶信息
# -P 表示密碼,-e表示顯示命令。 使用 -s 或 --superuser 賦予超級用戶權限
sudo -u postgres createuser ngw_admin -P -e
# 新建數據庫(db_ngw),所有者為 ngw_admin
sudo -u postgres createdb -O ngw_admin --encoding=UTF8 db_ngw
postgresql-client libraries and client binaries
postgresql-server core database server
postgresql-contrib additional supplied modules
postgresql-devel libraries and headers for C language development
pgadmin4 pgAdmin 4 graphical administration utility

### 安裝postgis

https://postgis.net/install/

The postgis2_95-client contains the PostGIS commandline tools shp2gpsql, pgsql2shp, raster2pgsql that are useful for loading or exporting spatial data.

sudo yum install epel-release # 沒有配置epel源的話
sudo yum install postgis2_95 postgis2_95-client # 安裝
# 需要 pgRouting 時 yum install pgrouting_95
# 登錄進入相應數據庫
psql -U ngw_admin -d db_ngw
# 為已有數據庫(db_ngw)擴展 postgis 功能
CREATE EXTENSION postgis;
SELECT PostGIS_Full_Version(); # 測試數據庫是否包含了postgis的功能

或者直接在shell中執行

sudo psql -u postgres -d db_ngw -c ‘CREATE EXTENSION postgis;‘
sudo psql -u postgres -d db_ngw -c ‘ALTER TABLE geometry_columns OWNER TO ngw_admin;‘
sudo psql -u postgres -d db_ngw -c ‘ALTER TABLE spatial_ref_sys OWNER TO ngw_admin;‘
sudo psql -u postgres -d db_ngw -c ‘ALTER TABLE geography_columns OWNER TO ngw_admin;‘
psql -h localhost -d db_ngw -U ngw_admin -c "SELECT PostGIS_Full_Version();"

使用template方式直接創建postgis數據庫,並指定所有者

# 登錄數據庫
# postgis_21_sample 為 2.1 版本postgis數據庫
create  database  geodataont   template   postgis_21_sample   owner   gdo;
# 或者
createdb -O gdo -U postgres -T postgis_22_sample geodataont

啟用 PostGIS 功能的相關SQL

DO NOT INSTALL it in the database called postgres.

# 連接數據庫之後,執行以下sql命令啟用相應功能
# Enable PostGIS (includes raster)
CREATE EXTENSION postgis;
# Enable Topology
CREATE EXTENSION postgis_topology;
# 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;

CREATE EXTENSION pgrouting;
SELECT * FROM pgr_version();
# yum install ogr_fdw95
CREATE EXTENSION ogr_fdw;

CentOS 7 源碼安裝PostGIS

GIS on CentOS 7 之 PostgreSQL & PostGIS