暂无图片
暂无图片
1
暂无图片
暂无图片
暂无图片

postgresql源码离线一键安装万能脚本分享

热衷于分享各种干货知识,大家有想看或者想学的可以评论区留言,秉承着“开源知识来源于互联网,回归于互联网”的理念,分享一些日常工作中能用到或者频率比较的内容,希望大家能够喜欢,并提出宝贵地意见,我们一起提升,守住自己的饭碗。

一、环境准备

只需要挂载镜像,或者上传镜像至指定目录进行挂载即可,上传安装包至任意目录,一键进行安装,此脚本的端口和密码大家可以自定义,备份的数据库名字也可以根据需要去修改,也包含了实例数据库的创建和巡检账号的创建。

源码安装包下载地址:https://www.postgresql.org/ftp/source/

  1. [root@localhost ~]# chmod +x pginstall.sh

  2. [root@localhost ~]# cat pginstall.sh

二、过程截图



三、脚本分享

  1. #!/bin/bash

  2. echo "-----------------------------开始PG数据库安装--------------------------------------"

  3. systemctl stop firewalld

  4. systemctl disable firewalld

  5. dir=$(pwd)

  6. echo "db variable list"

  7. BASEPATH=/pgdb

  8. FILE_CONF=/pgdb/data/postgresql.conf

  9. HBA_CONF=/pgdb/data/pg_hba.conf

  10. PGDATA=/pgdb/data

  11. PGHOME=/pgdb/pgsql

  12. SCRIPTS_DIR=/pgdb/scripts

  13. LOGPATH=/pgdb/data/log

  14. PORT=5785

  15. PASSWD="123456"

  16. cpu=$(cat /proc/cpuinfo | grep 'physical id' | sort | uniq | wc -l)

  17. sed -ri '/^root/a\postgres ALL=(ALL) NOPASSWD: ALL' /etc/sudoers

  18. sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config

  19. setenforce 0

  20. systemctl daemon-reload

  21. systemctl restart systemd-logind

  22. echo "安装相关依赖"

  23. cd /etc/yum.repos.d/

  24. rm -rf ./*

  25. cat >> etc/yum.repos.d/centos.repo <<-EOF

  26. [centos]

  27. name=oracle

  28. baseurl=file:///mnt

  29. enabled=1

  30. gpgcheck=0

  31. EOF

  32. cd

  33. mount dev/sr0 mnt

  34. yum clean all|wc -l

  35. yum makecache

  36. #当然大家也可以自定义目录上传iso镜像。进行挂载,比如上传到cdrom目录下,mount dev/cdrom mnt,修改repo文件,进行挂载即可。

  37. yum install -y zlib-devel libaio cmake make gcc gcc-c++ readline readline-devel perl bison flex libyaml net-tools expect openssh-clients tcl openssl openssl-devel ncurses-devel python python-devel openldap pam systemtap-sdt-devel perl-ExtUtils-Embed libxml2 libxml2-devel libxslt libxslt-devel uuid-devel

  38. echo "postgres exits"

  39. id $postgres >& dev/null

  40. if [ $? -ne 0 ]

  41. then

  42. echo "postgres already exits"

  43. else

  44. echo "postgres not exits,please create"

  45. groupadd postgres

  46. useradd -g postgres postgres

  47. echo "$PASSWD"|passwd --stdin postgres

  48. sed -ri '/^root/a\postgres ALL=(ALL) ALL' /etc/sudoers

  49. fi


  50. echo "create directory"

  51. if [ ! -d $BASEPATH ]

  52. then

  53. mkdir -p $BASEPATH/{data,pg_archive,pg_backup,scripts,tmp}

  54. fi



  1. tar -zxf $dir/postgresql*.tar.gz -C $BASEPATH/

  2. echo "pgsql upzip success"

  3. echo "directory rights"

  4. cd $BASEPATH

  5. mv postgresql-*/ pgsql

  6. chown -R postgres:postgres $BASEPATH

  7. chmod -R 755 $BASEPATH


  8. cd $PGHOME

  9. ./configure --prefix=$PGHOME --with-pgport=$PORT --with-openssl --with-perl --with-python --with-blocksize=32 --with-readline --with-libxml --with-libxslt

  10. cd /home/postgres

  11. postgresenvConf(){

  12. conf_exist=$(cat .bash_profile |grep postgres|wc -l)

  13. if [ $conf_exist -eq 0 ]; then

  14. echo "postgres user env configuration"

  15. cp .bash_profile .bash_profile.bak

  16. sed -i 's/^export PATH/#export PATH/' .bash_profile

  17. echo "#add by postgres" >> .bash_profile

  18. echo "export PGHOME=$PGHOME" >> .bash_profile

  19. echo "export PGDATA=$PGDATA" >> .bash_profile

  20. echo "export PGPORT=5785" >> .bash_profile

  21. echo "export PGPASSWORD=123456" >> .bash_profile

  22. echo 'export PATH=$PGHOME/bin:$PATH' >> .bash_profile

  23. echo 'export MANPATH=$PGHOME/share/man:$MANPATH' >> .bash_profile

  24. echo 'export LD_LIBRARY_PATH=$PGHOME/lib:$LD_LIBRARY_PATH' >> .bash_profile

  25. echo 'SCRIPTS_DIR=/pgdb/scripts' >> .bash_profile

  26. echo "export LANG="en_US.UTF-8"" >> .bash_profile

  27. echo 'export DATE=`date +"%Y%m%d%H%M"`' >> .bash_profile

  28. source /home/postgres/.bash_profile

  29. else

  30. echo "postgres user env is already config, so we do nothing"

  31. fi

  32. }

  33. postgresenvConf

  34. su - postgres -c 'echo "$PASSWD">> .pgpass'

  35. su - postgres -c "chmod 0600 /home/postgres/.pgpass"

  36. su - postgres -c "$PGHOME/bin/initdb --username=postgres --pwfile=/home/postgres/.pgpass -D $PGDATA --encoding=UTF8 --lc-collate=en_US.UTF-8 --lc-ctype=en_US.UTF-8"

  37. if [ $? == 0 ]

  38. then

  39. echo "初始化成功"

  40. chown -R postgres:postgres $BASEPATH

  41. chmod -R 755 $BASEPATH

  42. chmod -R 700 $PGDATA

  43. else

  44. echo "初始化失败"

  45. fi

  46. echo "configure param"

  47. cp $FILE_CONF $PGDATA/postgresql.confbak

  48. sed -i "/^#listen_addresses = 'localhost'/s/#listen_addresses = 'localhost'/listen_addresses = '*'/" $FILE_CONF

  49. sed -i "s/^#port = 5785/port = $PORT/" $FILE_CONF

  50. sed -i 's/max_connections = 100/max_connections = 1000/' $FILE_CONF

  51. sed -i 's/^#superuser_reserved_connections = 3/superuser_reserved_connections=10/' $FILE_CONF

  52. sed -i "/^#max_prepared_transactions = 0/s/#max_prepared_transactions = 0/max_prepared_transactions = 500/" $FILE_CONF

  53. sed -i "/^shared_buffers = 128MB/s/shared_buffers = 128MB/shared_buffers = 1024MB/" $FILE_CONF

  54. sed -i "/^#effective_cache_size = 4GB/s/#effective_cache_size = 4GB/effective_cache_size = 3GB/" $FILE_CONF

  55. sed -i "/^#work_mem = 4MB/s/^#work_mem = 4MB/work_mem = 30MB/" $FILE_CONF

  56. sed -i "/^#maintenance_work_mem = 64MB/s/#maintenance_work_mem = 64MB/maintenance_work_mem = 256MB/" $FILE_CONF # min( 8G, (主机内存*1/8)/max_parallel_maintenance_workers )

  57. sed -i 's/^#vacuum_cost_limit = 200/vacuum_cost_limit = 500/' $FILE_CONF

  58. sed -i "/^#max_parallel_maintenance_workers = 2/s/#max_parallel_maintenance_workers = 2/max_parallel_maintenance_workers = 4/" $FILE_CONF

  59. sed -i "/^#max_parallel_workers_per_gather = 2/s/#max_parallel_workers_per_gather = 2/max_parallel_workers_per_gather = 4/" $FILE_CONF

  60. sed -i "/^#max_parallel_workers = 8/s/^#//" $FILE_CONF

  61. sed -i "/^#max_worker_processes = 8/s/^#//" $FILE_CONF

  62. sed -i 's/^min_wal_size = 80MB/min_wal_size = 1GB/' $FILE_CONF

  63. sed -i 's/^max_wal_size = 1GB/max_wal_size = 2GB/' $FILE_CONF

  64. sed -i 's/^#checkpoint_timeout = 5min/checkpoint_timeout = 10min/' $FILE_CONF

  65. sed -i "/^#checkpoint_completion_target = 0.9/s/^#//" $FILE_CONF

  66. sed -i "/^#wal_level/s/^#//" $FILE_CONF

  67. sed -i 's/#archive_mode = off/archive_mode = on/' $FILE_CONF

  68. sed -i "/^#archive_command = ''/s/#archive_command = ''/archive_command ='\/usr\/bin\/lz4 -q -z %p \/pgdb\/pg_archive\/%f.lz4'/" $FILE_CONF

  69. sed -i "/^#log_destination = 'stderr'/s/#log_destination = 'stderr'/log_destination = 'csvlog'/" $FILE_CONF

  70. sed -i "/^#logging_collector = off/s/#logging_collector = off/logging_collector = on/" $FILE_CONF

  71. sed -i "/^#log_disconnections = off/s/#log_disconnections = off/log_disconnections = on/" $FILE_CONF

  72. sed -i "/^#log_connections = off/s/#log_connections = off/log_connections = on/" $FILE_CONF

  73. sed -i "/^#authentication_timeout = 1min/s/#authentication_timeout = 1min/authentication_timeout = 59s/" $FILE_CONF

  74. sed -i "/^#log_directory = 'log'/s/^#//" $FILE_CONF

  75. sed -i "/^#log_filename/s/^#//" $FILE_CONF

  76. sed -i "/^#log_file_mode/s/^#//" $FILE_CONF

  77. sed -i "/^#log_rotation_age/s/^#//" $FILE_CONF

  78. sed -i "/^#log_rotation_size/s/^#//" $FILE_CONF

  79. sed -i "/^#temp_buffers = 8MB/s/#temp_buffers = 8MB/temp_buffers = 256MB/" $FILE_CONF

  80. cp $HBA_CONF $PGDATA/pg_hba.confbak

  81. echo "host all all 0.0.0.0/0 md5" >> $HBA_CONF

  82. echo "8. auto starting up"

  83. cat > /usr/lib/systemd/system/postgres.service << "EOF"

  84. [Unit]

  85. Description=PostgreSQL database server

  86. After=network.target

  87. [Service]

  88. Type=forking

  89. User=postgres

  90. Group=postgres

  91. Environment=PGPORT=5785

  92. Environment=PGDATA=/pgdb/data

  93. OOMScoreAdjust=-1000

  94. ExecStart=/pgdb/pgsql/bin/pg_ctl start -D $PGDATA

  95. ExecStop=/pgdb/pgsql/bin/pg_ctl stop -D $PGDATA -s -m fast

  96. ExecReload=/pgdb/pgsql/bin/pg_ctl reload -D $PGDATA -s

  97. TimeoutSec=300

  98. [Install]

  99. WantedBy=multi-user.target

  100. EOF

  101. sed -i "s/^Environment=PGPORT=5785/Environment=PGPORT=$PORT/" /usr/lib/systemd/system/postgres.service

  102. chmod +x /usr/lib/systemd/system/postgres.service

  103. systemctl daemon-reload

  104. systemctl start postgres.service

  105. systemctl enable postgres.service

  106. #判断是否启动成功

  107. process=$(ps -ef | grep -v 'grep'| grep '$PGHOME/bin/postgres'|awk '{print $2}')

  108. if [ -n "$process" ];then #检测字符串长度是否不为 0,不为 0 返回 true。

  109. echo "install success ans start success"

  110. else

  111. echo "install fail"

  112. fi

  113. echo "-----------------------------恭喜完成安装--------------------------------------"

  114. echo "---------------------------切换归档日志------------------------------------------------------"

  115. su - postgres -c "$PGHOME/bin/psql -d postgres -h127.0.0.1 -p$PORT -c \"select pg_switch_wal();\""

  116. echo "---------------------------------------------------------------------------------------"

  117. echo "---------------------------添加备份任务------------------------------------------------------"

  118. cat > $SCRIPTS_DIR/pg_backup.sh << "EOF"

  119. #!/bin/bash

  120. echo "logical backup"

  121. PG_HOME=/pgdb

  122. PG_HOST="127.0.0.1"

  123. PG_PORT="5785"

  124. PG_USER="postgres"

  125. PG_PASSWD="123456"

  126. DATE="`date +%Y%m%d`"

  127. DIR_BACKUP="${PG_HOME}/pg_backup"

  128. DIR_LOG="${DIR_BACKUP}/logs"

  129. FILE_LOG="${DIR_LOG}/db_backup.INFO.`date +%F`.log"

  130. DAY=7

  131. DAY_LOG="`expr ${DAY} + 7`"

  132. DATABASES=("postgres" "test")

  133. test -d ${DIR_LOG} || mkdir -p ${DIR_LOG}

  134. test -d ${DIR_BACKUP}/${PG_USER}-${DATE} || mkdir -p ${DIR_BACKUP}/${PG_USER}-${DATE}

  135. # ------------------- Start -------------------

  136. echo -e "\n----------------- $(date +%F\ %T) Start -----------------"

  137. echo -e "\n================= $(date +%F\ %T) Start =================" >> ${FILE_LOG}

  138. for database in "${DATABASES[@]}"; do

  139. echo "---------- Current backup database: [ ${database} ] ----------"

  140. echo "----------- Backed-up database: [ ${database} ] -----------" >> ${FILE_LOG}

  141. ${PG_HOME}/pgsql/bin/pg_dump -h ${PG_HOST} -p ${PG_PORT} -U ${PG_USER} -w -Fc -d ${database} -b -f ${DIR_BACKUP}/${PG_USER}-${DATE}/db_${database}_${DATE}.dmp

  142. done

  143. cd ${DIR_BACKUP}

  144. tar -czf ${PG_USER}-${DATE}.tar.gz ${PG_USER}-${DATE}/

  145. echo "---------- Backup file created: [ ${PG_USER}-${DATE}.tar.gz ]"

  146. echo "Backup file created: ${DIR_BACKUP}/${PG_USER}-${DATE}.tar.gz" >> ${FILE_LOG}


  1. rm -f ${DIR_BACKUP}/${PG_USER}-${DATE}/*

  2. rmdir ${DIR_BACKUP}/${PG_USER}-${DATE}/

  3. # ---------------------------------------------------------------------------------

  4. echo "--------------------- Deleted old files ---------------------" >> ${FILE_LOG}

  5. echo "`find ${DIR_BACKUP} -type f -mtime +${DAY} -iname ${PG_USER}-\*.gz`" >> ${FILE_LOG}

  6. echo "`find ${DIR_LOG} -type f -mtime +${DAY_LOG} -iname db_backup.INFO.\*.log`" >> ${FILE_LOG}

  7. find ${DIR_BACKUP} -type f -mtime +${DAY} -iname ${PG_USER}-\*.gz -exec rm -f {} \;

  8. find ${DIR_LOG} -type f -mtime +${DAY_LOG} -iname db_backup.INFO.\*.log -exec rm -f {} \;

  9. echo -e "------------------ $(date +%F\ %T) End ------------------\n"

  10. echo -e "================== $(date +%F\ %T) End ==================\n" >> ${FILE_LOG}

  11. EOF

  12. echo "数据库逻辑备份:每日凌晨30分进行逻辑备份,保留7天备份文件"

  13. echo "11.configure crontab"

  14. if [[ -e /var/spool/cron/postgres ]]; then

  15. cp /var/spool/cron/postgres /var/spool/cron/postgresbak

  16. else

  17. touch /var/spool/cron/postgres

  18. fi

  19. chown -R postgres:postgres $SCRIPTS_DIR

  20. chmod +x $SCRIPTS_DIR/*.sh

  21. cat >> /var/spool/cron/postgres << "EOF"

  22. # PostgresBegin

  23. 30 00 * * * /pgdb/scripts/pg_backup.sh > /dev/null 2>&1

  24. 10 00 * * * find /pgdb/data/pg_archive -type f -name "0000000*" -mtime +5 -exec rm {} \; > /pgdb/data/pg_archive/del_pgarchive_`date +%F`.log 2>&1

  25. #00 01 * * * find /pgdb/data/pg_wal -type f -name "0000000*" -mtime +5 -exec rm {} \; > /pgdb/data/pg_wal/clean_pgwal_`date +%F`.log 2>&1

  26. 00 01 * * * find /pgdb/data/log -type f -name "postgresql*.log" -mtime +90 -exec rm {} \; > /pgdb/data/log/clean_log_`date +%F`.log 2>&1

  27. 00 01 * * * find /pgdb/data/log -type f -name "postgresql*.csv" -mtime +90 -exec rm {} \; > /pgdb/data/log/clean_csv_`date +%F`.log 2>&1

  28. EOF

  29. echo "--------------创建只读巡检用户-------------------"

  30. su - postgres -c "$PGHOME/bin/psql -d postgres -h127.0.0.1 -p5785 -c \"create user zyjc_read with encrypted password 'postgres';\""

  31. su - postgres -c "$PGHOME/bin/psql -d postgres -h127.0.0.1 -p5785 -c \"alter user zyjc_read set default_transaction_read_only=on;\""

  32. su - postgres -c "$PGHOME/bin/psql -d postgres -h127.0.0.1 -p5785 -c \"grant select on all tables in schema public to zyjc_read;\""

  33. su - postgres -c "$PGHOME/bin/psql -d postgres -h127.0.0.1 -p5785 -c \"alter default privileges in schema public grant select on tables to zyjc_read;\""

说明:本来想做个离线的rpm包,但因相互依赖的包太多,所以选择上传镜像作为rpm源。



文中的概念来源于互联网,如有侵权,请联系我删除。

欢迎关注公众号:小周的数据库进阶之路,一起交流数据库、中间件和云计算等技术。如果觉得读完本文有收获,可以转发给其他朋友,大家一起学习进步!感兴趣的朋友可以加我微信,拉您进群与业界的大佬们一起交流学习。



文章转载自小周的数据库进阶之路,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论