1
0

postgresql.sh 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #!/bin/bash
  2. # Author: yeho <lj2007331 AT gmail.com>
  3. # BLOG: https://linuxeye.com
  4. #
  5. # Notes: OneinStack for CentOS/RedHat 7+ Debian 9+ and Ubuntu 16+
  6. #
  7. # Project home page:
  8. # https://oneinstack.com
  9. # https://github.com/oneinstack/oneinstack
  10. Install_PostgreSQL() {
  11. pushd ${oneinstack_dir}/src > /dev/null
  12. # 首先安装必要的依赖包
  13. if [ "${PM}" == 'yum' ]; then
  14. yum -y install flex bison readline-devel zlib-devel openssl-devel
  15. elif [ "${PM}" == 'apt-get' ]; then
  16. apt-get -y install flex bison libreadline-dev zlib1g-dev libssl-dev
  17. fi
  18. id -u postgres >/dev/null 2>&1
  19. [ $? -ne 0 ] && useradd -d ${pgsql_install_dir} -s /bin/bash postgres
  20. mkdir -p ${pgsql_data_dir}
  21. chown -R postgres:postgres ${pgsql_data_dir}
  22. tar xzf postgresql-${pgsql_ver}.tar.gz
  23. pushd postgresql-${pgsql_ver}
  24. # 配置和编译
  25. ./configure --prefix=${pgsql_install_dir} \
  26. --with-openssl \
  27. --with-libxml \
  28. --with-libxslt \
  29. --with-icu
  30. if [ $? -ne 0 ]; then
  31. echo "${CFAILURE}PostgreSQL configure failed! ${CEND}"
  32. kill -9 $$; exit 1;
  33. fi
  34. make -j ${THREAD}
  35. if [ $? -ne 0 ]; then
  36. echo "${CFAILURE}PostgreSQL make failed! ${CEND}"
  37. kill -9 $$; exit 1;
  38. fi
  39. make install
  40. if [ $? -ne 0 ]; then
  41. echo "${CFAILURE}PostgreSQL make install failed! ${CEND}"
  42. kill -9 $$; exit 1;
  43. fi
  44. # 设置权限
  45. chmod 755 ${pgsql_install_dir}
  46. chown -R postgres:postgres ${pgsql_install_dir}
  47. # 复制并修改服务文件
  48. /bin/cp ${oneinstack_dir}/init.d/postgresql.service /lib/systemd/system/
  49. sed -i "s@=/usr/local/pgsql@=${pgsql_install_dir}@g" /lib/systemd/system/postgresql.service
  50. sed -i "s@PGDATA=.*@PGDATA=${pgsql_data_dir}@" /lib/systemd/system/postgresql.service
  51. # 启用服务
  52. systemctl enable postgresql
  53. # 初始化数据库
  54. su - postgres -c "${pgsql_install_dir}/bin/initdb -D ${pgsql_data_dir}"
  55. if [ $? -ne 0 ]; then
  56. echo "${CFAILURE}PostgreSQL initdb failed! ${CEND}"
  57. kill -9 $$; exit 1;
  58. fi
  59. # 启动服务
  60. systemctl start postgresql
  61. if [ $? -ne 0 ]; then
  62. echo "${CFAILURE}PostgreSQL start failed! ${CEND}"
  63. kill -9 $$; exit 1;
  64. fi
  65. sleep 5
  66. # 设置密码
  67. su - postgres -c "${pgsql_install_dir}/bin/psql -c \"alter user postgres with password '$dbpostgrespwd';\""
  68. # 配置远程访问
  69. sed -i 's@^host.*@#&@g' ${pgsql_data_dir}/pg_hba.conf
  70. sed -i 's@^local.*@#&@g' ${pgsql_data_dir}/pg_hba.conf
  71. echo 'local all all md5' >> ${pgsql_data_dir}/pg_hba.conf
  72. echo 'host all all 0.0.0.0/0 md5' >> ${pgsql_data_dir}/pg_hba.conf
  73. # 允许远程连接
  74. sed -i "s@^#listen_addresses.*@listen_addresses = '*'@" ${pgsql_data_dir}/postgresql.conf
  75. # 重新加载配置
  76. systemctl reload postgresql
  77. if [ -e "${pgsql_install_dir}/bin/psql" ]; then
  78. sed -i "s+^dbpostgrespwd.*+dbpostgrespwd='$dbpostgrespwd'+" ../options.conf
  79. echo "${CSUCCESS}PostgreSQL installed successfully! ${CEND}"
  80. else
  81. rm -rf ${pgsql_install_dir} ${pgsql_data_dir}
  82. echo "${CFAILURE}PostgreSQL install failed, Please contact the author! ${CEND}" && grep -Ew 'NAME|ID|ID_LIKE|VERSION_ID|PRETTY_NAME' /etc/os-release
  83. kill -9 $$; exit 1;
  84. fi
  85. popd
  86. popd
  87. [ -z "$(grep ^'export PATH=' /etc/profile)" ] && echo "export PATH=${pgsql_install_dir}/bin:\$PATH" >> /etc/profile
  88. [ -n "$(grep ^'export PATH=' /etc/profile)" -a -z "$(grep ${pgsql_install_dir} /etc/profile)" ] && sed -i "s@^export PATH=\(.*\)@export PATH=${pgsql_install_dir}/bin:\1@" /etc/profile
  89. . /etc/profile
  90. }