PostgreSQL集群方案-Postgres-XL

Postgres-XL

主机 系统 hostname
192.168.1.42 centos7 node1 GTM
192.168.1.43 centos7 node2 gtm_proxy、Coordinator
192.168.1.44 centos7 node3 Datanode1 Master
192.168.1.48 centos7 node4 Datanode2 Master

datanode和Coordinator都是可以设置Slave(从节点),这里不设置了;设置从节点可以在主节点出问题是进行切换

环境准备

在每个节点配置主机host

192.168.1.42 node1
192.168.1.43 node2
192.168.1.44 node3
192.168.1.48 node4

在每个节点上安装依赖包

yum install -y flex bison readline-devel zlib-devel openjade docbook-style-dsssl  gcc

卸载操作系统自带的PostgreSQL

rpm -qa |grep postgresql
rpm -qa |grep postgresql | xargs rpm -e --nodeps
rpm -qa |grep postgresql

添加postgres用户

#建立组:
groupadd  postgres
# 建立用户:
useradd  -m  -d   /home/postgres   postgres  -g  postgres
# 初始化密码:
passwd postgres
#输入密码:zykj123456(举例)
# 注:如果需要删除postgres用户,可以以root用户执行命令:
userdel -r postgres
# 如果需要,为postgres在指定文件夹赋予权限
sudo chown -R postgres:postgres  /home/postgres

配置免密登陆

root用户

ssh-keygen
ssh-copy-id -i ~/.ssh/id_rsa.pub root@node1
ssh-copy-id -i ~/.ssh/id_rsa.pub root@node2
ssh-copy-id -i ~/.ssh/id_rsa.pub root@node3
ssh-copy-id -i ~/.ssh/id_rsa.pub root@node4

postgres用户

 su - postgres
 ssh-keygen
 ssh-copy-id -i ~/.ssh/id_rsa.pub postgres@node1
 ssh-copy-id -i ~/.ssh/id_rsa.pub postgres@node2
 ssh-copy-id -i ~/.ssh/id_rsa.pub postgres@node3
 ssh-copy-id -i ~/.ssh/id_rsa.pub postgres@node4

如果配置完成后,发现ssh并不免密,可按下面操作进行完善:

chmod 755 /home/postgres
chmod 700 /home/postgres/.ssh
chmod 644 /home/postgres/.ssh/authorized_keys

配置防火墙

关闭防火墙

# 查看该防火墙状态
systemctl status firewalld
# 关闭防火墙
systemctl stop firewalld
# 关闭防火墙,避免重启后恢复
systemctl disable firewalld

或在防火墙中开放端口
遇到防护墙不能关闭的情况,可使用该步骤!
具体需要开放的端口参加(主机规划部分),以5432为例:
使用这些命令来永久打开一个新端口(如TCP/5432)。

sudo firewall-cmd --zone=public --add-port=5432/tcp --permanent
sudo firewall-cmd --reload

关闭SELinux

查看SELinux状态,执行:

/usr/sbin/sestatus -v 

如果SELinux status参数为enabled即为开启状态

临时关闭:

setenforce 0

永久关闭SELinux,执行:

vim /etc/selinux/config

将 SELINUX=enforcing 改为 SELINUX=disabled
重启后生效

修改内核参数

在/etc/sysctl.conf中添加参数kernel.sem

 vi /etc/sysctl.conf
kernel.sem = 50100 128256000 50100 2560

使参数生效

sysctl -p

验证参数是否生效

ipcs -ls

—— Semaphore Limits ——–
max number of arrays = 2560
max semaphores per array = 50100
max semaphores system wide = 128256000
max ops per semop call = 50100
semaphore max value = 32767

在每个节点上安装Postgres-xl

postgres-xl-10

上传postgres-xl-10r1.1.tar.gz压缩包到指定位置/usr/local/pg

tar -zxvf postgres-xl-10r1.1.tar.gz
chown -R postgres:postgres postgres-xl-10r1.1

如果报错信息如下


tar (child): bzip2:无法 exec: 没有那个文件或目录
tar (child): Error is not recoverable: exiting now
tar: Child returned status 2
tar: Error is not recoverable: exiting now

