Linux Centos 7 脚本全自动安装部署PostgreSQL

文章目录

简介

部署环境

操作系统:Linux Centos 7.4 x64
运行内存:4G测试环境用户的运行内存比较小
CPU:8核核心多安装速度快
数据库:PostgreSQL
数据库版本:12.5
需要的依赖包:
  1.GCC用于数据库安装编译
    1.libgomp-4.8.5-44.el7.x86_64.rpm
    2.cpp-4.8.5-44.el7.x86_64.rpm
    3.glibc-common-2.17-317.el7.x86_64.rpm
    4.libgcc-4.8.5-44.el7.x86_64.rpm
    5.glibc-2.17-317.el7.x86_64.rpm
    6.glibc-devel-2.17-317.el7.x86_64.rpm
    7.glibc-headers-2.17-317.el7.x86_64.rpm
    8.gcc-4.8.5-44.el7.x86_64.rpm
    9.kernel-headers-3.10.0-1160.el7.x86_64.rpm
    10.kernel-3.10.0-1160.el7.x86_64.rpm
    11.libmpc-1.0.1-3.el7.x86_64.rpm
    12.mpfr-3.1.1-4.el7.x86_64.rpm
  2.lsof用于查询运行中的服务端口,运维时使用
    1.lsof-4.87-6.el7.x86_64.rpm
  3.readline安装编译依赖包
    1.ncurses-base-5.9-14.20130511.el7_4.noarch.rpm
    2.ncurses-5.9-14.20130511.el7_4.x86_64.rpm
    3.ncurses-libs-5.9-14.20130511.el7_4.x86_64.rpm
    4.ncurses-devel-5.9-14.20130511.el7_4.x86_64.rpm
    5.readline-devel-6.2-11.el7.x86_64.rpm
  4.telnet用于服务端口测试
    1.telnet-0.17-64.el7.x86_64.rpm
  5.zlib安装编译依赖包-有18版本和19版本,取其一即可,建议用19版本
    version 18
      1.zlib-1.2.7-18.el7.x86_64.rpm
      2.zlib-devel-1.2.7-18.el7.x86_64.rpm
    version 19
      1.zlib-1.2.7-19.el7_9.x86_64.rpm
      2.zlib-devel-1.2.7-19.el7_9.x86_64.rpm
运行文件:run.sh

以上文件需要到centos官网下载

如果需要下载已经整理好的文件,请点此跳转至下载中心


脚本内容

新增了开机自启脚本

#!/bin/bash
#当前目录的绝对路径
curr_Dir=$(cd `dirname $0` ; pwd)
echo 当前所在目录:$curr_Dir
#服务端口
PORT=5432
echoSuffix="==》:"
userdel -r postgres
echo$((a=$a+1))$echoSuffix创建用户
adduser postgres
#新创建的用户并不能使用sudo命令,需要给他添加授权。
echo$((a=$a+1))$echoSuffix添加sudoers文件可写权限
chmod -v u+w /etc/sudoers

echo$((a=$a+1))$echoSuffix添加用户权限
echo "postgres    ALL=(ALL)    NOPASSWD:ALL" >> /etc/sudoers

echo$((a=$a+1))$echoSuffix初始化目录
mkdir /home/postgres/disk_d
mkdir /home/postgres/disk_d/applications
mkdir /home/postgres/disk_d/applications/jdk_11
mkdir /home/postgres/disk_d/applications/service/
mkdir /home/postgres/disk_d/applications/service/apps
mkdir /home/postgres/disk_d/applications/service/nginx
mkdir /home/postgres/disk_d/applications/service/postgresql
mkdir /home/postgres/disk_d/applications/service/postgresql/data
mkdir /home/postgres/disk_d/applications/service/tomcats
mkdir /home/postgres/disk_d/data
mkdir /home/postgres/disk_d/datasource
mkdir /home/postgres/disk_d/installpackage

