1、准备
Postgres下载 10.19
CMake下载 3.16.2
TimescaleDB下载 1.4.0
相关RPM包下载 openssl-devel gcc-c++
yum install yum-plugin-downloadonly
yum install --downloadonly --downloaddir=/home/dependency/ openssl-devel
yum install --downloadonly --downloaddir=/home/dependency/ gcc-c++
2、手动安装
将上述文件都拷贝到目标服务器
首先到RMP目录下安装相关依赖
rpm -Uvh --force --nodeps *.rpm
创建软件安装目录
mkdir -pv /opt/software
postgres安装包解压
tar zxvf postgresql-10.19-1-linux-x64-binaries.tar.gz -C /opt/software/
创建数据目录并授权
#进入postgres安装目录
pushd /opt/software/pgsql/
mkdir pgsql_data
useradd postgres
echo "password" | passwd postgres --stdin > /dev/null 2>&1
chown postgres /opt/software/pgsql/pgsql_data/
添加环境变量
echo 'export PATH=/opt/software/pgsql/bin:$PATH' >>/etc/profile;
echo 'export PGDATA=/opt/software/pgsql/pgsql_data'>>/etc/profile;
#生效环境变量
source /etc/profile
#退回安装包目录
popd
解压cmake
tar -zxvf cmake-3.16.2.tar.gz -C /opt/software/pgsql/
编译cmake
pushd /opt/software/pgsql/cmake-3.16.2
./bootstrap
gmake
make install
popd
解压timescaledb
tar -zxvf timescaledb.tar.gz -C /opt/software/pgsql
编译timescaledb
pushd /opt/software/pgsql/timescaledb
./bootstrap
cd build && make
make install
popd
修改密码、初始化postgres
su - postgres
/opt/software/pgsql/bin/initdb -D /opt/software/pgsql/pgsql_data
/opt/software/pgsql/bin/pg_ctl -D /opt/software/pgsql/pgsql_data/ start
cd /opt/software/pgsql/bin
./psql
ALTER USER postgres WITH PASSWORD 'your passd';
\q
/opt/software/pgsql/bin/pg_ctl stop
#退出用户
exit;
修改postgresql.conf配置文件,找到data_directory和shared_preload_libraries,放开注释,分别填入填入上面创建的数据目录和timescaledb安装目录名(不用绝对路径)
编写开机自启动脚本
vi /usr/lib/systemd/system/postgresql.service
[Unit]
Description=PostgreSQL 10.11 database server
Documentation=https://www.postgresql.org/docs/10/index.html
After=syslog.target
After=network.target
[Service]
Type=forking
User=postgres
Group=postgres
# Location of database directory
Environment=PGDATA=/opt/software/pgsql/pgsql_data/
OOMScoreAdjust=-1000
ExecStart=/opt/software/pgsql/bin/pg_ctl start -D ${PGDATA} -s -w -t 300
ExecStop=/opt/software/pgsql/pg_ctl stop -D ${PGDATA} -s -m fast
ExecReload=/opt/software/pgsql/bin/pg_ctl reload -D ${PGDATA} -s
TimeoutSec=0
[Install]
WantedBy=multi-user.target
修改脚本权限,并加入开机自启动
chmod 754 /usr/lib/systemd/system/postgresql.service
systemctl enable postgresql.service
systemctl start postgresql.service
查看服务是否启动成功
systemctl status postgresql.service
3、脚本自动安装
shell脚本(基于准备的包)
#!/bin/bash
#使用root用户操作
if [ "$(id -u)" != "0" ]; then
echo "You need to be 'root' dude." 1>&2
exit 1
fi
echo "=================================================="
echo " welcome to the postgres installer "
echo " "
path=`pwd`
#判断字符串是否相等
if [ "$path" = "/" ];then
path=''
fi
#安装包目录
pkgDir="$pwd/pkg"
#依赖目录
depenDir="$pkgDir/dependency"
#安装postgres数据库
function postgresFun()
{
#离线安装依赖包
rpm -Uvh --force --nodeps $dependencyDir/*.rpm
#检测linux是否安装postgreSql
PGSQL_VERSION=`psql --version 2>&1`
resultPGsqlInfo=$(echo $PGSQL_VERSION | grep "${ErrorInfo}")
if [[ "$resultPGsqlInfo" != "" ]]
then
echo "#######安装postgreSql数据库#######"
#解压缩
tar -zxvf $pkgDir/postgresql-10.19-1-linux-x64-binaries.tar.gz -C /opt/software/
cd /opt/software/pgsql
#创建数据目录并授权
mkdir -pv pgsql_data
useradd postgres
echo "your passd" | passwd postgres --stdin > /dev/null 2>&1
chown postgres /opt/software/pgsql/pgsql_data/
#切换用户初始化数据库
echo "#######正在初始化postgreSql#######"
#添加环境变量
echo 'export PATH=/opt/software/pgsql/bin:$PATH' >>/etc/profile;
echo 'export PGDATA=/opt/software/pgsql/pgsql_data'>>/etc/profile;
#生效环境变量
source /etc/profile
#判断cmake是否安装
MAKE_VERSION=`cmake --version 2>&1`
resultMAKEInfo=$(echo $MAKE_VERSION | grep "${ErrorInfo}")
if [[ "$resultMAKEInfo" != "" ]]
then
echo "######正在初始化CMAKE#######"
tar -zxvf $pkgDir/cmake-3.16.2.tar.gz -C /opt/software/pgsql/
cd cmake-3.16.2
./bootstrap
gmake
make install
fi
#安装时序数据库
tar -zxvf $pkgDir/timescaledb.tar.gz -C /opt/software/pgsql
cd /opt/software/pgsql/timescaledb
./bootstrap
cd build && make
make install
#设置开机自启动(自启动脚本参考手动安装)
\cp -rf "$docDir/psql/postgresql.service" /usr/lib/systemd/system/
chmod 754 /usr/lib/systemd/system/postgresql.service
systemctl enable postgresql.service
#修改密码
su - postgres <<CONSOLE
/opt/software/pgsql/bin/initdb -D /opt/software/pgsql/pgsql_data
sed -i "$a\要插入的文字" /opt/software/pgsql/pgsql_data/postgresql.conf
#\cp -rf "$docDir/psql/pg_hba.conf" /opt/software/pgsql/pgsql_data/
echo "data_directory='/opt/software/pgsql/pgsql_data'" >> /opt/software/pgsql/pgsql_data/postgresql.conf
echo "shared_preload_libraries = 'timescaledb' " >> /opt/software/pgsql/pgsql_data/postgresql.conf
/opt/software/pgsql/bin/pg_ctl -D /opt/software/pgsql/pgsql_data/ start
cd /opt/software/pgsql/bin
./psql <<PG
#创建数据库,用于下方导入数据
CREATE DATABASE test;
ALTER USER postgres WITH PASSWORD 'your passd';
\q
PG
/opt/software/pgsql/bin/pg_ctl stop
#退出用户
exit;
CONSOLE
systemctl start postgresql.service
#导入数据库(自己的数据库初始文件)
pg_restore -h 127.0.0.1 -p 5432 -U postgres -w -d test -v "$docDir/psql/test.backup"
echo "#######finish postgreSql#######"
else
echo "#######postgreSql already installed#######"
fi
}
修改pg_hba.conf(仅可使用SSH连接数据库)
# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 trust
host all all 0.0.0.0/0 password
# IPv6 local connections:
host all all ::1/128 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication all trust
host replication all 127.0.0.1/32 trust
host replication all ::1/128 trust