原因:缺少bzip2包

yum install -y bzip2

切换用户

su - postgres

安装

 cd /usr/local/pg/postgres-xl-10r1.1
 ./configure --prefix=/home/postgres/pgxl10
 # pgxl10:  ./configure --prefix=/home/postgres/pgxl10
 make
 make install

安装扩展

 cd contrib
 make
 make install

如果报错如下,则是需要安装gcc编译器

configure: error: no acceptable C compiler found in $PATH

yum -y install gcc

配置环境变量

su - postgres
 #注意这里环境变量一定要添加到.bashrc文件中,否则会找不到相关命令
vi .bashrc

export PGHOME=/home/postgres/pgxl10
export PGUSER=postgres
export LD_LIBRARY_PATH=$PGHOME/lib:$LD_LIBRARY_PATH
export PATH=$PGHOME/bin:$PATH

使环境变量生效:

source ~/.bashrc

验证环境变量是否生效:

pg_ctl --version 

image-20201125153308528

配置Postgres-XL集群

在GTM节点(node1)上以postgres用户执行:

pgxc_ctl              ---初次执行,会提示Error说没有配置文件,忽略即可
PGXC prepare            ---执行该命令将会生成一份配置文件模板
PGXC exit               --退出 pgxc_ctl交互窗

执行完成后,在postgres用户根目录下,会生成一个pgxc_ctl目录,编辑其中的pgxc_ctl.conf文件

vi pgxc_ctl.conf
#!/usr/bin/env bash
#
# Postgres-XC Configuration file for pgxc_ctl utility. 
#
# Configuration file can be specified as -c option from pgxc_ctl command.   Default is
# $PGXC_CTL_HOME/pgxc_ctl.org.
#
# This is bash script so you can make any addition for your convenience to configure
# your Postgres-XC cluster.
#
# Please understand that pgxc_ctl provides only a subset of configuration which pgxc_ctl
# provide.  Here's several several assumptions/restrictions pgxc_ctl depends on.
#
# 1) All the resources of pgxc nodes has to be owned by the same user.   Same user means
#    user with the same user name.  User ID may be different from server to server.
#    This must be specified as a variable $pgxcOwner.
#
# 2) All the servers must be reacheable via ssh without password.   It is highly recommended
#    to setup key-based authentication among all the servers.
#
# 3) All the databases in coordinator/datanode has at least one same superuser.  Pgxc_ctl
#    uses this user to connect to coordinators and datanodes.   Again, no password should
#    be used to connect.  You have many options to do this, pg_hba.conf, pg_ident.conf and
#    others.  Pgxc_ctl provides a way to configure pg_hba.conf but not pg_ident.conf.   This
#    will be implemented in the later releases.
#
# 4) Gtm master and slave can have different port to listen, while coordinator and datanode
#    slave should be assigned the same port number as master.
#
# 5) Port nuber of a coordinator slave must be the same as its master.
#
# 6) Master and slave are connected using synchronous replication.  Asynchronous replication
#    have slight (almost none) chance to bring total cluster into inconsistent state.
#    This chance is very low and may be negligible.  Support of asynchronous replication
#    may be supported in the later release.
#
# 7) Each coordinator and datanode can have only one slave each.  Cascaded replication and
#    multiple slave are not supported in the current pgxc_ctl.
#
# 8) Killing nodes may end up with IPC resource leak, such as semafor and shared memory.
#    Only listening port (socket) will be cleaned with clean command.
#
# 9) Backup and restore are not supported in pgxc_ctl at present.   This is a big task and
#    may need considerable resource.
#
#========================================================================================
#
#
# pgxcInstallDir variable is needed if you invoke "deploy" command from pgxc_ctl utility.
# If don't you don't need this variable.
pgxcInstallDir=$PGHOME
pgxlDATA=$PGHOME/data
#---- OVERALL -----------------------------------------------------------------------------
#
pgxcOwner=postgres			# owner of the Postgres-XC databaseo cluster.  Here, we use this
						# both as linus user and database user.  This must be
						# the super user of each coordinator and datanode.