echo$((a=$a+1))$echoSuffix将windows下文件上传到了linux服务器,导致有文件的执行权限丢失。
chmod 777 ./gcc_Install/*.*
chmod 777 ./readline-devel_Install/*.*
chmod 777 ./zlib_Install/version-19/*.*
chmod 777 ./telnet/*.*
chmod 777 ./lsof/*.*

echo$((a=$a+1))$echoSuffix安装GCC依赖包
rpm -Uvh ./gcc_Install/*.rpm --nodeps --force
echo$((a=$a+1))$echoSuffix安装readline依赖包
rpm -Uvh ./readline-devel_Install/*.rpm --nodeps --force
echo$((a=$a+1))$echoSuffix安装zlib依赖包
rpm -Uvh ./zlib_Install/version-19/*.rpm --nodeps --force
echo$((a=$a+1))$echoSuffix安装telnet依赖包
rpm -Uvh ./telnet/*.rpm --nodeps --force
echo$((a=$a+1))$echoSuffix安装lsof端口查询依赖包
rpm -Uvh ./lsof/*.rpm --nodeps --force

#临时变量
PGHOME=/home/postgres/disk_d/applications/service/postgresql
PGDATA=$PGHOME/data
echo$((a=$a+1))$echoSuffix将用户环境变量写入/home/postgres/bin目录下的.bashrc文件
echo PGHOME=$PGHOME >> /home/postgres/.bashrc
echo export PGHOME >> /home/postgres/.bashrc
echo PGDATA=$PGDATA >> /home/postgres/.bashrc
echo export PGDATA >> /home/postgres/.bashrc
echo PATH=$PATH:$HOME/.local/bin:$HOME/bin:$PGHOME >> /home/postgres/.bashrc
echo export PATH >> /home/postgres/.bashrc

echo$((a=$a+1))$echoSuffix为用户授权目录下所有子目录权限
chown -R postgres /home/postgres/disk_d/applications/service/postgresql
chown -R postgres /home/postgres/disk_d/data

echo$((a=$a+1))$echoSuffix删除已解压的数据库安装包,重新解压缩数据库文件
rm -rf ./postgresql-12.5

echo$((a=$a+1))$echoSuffix解压数据库安装包
tar -xf postgresql-*.tar.gz

echo$((a=$a+1))$echoSuffix进入已解压的数据库目录
cd postgresql-12.5

echo$((a=$a+1))$echoSuffix配置初始化
#Shell 默认提供了获取命令执行输出的方法,即用 ` 号将需要获取输出的操作括起来,并赋值给一个变量,则 Shell 会在等待命令执行完成后把输出内容用于赋值,所以,这就是实现了我们需要的等待效果,并且时间精准性很高。
configureOut=`$curr_Dir/postgresql-12.5/configure --prefix /home/postgres/disk_d/applications/service/postgresql`
echo $configureOut

echo$((a=$a+1))$echoSuffix执行安装编译
configureOut=`make clean; make`
echo $configureOut

echo$((a=$a+1))$echoSuffix执行安装
configureOut=`make install`
echo $configureOut
configureOut=""

echo$((a=$a+1))$echoSuffix使用指定的新增数据库用户,重新启用配置文件 开始
su - postgres <<EOF
source /home/postgres/.bashrc
echo $echoSuffix数据库用户环境变量已初始化完成
echo 第$((a=$a+1))$echoSuffix初始化数据库
$PGHOME/bin/initdb
echo $echoSuffix数据库初始化完成

echo 第$((a=$a+1))$echoSuffix使用sed命令修改配置文件
echo $echoSuffix将【host    all     all   127.0.0.1/32 trust 】替换为【host    all        all         0.0.0.0/0      trust 】
sed -i "s/host    all             all             127.0.0.1\/32            trust/host    all             all             0.0.0.0\/0            trust/g" $PGDATA/pg_hba.conf
#将前面注释符“#”取消
sed -i "s/#listen_addresses = /listen_addresses = /g" $PGDATA/postgresql.conf
#替换掉localhost,将地址改为*
sed -i "s/listen_addresses = 'localhost'/listen_addresses = '*' /g" $PGDATA/postgresql.conf
#启用端口
sed -i "s/#port = 5432/port = $PORT/g" $PGDATA/postgresql.conf
#修改连接数
sed -i "s/max_connections = 100	/max_connections = 1000	/g" $PGDATA/postgresql.conf
echo $echoSuffix配置文件初始化完成
echo 第$((a=$a+1))$echoSuffix启动数据库
$PGHOME/bin/pg_ctl start
echo $echoSuffix数据库已启动
echo $echoSuffix创建表空间【hospitaltable】
$PGHOME/bin/psql -h127.0.0.1 -p$PORT -U postgres <<tableSpaceCreate
	create tablespace pgtbl_test location '/home/postgres/disk_d/applications/service/postgresql/data/';
tableSpaceCreate
echo $echoSuffix表空间已创建【hospitaltable】

echo $echoSuffix设置数据库密码
$PGHOME/bin/psql -h127.0.0.1 -p$PORT -U postgres <<PWDSETSQL
	alter role postgres with password 'p@ssw0rd';
PWDSETSQL
echo $echoSuffix数据库密码已设置【p@ssw0rd】

echo 第$((a=$a+1))$echoSuffix将【host    all        all         0.0.0.0/0      trust 】替换为【host    all        all         0.0.0.0/0      md5 】
sed -i "s/host    all             all             0.0.0.0\/0            trust/host    all             all             0.0.0.0\/0            md5/g" $PGDATA/pg_hba.conf

echo 第$((a=$a+1))$echoSuffix重启数据库
$PGHOME/bin/pg_ctl restart
EOF
echo $echoSuffix使用指定的新增数据库用户,重新启用配置文件 结束

echo$((a=$a+1))$echoSuffix使用telnet测试是否已启动
telnet localhost $PORT
#按服务端口检索
lsof -i:$PORT

echo$((a=$a+1))$echoSuffix开放端口$PORT
firewall-cmd --add-port=$PORT/tcp --permanent
echo$((a=$a+1))$echoSuffix重启防火墙
firewall-cmd --reload

echo$((a=$a+1))步设置数据库开机自启
#拷贝linux文件,并更名为postgresql
cp $curr_Dir/postgresql-12.5/contrib/start-scripts/linux /etc/init.d/postgresql
echo$((a=$a+1))步配置开机自启参数,设置数据库主目录和数据目录
sed -i "s/\/usr\/local\/pgsql/\/home\/postgres\/disk_d\/applications\/service\/postgresql/g" /etc/init.d/postgresql
sed -i "s/\/usr\/local\/pgsql\/data/\/home\/postgres\/disk_d\/applications\/service\/postgresql\/data/g" /etc/init.d/postgresql
echo$((a=$a+1))步给postgresql赋予权限
chmod a+x /etc/init.d/postgresql
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论
要在 CentOS 7 上离线安装 PostgreSQL,您需要完成以下步骤: 1. 从 PostgreSQL 官方网站下载适用于您的 CentOS 版本的 PostgreSQL 安装程序和依赖项。 2. 将下载的文件传输到 CentOS 7 服务器。 3. 解压 PostgreSQL 安装程序和依赖项。 4. 安装依赖项,以确保 PostgreSQL 能够在 CentOS 7 上运行。您可以使用以下命令安装这些依赖项: ``` yum install -y readline-devel zlib-devel openssl-devel libxml2-devel libxslt-devel ``` 5. 运行 PostgreSQL 安装程序。您可以使用以下命令运行安装程序: ``` rpm -ivh postgresql-<version>.rpm ``` 其中 `<version>` 是您下载的 PostgreSQL 版本号。 6. 启动 PostgreSQL 服务。您可以使用以下命令启动服务并设置其在系统启动时自动启动: ``` systemctl enable postgresql systemctl start postgresql ``` 7. 确认 PostgreSQL 是否已正确安装并正在运行: ``` systemctl status postgresql ``` 如果一切正常,您应该看到类似于以下内容的输出: ``` Loaded: loaded (/usr/lib/systemd/system/postgresql.service; enabled; vendor preset: disabled) Active: active (running) since Wed 2018-06-20 12:19:44 IST; 6s ago Main PID: 10369 (postgres) CGroup: /system.slice/postgresql.service ├─10369 /usr/bin/postgres -D /var/lib/pgsql/data -p 5432 ├─10371 postgres: logger process ├─10373 postgres: checkpointer process ├─10374 postgres: writer process ├─10375 postgres: wal writer process ├─10376 postgres: autovacuum launcher process └─10377 postgres: stats collector process ``` 恭喜您,现在您已在 CentOS 7 上成功离线安装 PostgreSQL

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

TorZhu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值