postgresql.sh 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #!/bin/bash
  2. # Author: yeho <lj2007331 AT gmail.com>
  3. # BLOG: https://linuxeye.com
  4. #
  5. # Notes: OneinStack for CentOS/RedHat 6+ Debian 8+ and Ubuntu 14+
  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. id -u postgres >/dev/null 2>&1
  13. [ $? -ne 0 ] && useradd -d ${pgsql_install_dir} -s /bin/bash postgres
  14. mkdir -p ${pgsql_data_dir};chown postgres.postgres -R ${pgsql_data_dir}
  15. tar xzf postgresql-${pgsql_ver}.tar.gz
  16. pushd postgresql-${pgsql_ver}
  17. ./configure --prefix=$pgsql_install_dir
  18. make -j ${THREAD}
  19. make install
  20. chmod 755 ${pgsql_install_dir}
  21. chown -R postgres.postgres ${pgsql_install_dir}
  22. if [ -e /bin/systemctl ]; then
  23. /bin/cp ${oneinstack_dir}/init.d/postgresql.service /lib/systemd/system/
  24. sed -i "s@=/usr/local/pgsql@=${pgsql_install_dir}@g" /lib/systemd/system/postgresql.service
  25. sed -i "s@PGDATA=.*@PGDATA=${pgsql_data_dir}@" /lib/systemd/system/postgresql.service
  26. systemctl enable postgresql
  27. else
  28. /bin/cp ./contrib/start-scripts/linux /etc/init.d/postgresql
  29. sed -i "s@^prefix=.*@prefix=${pgsql_install_dir}@" /etc/init.d/postgresql
  30. sed -i "s@^PGDATA=.*@PGDATA=${pgsql_data_dir}@" /etc/init.d/postgresql
  31. chmod +x /etc/init.d/postgresql
  32. [ "${PM}" == 'yum' ] && { chkconfig --add postgresql; chkconfig postgresql on; }
  33. [ "${PM}" == 'apt-get' ] && update-rc.d postgresql defaults
  34. fi
  35. popd
  36. su - postgres -c "${pgsql_install_dir}/bin/initdb -D ${pgsql_data_dir}"
  37. service postgresql start
  38. sleep 5
  39. su - postgres -c "${pgsql_install_dir}/bin/psql -c \"alter user postgres with password '$dbpostgrespwd';\""
  40. sed -i 's@^host.*@#&@g' ${pgsql_data_dir}/pg_hba.conf
  41. sed -i 's@^local.*@#&@g' ${pgsql_data_dir}/pg_hba.conf
  42. echo 'local all all md5' >> ${pgsql_data_dir}/pg_hba.conf
  43. echo 'host all all 0.0.0.0/0 md5' >> ${pgsql_data_dir}/pg_hba.conf
  44. sed -i "s@^#listen_addresses.*@listen_addresses = '*'@" ${pgsql_data_dir}/postgresql.conf
  45. service postgresql reload
  46. if [ -e "${pgsql_install_dir}/bin/psql" ]; then
  47. sed -i "s+^dbpostgrespwd.*+dbpostgrespwd='$dbpostgrespwd'+" ../options.conf
  48. echo "${CSUCCESS}PostgreSQL installed successfully! ${CEND}"
  49. else
  50. rm -rf ${pgsql_install_dir} ${pgsql_data_dir}
  51. echo "${CFAILURE}PostgreSQL install failed, Please contact the author! ${CEND}" && lsb_release -a
  52. kill -9 $$
  53. fi
  54. popd
  55. [ -z "$(grep ^'export PATH=' /etc/profile)" ] && echo "export PATH=${pgsql_install_dir}/bin:\$PATH" >> /etc/profile
  56. [ -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
  57. . /etc/profile
  58. }