pgxcUser=$pgxcOwner		# OS user of Postgres-XC owner

tmpDir=/tmp					# temporary dir used in XC servers
localTmpDir=$tmpDir			# temporary dir used here locally

configBackup=n					# If you want config file backup, specify y to this value.
configBackupHost=pgxc-linker	# host to backup config file
configBackupDir=$HOME/pgxc		# Backup directory
configBackupFile=pgxc_ctl.bak	# Backup file name --> Need to synchronize when original changed.

#---- GTM ------------------------------------------------------------------------------------

# GTM is mandatory.  You must have at least (and only) one GTM master in your Postgres-XC cluster.
# If GTM crashes and you need to reconfigure it, you can do it by pgxc_update_gtm command to update
# GTM master with others.   Of course, we provide pgxc_remove_gtm command to remove it.  This command
# will not stop the current GTM.  It is up to the operator.


#---- GTM Master -----------------------------------------------

#---- Overall ----
gtmName=gtm
gtmMasterServer=node1
gtmMasterPort=6666
gtmMasterDir=$pgxlDATA/nodes/gtm

#---- Configuration ---
gtmExtraConfig=none			# Will be added gtm.conf for both Master and Slave (done at initilization only)
gtmMasterSpecificExtraConfig=none	# Will be added to Master's gtm.conf (done at initialization only)

#---- GTM Slave -----------------------------------------------

# Because GTM is a key component to maintain database consistency, you may want to configure GTM slave
# for backup.

#---- Overall ------
gtmSlave=n					# Specify y if you configure GTM Slave.   Otherwise, GTM slave will not be configured and
							# all the following variables will be reset.
gtmSlaveName=gtmSlave
gtmSlaveServer=node12		# value none means GTM slave is not available.  Give none if you don't configure GTM Slave.
gtmSlavePort=20001			# Not used if you don't configure GTM slave.
gtmSlaveDir=$HOME/pgxc/nodes/gtm	# Not used if you don't configure GTM slave.
# Please note that when you have GTM failover, then there will be no slave available until you configure the slave
# again. (pgxc_add_gtm_slave function will handle it)

#---- Configuration ----
gtmSlaveSpecificExtraConfig=none # Will be added to Slave's gtm.conf (done at initialization only)

#---- GTM Proxy -------------------------------------------------------------------------------------------------------
# GTM proxy will be selected based upon which server each component runs on.
# When fails over to the slave, the slave inherits its master's gtm proxy.  It should be
# reconfigured based upon the new location.
#
# To do so, slave should be restarted.   So pg_ctl promote -> (edit postgresql.conf and recovery.conf) -> pg_ctl restart
#
# You don't have to configure GTM Proxy if you dont' configure GTM slave or you are happy if every component connects
# to GTM Master directly.  If you configure GTL slave, you must configure GTM proxy too.

#---- Shortcuts ------
gtmProxyDir=$pgxlDATA/nodes/gtm_pxy

#---- Overall -------
gtmProxy=y				# Specify y if you conifugre at least one GTM proxy.   You may not configure gtm proxies
						# only when you dont' configure GTM slaves.
						# If you specify this value not to y, the following parameters will be set to default empty values.
						# If we find there're no valid Proxy server names (means, every servers are specified
						# as none), then gtmProxy value will be set to "n" and all the entries will be set to
						# empty values.
gtmProxyNames=(gtm_pxy1 gtm_pxy2)	# No used if it is not configured
gtmProxyServers=(node2 node3)			# Specify none if you dont' configure it.
gtmProxyPorts=(6666 6666)				# Not used if it is not configured.
gtmProxyDirs=($gtmProxyDir $gtmProxyDir)	# Not used if it is not configured.

#---- Configuration ----
gtmPxyExtraConfig=none		# Extra configuration parameter for gtm_proxy.  Coordinator section has an example.
gtmPxySpecificExtraConfig=(none none)

#---- Coordinators ----------------------------------------------------------------------------------------------------

