1
0

postgresql.sh 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. 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. /bin/cp ${oneinstack_dir}/init.d/postgresql.service /lib/systemd/system/
  23. sed -i "s@=/usr/local/pgsql@=${pgsql_install_dir}@g" /lib/systemd/system/postgresql.service
  24. sed -i "s@PGDATA=.*@PGDATA=${pgsql_data_dir}@" /lib/systemd/system/postgresql.service
  25. systemctl enable postgresql
  26. popd
  27. su - postgres -c "${pgsql_install_dir}/bin/initdb -D ${pgsql_data_dir}"
  28. systemctl start postgresql
  29. sleep 5
  30. su - postgres -c "${pgsql_install_dir}/bin/psql -c \"alter user postgres with password '$dbpostgrespwd';\""
  31. sed -i 's@^host.*@#&@g' ${pgsql_data_dir}/pg_hba.conf
  32. sed -i 's@^local.*@#&@g' ${pgsql_data_dir}/pg_hba.conf
  33. echo 'local all all md5' >> ${pgsql_data_dir}/pg_hba.conf
  34. echo 'host all all 0.0.0.0/0 md5' >> ${pgsql_data_dir}/pg_hba.conf
  35. sed -i "s@^#listen_addresses.*@listen_addresses = '*'@" ${pgsql_data_dir}/postgresql.conf
  36. systemctl reload postgresql
  37. if [ -e "${pgsql_install_dir}/bin/psql" ]; then
  38. sed -i "s+^dbpostgrespwd.*+dbpostgrespwd='$dbpostgrespwd'+" ../options.conf
  39. echo "${CSUCCESS}PostgreSQL installed successfully! ${CEND}"
  40. else
  41. rm -rf ${pgsql_install_dir} ${pgsql_data_dir}
  42. echo "${CFAILURE}PostgreSQL install failed, Please contact the author! ${CEND}" && grep -Ew 'NAME|ID|ID_LIKE|VERSION_ID|PRETTY_NAME' /etc/os-release
  43. kill -9 $$; exit 1;
  44. fi
  45. popd
  46. [ -z "$(grep ^'export PATH=' /etc/profile)" ] && echo "export PATH=${pgsql_install_dir}/bin:\$PATH" >> /etc/profile
  47. [ -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
  48. . /etc/profile
  49. }