CentOS7.5安裝Postgresql10.5和PostGIS

參考順序:

  1. CentOS7.5安裝Postgresql10.5和PostGISCentOS7.6安裝Postgresq11和PostGIS
  2. CentOS7部署GeoServer
  3. CentOS7部署osm2pgsql
  4. GeoServer發佈OSM地圖

CentOS7 安裝Postgres10.5和PostGIS

每次安裝Postgresql總會有莫名其妙的問題,最好安裝前先裝好阿里和網易的yum源。

  • 系統:Centos7.5

一、安裝Postgres10.5

1.首先安裝PostgreSQL的rpm

yum install https://download.postgresql.org/pub/repos/yum/10/redhat/rhel-7-x86_64/pgdg-centos10-10-2.noarch.rpm -y  

 

查看postgresql源

yum list | grep postgresql

安裝postgresql10-contrib和postgresql10-server

yum install postgresql10-contrib postgresql10-server -y

這樣會給我們的系統增加一個postgres用戶

2.修改默認數據目錄 
Postgresql默認的數據目錄是/var/lib/pgsql/版本號/data目錄,這要求你在/var下有足夠的存儲空間,我們這裏將其換掉,假設/home的空間很大。

首先在/home下創建一個Postgresql的數據目錄,指定所有者postgres同時分配權限

mkdir /home/postgresql_data
chown postgres:postgres /home/postgresql_data
chmod 750 /home/postgresql_data

設置環境變量

export PATH=/usr/pgsql-10/bin:$PATH
export LD_LIBRARY_PATH=/usr/pgsql-10/lib
export PGDATA=/home/postgresql_data

切換到postgres用戶,使用initdb初始化數據庫,這樣在/home/postgresql_data下會增加很多東西, 

修改/usr/lib/systemd/system/postgresql-10.service文件的內容,在#Location of database direcotry裏面指定正確的PGDATA:

#Location of database directory
Environment=PGDATA=/home/postgresql_data

3.在root用戶下配置數據庫服務開機啓動並立即啓動數據庫服務

systemctl enable postgresql-10.service
service postgresql-10 start
service postgresql-10 status

檢查數據庫狀態,有綠色,沒紅色說明啓動完成 

這個過程中出現過一個錯誤,啓動失敗:

我這邊將/home/postgresql_data下的postmaster.pid刪除再重啓服務就好了

4.修改密碼 
分爲postgres用戶密碼和數據庫密碼,保持一致吧

passwd postgres

設置數據庫密碼:

su postgres
psql
ALTER USER postgres WITH PASSWORD '密碼';

二、安裝PostGIS

1.先安裝幾個工具包

yum  install wget net-tools epel-release -y

然後安裝postgis

yum install postgis24_10 postgis24_10-client -y

安裝拓展工具

yum install ogr_fdw10 -y
yum install pgrouting_10 -y

2.創建數據庫osm_db

CREATE DATABASE osm_db OWNER postgres;

進入數據庫

\c osm_db

安裝PostGis擴展

CREATE EXTENSION postgis;
CREATE EXTENSION postgis_topology;
CREATE EXTENSION ogr_fdw;

然後可以驗證是否安裝成功

SELECT postgis_full_version();

三、設置遠程連接

1.修改配置文件 
首先修改/home/postgresql_data/pg_hba.conf,改爲:

其次修改/home/postgresql_data/postgresql.conf,改爲: 

之後重啓服務

service postgresql-10 restart

2.開啓防火牆

①開啓防火牆對外端口服務

#開放postgresql服務
firewall-cmd --add-service=postgresql --permanent  

#或者開放5432端口
firewall-cmd --zone=public --add-port=5432/tcp --permanent

#命令含義:

#--zone #作用域

#--add-port=5432/tcp #添加端口,格式爲:端口/通訊協議

#--permanent #永久生效

②重啓防火牆

#重載防火牆
firewall-cmd --reload

#或重啓防火牆
systemctl restart firewalld.service

③查看已經對外開放的端口

firewall-cmd --list-ports

④其他相關命令

關閉防火牆:

systemctl stop firewalld.service

查看監聽(Listen)的端口

netstat -lntp

檢查端口被哪個進程佔用

netstat -lnp|grep 8080

刪除端口配置:

firewall-cmd --zone= public--remove-port=80/tcp --permanent

 

發佈了55 篇原創文章 · 獲贊 18 · 訪問量 16萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章