#---- shortcuts ----------
coordMasterDir=$pgxlDATA/nodes/coord
coordSlaveDir=$pgxlDATA/nodes/coord_slave
coordArchLogDir=$pgxlDATA/nodes/coord_archlog

#---- Overall ------------
coordNames=(coord1)		# Master and slave use the same name
coordPorts=(5432)			# Master ports
poolerPorts=(20004)			# Master pooler ports
coordPgHbaEntries=(0.0.0.0/0)				# Assumes that all the coordinator (master/slave) accepts
												# the same connection
												# This entry allows only $pgxcOwner to connect.
												# If you'd like to setup another connection, you should
												# supply these entries through files specified below.
# Note: The above parameter is extracted as "host all all 0.0.0.0/0 trust".   If you don't want
# such setups, specify the value () to this variable and suplly what you want using coordExtraPgHba
# and/or coordSpecificExtraPgHba variables.
#coordPgHbaEntries=(::1/128)	# Same as above but for IPv6 addresses

#---- Master -------------
coordMasterServers=(node2)		# none means this master is not available
coordMasterDirs=$coordMasterDir
coordMaxWALsernder=10	# max_wal_senders: needed to configure slave. If zero value is specified,
						# it is expected to supply this parameter explicitly by external files
						# specified in the following.	If you don't configure slaves, leave this value to zero.
coordMaxWALSenders=$coordMaxWALsernder
						# max_wal_senders configuration for each coordinator.

#---- Slave -------------
coordSlave=n			# Specify y if you configure at least one coordiantor slave.  Otherwise, the following
						# configuration parameters will be set to empty values.
						# If no effective server names are found (that is, every servers are specified as none),
						# then coordSlave value will be set to n and all the following values will be set to
						# empty values.

coordUserDefinedBackupSettings=n	# Specify whether to update backup/recovery
									# settings during standby addition/removal.

coordSlaveSync=y		# Specify to connect with synchronized mode.
coordSlaveServers=(node07 node08 node09 node06)			# none means this slave is not available
coordSlavePorts=(20004 20005 20004 20005)			# Master ports
coordSlavePoolerPorts=(20010 20011 20010 20011)			# Master pooler ports
coordSlaveDirs=($coordSlaveDir $coordSlaveDir $coordSlaveDir $coordSlaveDir)
coordArchLogDirs=($coordArchLogDir $coordArchLogDir $coordArchLogDir $coordArchLogDir)

#---- Configuration files---
# Need these when you'd like setup specific non-default configuration 
# These files will go to corresponding files for the master.
# You may supply your bash script to setup extra config lines and extra pg_hba.conf entries 
# Or you may supply these files manually.
coordExtraConfig=coordExtraConfig	# Extra configuration file for coordinators.  
						# This file will be added to all the coordinators'
						# postgresql.conf
# Pleae note that the following sets up minimum parameters which you may want to change.
# You can put your postgresql.conf lines here.
cat > $coordExtraConfig <<EOF
#================================================
# Added to all the coordinator postgresql.conf
# Original: $coordExtraConfig
log_destination = 'stderr'
logging_collector = on
log_directory = 'pg_log'
listen_addresses = '*'
max_connections = 512
EOF

# Additional Configuration file for specific coordinator master.
# You can define each setting by similar means as above.
coordSpecificExtraConfig=(none none none none)
coordExtraPgHba=none	# Extra entry for pg_hba.conf.  This file will be added to all the coordinators' pg_hba.conf
coordSpecificExtraPgHba=(none none none none)

#----- Additional Slaves -----
#
# Please note that this section is just a suggestion how we extend the configuration for
# multiple and cascaded replication.   They're not used in the current version.
#
coordAdditionalSlaves=n		# Additional slave can be specified as follows: where you
coordAdditionalSlaveSet=(cad1)		# Each specifies set of slaves.   This case, two set of slaves are
											# configured
cad1_Sync=n		  		# All the slaves at "cad1" are connected with asynchronous mode.
							# If not, specify "y"
							# The following lines specifies detailed configuration for each
							# slave tag, cad1.  You can define cad2 similarly.
