1、準備
Postgres下載 10.19
CMake下載 3.16.2
TimescaleDB下載 1.4.0
相關(guān)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哗魂、手動安裝
將上述文件都拷貝到目標服務(wù)器
首先到RMP目錄下安裝相關(guān)依賴
rpm -Uvh --force --nodeps *.rpm
創(chuàng)建軟件安裝目錄
mkdir -pv /opt/software
postgres安裝包解壓
tar zxvf postgresql-10.19-1-linux-x64-binaries.tar.gz -C /opt/software/
創(chuàng)建數(shù)據(jù)目錄并授權(quán)
#進入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/
添加環(huán)境變量
echo 'export PATH=/opt/software/pgsql/bin:$PATH' >>/etc/profile;
echo 'export PGDATA=/opt/software/pgsql/pgsql_data'>>/etc/profile;
#生效環(huán)境變量
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录别,放開注釋朽色,分別填入填入上面創(chuàng)建的數(shù)據(jù)目錄和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
修改腳本權(quán)限,并加入開機自啟動
chmod 754 /usr/lib/systemd/system/postgresql.service
systemctl enable postgresql.service
systemctl start postgresql.service
查看服務(wù)是否啟動成功
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數(shù)據(jù)庫
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數(shù)據(jù)庫#######"
#解壓縮
tar -zxvf $pkgDir/postgresql-10.19-1-linux-x64-binaries.tar.gz -C /opt/software/
cd /opt/software/pgsql
#創(chuàng)建數(shù)據(jù)目錄并授權(quán)
mkdir -pv pgsql_data
useradd postgres
echo "your passd" | passwd postgres --stdin > /dev/null 2>&1
chown postgres /opt/software/pgsql/pgsql_data/
#切換用戶初始化數(shù)據(jù)庫
echo "#######正在初始化postgreSql#######"
#添加環(huán)境變量
echo 'export PATH=/opt/software/pgsql/bin:$PATH' >>/etc/profile;
echo 'export PGDATA=/opt/software/pgsql/pgsql_data'>>/etc/profile;
#生效環(huán)境變量
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
#安裝時序數(shù)據(jù)庫
tar -zxvf $pkgDir/timescaledb.tar.gz -C /opt/software/pgsql
cd /opt/software/pgsql/timescaledb
./bootstrap
cd build && make
make install
#設(shè)置開機自啟動(自啟動腳本參考手動安裝)
\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
#創(chuàng)建數(shù)據(jù)庫葫男,用于下方導入數(shù)據(jù)
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
#導入數(shù)據(jù)庫(自己的數(shù)據(jù)庫初始文件)
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連接數(shù)據(jù)庫)
# "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