由于在一些环境下无法配置外网,导致安装组件的时候非常的麻烦,存在各种依赖或者包的版本问题。为了简化这种重复性的动作,一键安装 PostgreSQL 成为了一种需要。
下面是一键安装 PostgreSQL 数据库的脚本:
[root@localhost postgresql-12]# cat postgresql_install.sh
#!/bin/bash
# -------------------------------------------------------------------------------
# ScriptName: postgresql_install.sh
# Author: cloud.com 云端
# Description: 一键部署 PostgreSQL
# Version: v1.0
# Date: 2023-06-01
# -------------------------------------------------------------------------------
echo "
┌──────────────────────────────────────────────────────────────────────┐
│ │
│ ∙ Linux 环境下一键部署 PostgreSQL v1.0 ∙ │
│ (One-click postgresql installation based on Linux environment) │
│ │
│ ∙ Description : 基于 Linux 环境实现 PostgreSQL 一键式部署 │
│ ∙ Author : cloud.com 云端 │
│ ∙ E-Mail : 2429425191@qq.com │
│ ∙ Version : v1.0 │
│ │
└──────────────────────────────────────────────────────────────────────┘
"
# 关闭系统防火墙和安全机制
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
systemctl stop firewalld
systemctl disable firewalld
# 关闭邮件服务以及 NetworkManager
systemctl stop postfix.service
systemctl disable postfix.service
systemctl stop NetworkManager
systemctl disable NetworkManager
# 修改文件句柄数
cat > /etc/security/limits.conf << EOF
root hard nproc 65535
root soft nproc 65535
* hard nproc 65535
* soft nproc 65535
root hard nofile 65535
root soft nofile 65535
* hard nofile 65535
* soft nofile 65535
EOF
sed -i 's/* soft nproc 4096/* soft nproc 65535/' /etc/security/limits.d/20-nproc.conf
# 修改环境变量提示符
cat > /etc/profile << EOF
export PS1='[\u@\h \W]\$ '
EOF
source /etc/profile
# 删除系统自带的 postgresql
rpm -qa | grep postgres
rpm -e postgresql-9.2.24-8.el7_9.x86_64 --nodeps
rpm -e postgresql-server-9.2.24-8.el7_9.x86_64 --nodeps
rpm -e postgresql-libs-9.2.24-8.el7_9.x86_64 --nodeps
rpm -qa | grep postgresql | xargs rpm -e --nodeps
# 安装 postgresql
userdel postgres
groupdel postgres
groupadd postgres
useradd -d /home/postgres -g postgres postgres
cd /usr/local/src/postgresql-12/
chmod 755 /usr/local/src/postgresql-12/postgresql.conf
chmod 755 /usr/local/src/postgresql-12/pg_hba.conf
yum localinstall -y *
cat > /etc/profile.d/setenv-postgres.sh << EOF
export PGHOME=/home/postgres
export PGDATA=/home/postgres/pgsql-12/data
export PGLIBRARY=/usr/pgsql-12/lib
export PATH=\$PATH:/usr/pgsql-12/bin
EOF
source /etc/profile
su - postgres -c "/usr/pgsql-12/bin/initdb -D /home/postgres/pgsql-12/data"
mkdir -p /home/postgres/pgsql-12/data/archive
mkdir -p /home/postgres/pgsql-12/data/log
cp /usr/local/src/postgresql-12/postgresql.conf /home/postgres/pgsql-12/data/
cp /usr/local/src/postgresql-12/pg_hba.conf /home/postgres/pgsql-12/data/
chown -R postgres:postgres /home/postgres
su - postgres -c "/usr/pgsql-12/bin/pg_ctl -D /home/postgres/pgsql-12/data -l logfile start"
su - postgres -c "/usr/pgsql-12/bin/psql -h 127.0.0.1 -p 5432 -U postgres -d postgres"
/usr/pgsql-12/bin/psql -h 127.0.0.1 -p 5432 -U postgres -d postgres -c "alter user postgres with password 'Cnhis.com@2023';"
这个是另外的一种可以安装自己的需求简单修改一下即可
[root@localhost postgresql-14]# cat install_postgresql.sh
#!/bin/bash
# *******************************************
# Author: peter
# FileName: install_postgresql.sh
# Date: 2023-08-11
# Description: Install PostgreSQL
# *******************************************
postgres_user=postgres
postgres_password=centos@2023
pgsql_install_dir=/home/postgres/pgsql-14
pgsql_data=/home/postgres/pgsql-14/data
echo "Create user and groups for postgres Database service"
groupadd $postgres_user
useradd -g $postgres_user $postgres_user -d /home/postgres
mkdir -p $pgsql_install_dir $pgsql_data
chown -R $postgres_user:$postgres_user $pgsql_install_dir
echo "Dependent installation"
yum install -y gcc gcc-c++ net-tools sysstat make cmake readline readline-devel zlib zlib-devel lz4-devel openssl openssl-devel libxml2 libxml2-devel pam pam-devel ncurses ncurses-devel perl-ExtUtils-Embed libxslt libxslt-devel perl perl-devel flex flex-devel uuid uuid-devel
echo "Install pgsql"
tar -zxvf /usr/local/src/postgresql-14.7.tar.gz
cd /usr/local/src/postgresql-14.7
./configure --prefix=$pgsql_install_dir && make && make install
echo "pgsql environment variable"
su - postgres <<EOF
echo 'export PGHOME=/home/postgres/pgsql-14' >> ~/.bash_profile
echo 'export PGDATA=/home/postgres/pgsql-14/data' >> ~/.bash_profile
echo 'export PATH=$PATH:/home/postgres/pgsql-14/bin' >> ~/.bash_profile
echo 'export MANPATH=/home/postgres/pgsql-14/share/man' >> ~/.bash_profile
echo 'export LD_LIBRARY_PATH=/home/postgres/pgsql-14/lib' >> ~/.bash_profile
echo 'export LANG=en_US.UTF8' >> ~/.bash_profile
source ~/.bash_profile
/home/postgres/pgsql-14/bin/initdb -D /home/postgres/pgsql-14/data
cd /home/postgres/pgsql-14/data
/home/postgres/pgsql-14/bin/pg_ctl -D /home/postgres/pgsql-14/data -l logfile start
/home/postgres/pgsql-14/bin/psql -c "ALTER USER postgres WITH PASSWORD 'centos@2023'"
EOF
echo "postgresql.service add system"
cat >>/etc/systemd/system/postgresql.service<<EOF
[Unit]
Description=PostgresQL database server
After=network.target
[service]
Type=forking
User=postgres
Environment=PGHOME=/home/postgres/pgsql-14
Environment=PGDATA=/home/postgres/pgsql-14/data
ExecStart=/home/postgres/pgsql-14/bin/pg_ctl start -D /home/postgres/pgsql-14/data
ExecStop=/home/postgres/pgsql-14/bin/pg_ctl stop -D /home/postgres/pgsql-14/data
ExecReload=/home/postgres/pgsql-14/bin/pg_ctl reload -D /home/postgres/pgsql-14/data
Restart=always
TimeoutSec=0
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload && systemctl enable postgresql