cad1_Servers=(node08 node09 node06 node07)	# Hosts
cad1_dir=$HOME/pgxc/nodes/coord_slave_cad1
cad1_Dirs=($cad1_dir $cad1_dir $cad1_dir $cad1_dir)
cad1_ArchLogDir=$HOME/pgxc/nodes/coord_archlog_cad1
cad1_ArchLogDirs=($cad1_ArchLogDir $cad1_ArchLogDir $cad1_ArchLogDir $cad1_ArchLogDir)


#---- Datanodes -------------------------------------------------------------------------------------------------------

#---- Shortcuts --------------
datanodeMasterDir=$pgxlDATA/nodes/dn_master
datanodeSlaveDir=$pgxlDATA/nodes/dn_slave
datanodeArchLogDir=$pgxlDATA/nodes/datanode_archlog

#---- Overall ---------------
#primaryDatanode=datanode1				# Primary Node.
# At present, xc has a priblem to issue ALTER NODE against the primay node.  Until it is fixed, the test will be done
# without this feature.
primaryDatanode=datanode1				# Primary Node.
datanodeNames=(datanode1 datanode2)
datanodePorts=(5433 5433)	# Master ports
datanodePoolerPorts=(20005 20005)	# Master pooler ports
datanodePgHbaEntries=(0.0.0.0/0)	# Assumes that all the coordinator (master/slave) accepts
										# the same connection
										# This list sets up pg_hba.conf for $pgxcOwner user.
										# If you'd like to setup other entries, supply them
										# through extra configuration files specified below.
# Note: The above parameter is extracted as "host all all 0.0.0.0/0 trust".   If you don't want
# such setups, specify the value () to this variable and suplly what you want using datanodeExtraPgHba
# and/or datanodeSpecificExtraPgHba variables.
#datanodePgHbaEntries=(::1/128)	# Same as above but for IPv6 addresses

#---- Master ----------------
datanodeMasterServers=(node3 node4)	# none means this master is not available.
													# This means that there should be the master but is down.
													# The cluster is not operational until the master is
													# recovered and ready to run.	
datanodeMasterDirs=($datanodeMasterDir/datanode1 $datanodeMasterDir/datanode1)
datanodeMaxWalSender=10								# max_wal_senders: needed to configure slave. If zero value is 
													# specified, it is expected this parameter is explicitly supplied
													# by external configuration files.
													# If you don't configure slaves, leave this value zero.
datanodeMaxWALSenders=($datanodeMaxWalSender $datanodeMaxWalSender)
						# max_wal_senders configuration for each datanode

#---- Slave -----------------
datanodeSlave=n			# Specify y if you configure at least one coordiantor slave.  Otherwise, the following
						# configuration parameters will be set to empty values.
						# If no effective server names are found (that is, every servers are specified as none),
						# then datanodeSlave value will be set to n and all the following values will be set to
						# empty values.

datanodeUserDefinedBackupSettings=n	# Specify whether to update backup/recovery
									# settings during standby addition/removal.

datanodeSlaveServers=(node07 node08 node09 node06)	# value none means this slave is not available
datanodeSlavePorts=(20008 20009 20008 20009)	# value none means this slave is not available
datanodeSlavePoolerPorts=(20012 20013 20012 20013)	# value none means this slave is not available
datanodeSlaveSync=y		# If datanode slave is connected in synchronized mode
datanodeSlaveDirs=($datanodeSlaveDir $datanodeSlaveDir $datanodeSlaveDir $datanodeSlaveDir)
datanodeArchLogDirs=( $datanodeArchLogDir $datanodeArchLogDir $datanodeArchLogDir $datanodeArchLogDir )

# ---- Configuration files ---
# You may supply your bash script to setup extra config lines and extra pg_hba.conf entries here.
# These files will go to corresponding files for the master.
# Or you may supply these files manually.
datanodeExtraConfig=none	# Extra configuration file for datanodes.  This file will be added to all the 
							# datanodes' postgresql.conf
