postgresql 與PostGis 離線環境安裝 上傳文件至伺服器 #安裝所需依賴 yum install /opt/PGsql-13-gis/rpm/* -y Postgresql安裝 tar -zxvf postgresql-13.2.tar.gz #進入該目錄 ./configure -- ...
postgresql 與PostGis 離線環境安裝
上傳文件至伺服器
#安裝所需依賴
yum install /opt/PGsql-13-gis/rpm/* -y
Postgresql安裝
tar -zxvf postgresql-13.2.tar.gz
#進入該目錄
./configure --prefix=/usr/local/pgsql --with-uuid=ossp --with-libxml
make
make install
#添加用戶
useradd postgres
mkdir /data/pgsql/data -p
mkdir /data/pgsql/log -p
touch /data/pgsql/log/server.log
chown postgres:postgres /data/pgsql/ -R
#關閉selinux
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
#使用systemd管理pgsql
cp contrib/start-scripts/linux /etc/init.d/pgsql
---
#添加init.d為可選項
vi /etc/init.d/pgsql
###
PGLOG="/usr/local/pgsql/log/server.log" #修改此項
###
chmod a+x /etc/init.d/pgsql
chkconfig --add /etc/init.d/pgsql
echo '/usr/local/pgsql/lib' >> /etc/ld.so.conf
ldconfig
---
echo 'export PATH=/usr/local/pgsql/bin/:$PATH' >> /etc/profile #配置環境變數
echo 'export PGDATA=/data/pgsql/data' >> /etc/profile
echo 'export PGHOME=/usr/local/pgsql/:$PATH' >> /etc/profile
echo 'export LD_LIBRARY_PATH=/usr/local/pgsql/lib/:$LD_LIBRARY_PATH' >> /etc/profile
source /etc/profile
su - postgres
###
initdb -D /data/pgsql/data/
pg_ctl -D /data/pgsql/data/ -l /data/pgsql/log/server.log start
pg_ctl -D /data/pgsql/data/ -l /data/pgsql/log/server.log stop
exit
###
systemctl start pgsql #啟動資料庫
#initdb報錯解決方案
echo "kernel.shmmni = 8192" >> /etc/sysctl.conf
sysctl -p
設置外部訪問
vi /usr/local/pgsql/data/pg_hba.conf
###
# IPv4 local connections:
host all all 0.0.0.0/0 trust
###
vi /usr/local/pgsql/data/postgresql.conf
###
listen_addresses = '*' # what IP address(es) to listen on;
###
firewall-cmd --zone=public --add-port=5432/tcp --permanent ##配置防火牆放行5432埠
firewall-cmd --reload ##防火牆重新載入配置文件
systemctl restart pgsql
安裝PostGis
echo 'export CMAKE_HOME=/usr/bin/cmake' >> /etc/profile
安裝proj
tar -zxvf proj-6.1.0.tar.gz
./configure --prefix=/usr/local/proj
make
make install
echo "/usr/local/proj/lib/" >> /etc/ld.so.conf
ldconfig
安裝geos
tar -xvf geos-3.8.0.tar.bz2
./configure --prefix=/usr/local/geos
make
make install
echo "/usr/local/geos/lib/" >> /etc/ld.so.conf
ldconfig
安裝gdal
tar -zxvf gdal-3.3.3.tar.gz
./configure --prefix=/usr/local/gdal --with-proj=/usr/local/proj
make
make install
echo "/usr/local/gdal/lib/" >> /etc/ld.so.conf
ldconfig
安裝json-c
unzip json-c-json-c-0.13.zip
./configure --prefix=/usr/local/json-c/
make
make install
echo "/usr/local/json-c/lib/" >> /etc/ld.so.conf
ldconfig
安裝protobuf
tar -zxvf protobuf-3.20.3.tar.gz
./autogen.sh
./configure --prefix=/usr/local/protobuf
make
make install
echo '/usr/local/protobuf/lib/' >> /etc/ld.so.conf
ldconfig
echo 'export PKG_CONFIG_PATH=/usr/local/protobuf/lib/pkgconfig/' >> /etc/profile
echo 'export PROTOBUF=/usr/local/protobuf' >> /etc/profile
source /etc/profile
安裝protobuf-c
tar -zxvf protobuf-c-1.4.0.tar.gz
./autogen.sh
./configure --prefix=/usr/local/protobuf-c/
make
make install
echo "/usr/local/protobuf-c/lib/" >> /etc/ld.so.conf
ldconfig
echo 'export PATH=$CMAKE_HOME/bin:$PROTOBUF_HOME/bin:$PATH:/usr/local/protobuf-c/bin' >> /etc/profile
echo 'export PATH=$CMAKE_HOME/bin:$PROTOBUF_HOME/bin:$PATH:/usr/local/protobuf-c/bin:/usr/local/gadl/bin' >> /etc/profile
source /etc/profile
安裝CGAL
tar -zxvf cgal-releases-CGAL-4.7.tar.gz
cmake3 . -DCMAKE_INSTALL_PREFIX=/usr/local/cgal
make
make install
echo "/usr/local/cgal/lib/" >> /etc/ld.so.conf
ldconfig
安裝SFCGAL
tar -zxvf SFCGAL-v1.3.10.tar.gz
cmake3 . -DCMAKE_INSTALL_PREFIX=/usr/local/sfcgal
make
make install
echo "/usr/local/sfcgal/lib64" >> /etc/ld.so.conf
ldconfig
安裝postgis
tar -zxvf postgis-3.1.6.tar.gz
./configure --prefix=/usr/local/postgis --with-pgsql=/usr/local/pgsql/bin/pg_config --with-pgconfig=/usr/local/pgsql/bin/pg_config --with-geosconfig=/usr/local/geos/bin/geos-config --with-projdir=/usr/local/proj --with-gdalconfig=/usr/local/gdal/bin/gdal-config --with-jsondir=/usr/local/json-c --with-protobufdir=/usr/local/protobuf-c --with-sfcgal=/usr/local/sfcgal/bin/sfcgal-config
make
make install
安裝pointcloud
tar -zxvf pointcloud-1.2.2.tar.gz
./autogen.sh
./configure --prefix=/usr/local/pointcloud --with-pgconfig=/usr/local/pgsql/bin/pg_config --with-xml2config=/usr/bin/xml2-config
make
make install
安裝插件
#進入postgresql-13.2下的contrib目錄
cd .../postgresql-13.2/contrib/
#安裝插件
cd adminpack/ && make && make install && cd ..
cd dblink/ && make && make install && cd ..
cd fuzzystrmatch/ && make && make install && cd ..
cd hstore/ && make && make install && cd ..
cd postgres_fdw/ && make && make install && cd ..
cd tablefunc/ && make && make install && cd ..
cd uuid-ossp/ && make && make install && cd ..
cd xml2/ && make && make install && cd ..
進入資料庫激活插件
ldconfig
su - postgres
psql
##
create extension postgis;
create extension postgis_raster;
create extension postgis_sfcgal;
create extension fuzzystrmatch ;
create extension postgis_tiger_geocoder;
create extension postgis_topology;
create extension adminpack;
create extension dblink ;
create extension hstore ;
create extension postgres_fdw ;
create extension tablefunc ;
create extension "uuid-ossp";
create extension xml2 ;
create extension pointcloud;
create extension pointcloud_postgis ;
測試插件是否安裝成功
-- 建表
CREATE TABLE cities ( id int4, name varchar(50) );
-- 添加位置欄位
SELECT AddGeometryColumn ('cities', 'the_geom', 4326, 'POINT', 2);
-- 插入幾條數據
INSERT INTO cities (id, the_geom, name) VALUES (1,ST_GeomFromText('POINT(-5.911 3.115)',4326),'BeiJing,China');
INSERT INTO cities (id, the_geom, name) VALUES (2,ST_GeomFromText('POINT(-5.921 3.215)',4326),'BeiJing,China');
INSERT INTO cities (id, the_geom, name) VALUES (3,ST_GeomFromText('POINT(-5.931 3.315)',4326),'BeiJing,China');
INSERT INTO cities (id, the_geom, name) VALUES (4,ST_GeomFromText('POINT(-5.941 3.415)',4326),'BeiJing,China');
INSERT INTO cities (id, the_geom, name) VALUES (5,ST_GeomFromText('POINT(-5.951 3.515)',4326),'BeiJing,China');
INSERT INTO cities (id, the_geom, name) VALUES (6,ST_GeomFromText('POINT(-15.951 13.515)',4326),'Out,BeiJing,China');
-- 查詢全表
select id,name,ST_AsText(the_geom) from cities ;
-- 查詢任意兩點間球面距離,並以id排序
SELECT p1.name,p2.name,ST_DistanceSphere(p1.the_geom,p2.the_geom) FROM cities AS p1, cities AS p2 WHERE p1.id > p2.id;
-- 查詢矩形內的點
select id, name, ST_AsText(the_geom) from cities where the_geom && ST_SetSRID(ST_MakeBox2D(ST_POINT(-10.0,-10.0),ST_POINT(10.0,10.0)),4326);
-- 任意給出幾個點,查詢該空間範圍內的點,第一個點和最後一個點應是同一個點
select id,name,ST_AsText(cities.the_geom) from cities where the_geom && ST_AsText(ST_MakePolygon(ST_GeomFromText('LINESTRING ( -10.31 10.97 , -10.15 -10.09 , 10.35 10.27 , 10.31 -10.97 , -10.31 10.97)')));
-- 同上
select id,name,ST_AsText(cities.the_geom) from cities where the_geom && ST_MakePolygon(ST_GeomFromText('LINESTRING ( -10.31 10.97 , -10.15 -10.09 , 10.35 10.27 , 10.09 -10.88 , 30.31 -40.97 , -1.11 60.33 , -10.31 10.97)'));
-- string應該用單引號,雙引號會報錯
update cities set name = 'America,LAS' where id = 5;
-- 刪除數據
delete from cities where id = 6;
-- 查詢距離點(-87.71 43.741)距離為151600000米的所有點
SELECT name,st_astext(the_geom) FROM cities WHERE ST_DWithin(ST_Transform(ST_GeomFromText('POINT(-87.71 43.741)',4326),26986),ST_Transform(the_geom,26986), 151600000);