创建一个postgres目录
mkdir - p postgres
cd postgres
下载二进制文件
wget https://get.enterprisedb.com/postgresql/postgresql-9.5.24-1-linux-x64-binaries.tar.gz
mv postgresql-9.5.24-1-linux-x64-binaries.tar.gz postgresql-9.5.24.tar.gz
cat stup.sh
#!/bin/bash
DATADIR=/data/pgdata
workdir=$(cd $(dirname $0); pwd)
#判断是否有postgre版本的安装包
cd /usr/local
if [ -d pgsql ];then
rm -rf pgsql
echo "安装包删除成功"
fi
#安装包解压到指定目录
cd $workdir
tar -zxf postgresql-9.5.24.tar.gz -C /usr/local/
echo "pgsql文件解压成功"
#判断用户是否存在
id $postgres >& /dev/null
echo "用户postgres已存在"
if [ $? -ne 0 ]
then
echo "用户不存在,开始创建postgres用户"
groupadd postgres
useradd -g postgres postgres
fi
#安装pgsql
if [ -d $DATADIR ];then
echo "数据目录存在"
else
mkdir -p $DATADIR
chown -R postgres:postgres $DATADIR
echo "给pgsql创建data目录"
fi
echo "修改用户组"
chown -R postgres:postgres /usr/local/pgsql
echo "添加环境变量"
if [ -f /etc/profile ] ;then
cp -r /etc/profile /etc/profile.bak
echo "export PGHOME=/usr/local/pgsql" >> /etc/profile
echo "export PGDATA=$DATADIR" >> /etc/profile
echo "export PATH=$PATH:$PGHOME/bin" >> /etc/profile
source /etc/profile
fi
echo "切换至postgres用户来初始化数据库"
su - postgres -c "/usr/local/pgsql/bin/initdb -D $DATADIR"
#添加系统systemd服务启动
cat >> /etc/systemd/system/postgresql.service <<EOF
[Unit]
Description=PostgreSQL database server
After=network.target
[Service]
Type=forking
User=postgres
Group=postgres
#Environment=PGDATA=/data/pgdata
#OOMScoreAdjust=-1000
ExecStart=/usr/local/pgsql/bin/pg_ctl -D /data/pgdata start
ExecStop=/usr/local/pgsql/bin/pg_ctl stop -D /data/pgdata
ExecReload=/usr/local/pgsql/bin/pg_ctl reload -D /data/pgdata
TimeoutSec=300
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl start postgresql.service
systemctl enable postgresql.service
#修改数据库配置
sed -i -e"s/^#listen_addresses =.*$/listen_addresses = '*'/" $DATADIR/postgresql.conf
sed -i -e"s/^max_connections = 100.*$/max_connections = 1000/" $DATADIR/postgresql.conf
sed -i -e"s/^shared_buffers =.*$/shared_buffers = 2GB/" $DATADIR/postgresql.conf
sed -i -e"s/^#effective_cache_size = 128MB.*$/effective_cache_size = 4GB/" $DATADIR/postgresql.conf
sed -i -e"s/^#work_mem = 1MB.*$/work_mem = 128MB/" $DATADIR/postgresql.conf
echo "host all all 0.0.0.0/0 md5" >> $DATADIR/pg_hba.conf
#重启数据库
systemctl restart postgresql.service