datanodeSpecificExtraConfig=(none none none none)
datanodeExtraPgHba=none		# Extra entry for pg_hba.conf.  This file will be added to all the datanodes' postgresql.conf
datanodeSpecificExtraPgHba=(none none none none)

#----- Additional Slaves -----
datanodeAdditionalSlaves=n	# Additional slave can be specified as follows: where you
# datanodeAdditionalSlaveSet=(dad1 dad2)		# Each specifies set of slaves.   This case, two set of slaves are
											# configured
# dad1_Sync=n		  		# All the slaves at "cad1" are connected with asynchronous mode.
							# If not, specify "y"
							# The following lines specifies detailed configuration for each
							# slave tag, cad1.  You can define cad2 similarly.
# dad1_Servers=(node08 node09 node06 node07)	# Hosts
# dad1_dir=$HOME/pgxc/nodes/coord_slave_cad1
# dad1_Dirs=($cad1_dir $cad1_dir $cad1_dir $cad1_dir)
# dad1_ArchLogDir=$HOME/pgxc/nodes/coord_archlog_cad1
# dad1_ArchLogDirs=($cad1_ArchLogDir $cad1_ArchLogDir $cad1_ArchLogDir $cad1_ArchLogDir)

#---- WAL archives -------------------------------------------------------------------------------------------------
walArchive=n	# If you'd like to configure WAL archive, edit this section.
				# Pgxc_ctl assumes that if you configure WAL archive, you configure it
				# for all the coordinators and datanodes.
				# Default is "no".   Please specify "y" here to turn it on.
#
#		End of Configuration Section
#
#==========================================================================================================================

#========================================================================================================================
# The following is for extension.  Just demonstrate how to write such extension.  There's no code
# which takes care of them so please ignore the following lines.  They are simply ignored by pgxc_ctl.
# No side effects.
#=============<< Beginning of future extension demonistration >> ========================================================
# You can setup more than one backup set for various purposes, such as disaster recovery.
walArchiveSet=(war1 war2)
war1_source=(master)	# you can specify master, slave or ano other additional slaves as a source of WAL archive.
					# Default is the master
wal1_source=(slave)
wal1_source=(additiona_coordinator_slave_set additional_datanode_slave_set)
war1_host=node10	# All the nodes are backed up at the same host for a given archive set
war1_backupdir=$HOME/pgxc/backup_war1
wal2_source=(master)
war2_host=node11
war2_backupdir=$HOME/pgxc/backup_war2
#=============<< End of future extension demonistration >> ========================================================

初始化集群

pgxc_ctl  -c  ~/pgxc_ctl/pgxc_ctl.conf init all

启动集群

pgxc_ctl  -c /home/postgres/pgxc_ctl/pgxc_ctl.conf start all

停止集群

pgxc_ctl -c /home/postgres/pgxc_ctl/pgxc_ctl.conf stop all

删除集群

在所有节点上以postgres用户执行:

rm /home/postgres/pgxl9.5/data/nodes

使用Postgres-XL集群

查看数据库版本

select version();
-- PostgreSQL 9.5.8 (Postgres-XL 9.5r1.6) on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44), 64-bit

建表

【REPLICATION表】即复制表,表的内容在各个数据节点完全相同。

CREATE TABLE repltab (id int, value text) DISTRIBUTE BY REPLICATION;

【DISTRIBUTE表】及分片表,按照分片(sharding)规则,数据会被分配到不同的数据节点中。通过协调节点查询完整的数据集合。

CREATE TABLE disttab(id int, value text) DISTRIBUTE BY HASH(id);

插入数据

-- 随机生成150万数据,插入disttab分片表
insert into disttab(id, value)
select n, md5(random()::text)
from generate_series(1, 1500000) n;
-- 随机生成150万数据,插入repltab复制表
insert into repltab(id, value)
select n, md5(random()::text)
from generate_series(1, 1500000) n;

查看分片表的数据分布情况

-- 查看分片表的数据分布情况
SELECT xc_node_id, count(*) FROM disttab GROUP BY xc_node_id;

随机抽样

-- system抽样方法
EXPLAIN ANALYZE
select *
from disttab TABLESAMPLE system(1);

-- bernoulli抽样方法
EXPLAIN ANALYZE
select *
from disttab TABLESAMPLE bernoulli(1);

安装postgis

网上教程:

教程一

教程二

当给pgxl安装postgis时,只需要给pgxl的coordinator、datanode节点安装postgis,其余节点不需要安装。

关闭集群

在node1(GTM)节点运行

pgxc_ctl -c /home/postgres/pgxc_ctl/pgxc_ctl.conf stop all

下载postgis安装包及其依赖包

mkdir /usr/local/pg/postgis-jars
cd postgis-jars

wget http://download.osgeo.org/gdal/2.0.1/gdal-2.3.2.tar.gz
wget http://download.osgeo.org/geos/geos-3.7.0.tar.bz2
wget http://download.osgeo.org/proj/proj-5.2.0.tar.gz
wget https://github.com/json-c/json-c/archive/json-c-0.13.1-20180305.tar.gz
wget https://github.com/google/protobuf/releases/download/v3.6.1/protobuf-all-3.6.1.tar.gz
wget https://github.com/protobuf-c/protobuf-c/releases/download/v1.3.1/protobuf-c-1.3.1.tar.gz
wget http://download.osgeo.org/postgis/source/postgis-3.0.2.tar.gz

yum安装基础依赖

yum -y install gcc-c++.x86_64
yum install libxml2 libxslt -y
yum -y install libxml2 libxml2-dev libxml libxml2*

安装postgis

必须安装的依赖

sudo chown -R postgres:postgres  /usr/local/pg/lib
su - postgres
# 安装proj
tar -zxvf proj-5.2.0.tar.gz
cd proj-5.2.0
./configure --prefix=/home/postgres/proj
#make -j4 使用多个核心去跑任务
make
make install
# 安装geos
# yum install -y bzip2
tar -jxvf geos-3.7.0.tar.bz2
cd geos-3.7.0
./configure --prefix=/home/postgres/geos
#make -j4
make
make install
# 安装gdal
tar -zxvf gdal-2.3.2.tar.gz
cd gdal-2.3.2
./configure --prefix=/home/postgres/gdal
#make -j4
make
make install

选择安装的依赖

我需要使用mvt和geoJson的功能

安装json-c
wget https://github.com/json-c/json-c/archive/json-c-0.13.1-20180305.tar.gz
tar -zxvf json-c-0.13.1-20180305.tar.gz
cd json-c-json-c-0.13.1-20180305
./configure  --prefix=/home/postgres/json-c-0.13.1
make
make install
安装Protobuf(root用户)
su root
#安装相关依赖
sudo yum install autoconf -y
sudo yum install automake -y
sudo yum install libtool -y

wget https://github.com/google/protobuf/releases/download/v3.6.1/protobuf-all-3.6.1.tar.gz
tar zxvf protobuf-all-3.6.1.tar.gz
cd protobuf-3.6.1

./autogen.sh
#配置安装目录
./configure  --prefix=/usr/local/protobuf-3.6.1
make
make install
#配置环境变量,增加/home/postgres/protobuf-3.6.1安装目录
vim /etc/profile
export CMAKE_HOME=/usr/bin/cmake
export PKG_CONFIG_PATH=/usr/local/protobuf-3.6.1/lib/pkgconfig
export PROTOBUF_HOME=/usr/local/protobuf-3.6.1
export PATH=$CMAKE_HOME/bin:$PROTOBUF_HOME/bin:$PATH
#保存退出
source /etc/profile
#验证protobuf执行程序
protoc --version
#出现libprotoc 3.6.1说明安装成功
安装protobuf-c(root用户)
su root
wget https://github.com/protobuf-c/protobuf-c/releases/download/v1.3.1/protobuf-c-1.3.1.tar.gz
tar -zxvf protobuf-c-1.3.1.tar.gz    
cd protobuf-c-1.3.1  
#导入protobuf的pkgconfig,否则"--No package 'protobuf' found"
export PKG_CONFIG_PATH=/usr/local/protobuf-3.6.1/lib/pkgconfig
./configure  --prefix=/usr/local/protobuf-c-1.3.1
make    
make install   
#配置环境变量,增加下protobuf-c-1.3.1/bin
vim /etc/profile
# 在export PATH=$CMAKE_HOME/bin:$PROTOBUF_HOME/bin:$PATH 追加
export PATH=$CMAKE_HOME/bin:$PROTOBUF_HOME/bin:$PATH:/usr/local/protobuf-c-1.3.1/bin
#保存退出
source /etc/profile
修改动态库的路径(so库的路径)
vim /etc/ld.so.conf
#编辑内容如下
include ld.so.conf.d/*.conf

/usr/local/protobuf-3.6.1/lib
/usr/local/protobuf-c-1.3.1/lib
#/home/postgres/lib
#/usr/local/proj-5.2.0/lib
#/usr/local/gdal-2.3.2/lib
#/usr/local/geos-3.7.0/lib
#/usr/local//sfcgal-1.3.6/lib64
#/usr/local/json-c-0.13.1/lib

#编辑完成后wq!保存退出
#保存配置,重启生效
[root@xx /]# ldconfig -v
安装postgis
# 安装postgis(只有postgis版本)
tar -zxvf postgis-3.0.2.tar.gz
cd postgis-3.0.2
./configure --prefix=/home/postgres/postgis --with-pgsql=/home/postgres/pgxl10/bin/pg_config --with-pgconfig=/home/postgres/pgxl10/bin/pg_config --with-geosconfig=/home/postgres/geos/bin/geos-config  --with-projdir=/home/postgres/proj --with-gdalconfig=/home/postgres/gdal/bin/gdal-config
#make -j4
make
make install


#安装postgis(mvt、json)
tar -zxvf postgis-3.0.2.tar.gz
cd postgis-3.0.2
./configure --prefix=/home/postgres/postgis --with-pgsql=/home/postgres/pgxl10/bin/pg_config --with-pgconfig=/home/postgres/pgxl10/bin/pg_config --with-geosconfig=/home/postgres/geos/bin/geos-config  --with-projdir=/home/postgres/proj --with-gdalconfig=/home/postgres/gdal/bin/gdal-config --with-jsondir=/home/postgres/json-c-0.13.1  --with-protobufdir=/usr/local/protobuf-c-1.3.1
#make -j4
make
make install

安装问题截图

有问题的

image-20201201090415101

image-20201203172447319

正常完成的

image-20201203194617409

image-20201204150844564

完善postgis依赖包,权限完善

直接使用可能会报类似如下错误

image-20201204162434870

# sudo chown -R postgres:postgres  /usr/local/pg/lib

cp /home/postgres/geos/lib/libgeos_c.so.1 /home/postgres/pgxl10/lib/
cp /home/postgres/proj/lib/libproj.so.13 /home/postgres/pgxl10/lib/
cp /home/postgres/json-c-0.13.1/lib/libjson-c.so.4 /home/postgres/pgxl10/lib/

重启集群

在node1(GTM)节点运行

pgxc_ctl  -c /home/postgres/pgxc_ctl/pgxc_ctl.conf start all

测试postgis

--测试postgis
EXPLAIN ANALYZE select st_geomfromtext('Point(103.667546 36.534295)');
-- 测试geohash
select st_geohash(st_transform(st_geomfromtext('Point(103.667546 36.534295)', 4326), 4326), 12) as geohash;
-- 测试mvt
WITH mvtgeom AS
         (
             SELECT ST_TileEnvelope(12, 513, 412) AS geom
         )
SELECT ST_AsMVT(mvtgeom.*)
FROM mvtgeom;

如果以上查询都正常那么就安装成功了

安装扩展

安装uuid
yum -y install e2fsprogs-devel uuid-devel libuuid-devel
cd /usr/local/pg/postgres-xl-10r1.1/
./configure --prefix=/home/postgres/pgxl10/ -with-uuid=ossp
cd /usr/local/pg/postgres-xl-10r1.1/contrib/uuid-ossp
make
make install

猜你喜欢

转载自blog.csdn.net/qq_36213352/article/details/117856594