1
0

postgresql.sh 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #!/bin/bash
  2. # Author: yeho <lj2007331 AT gmail.com>
  3. # BLOG: https://blog.linuxeye.cn
  4. #
  5. # Notes: OneinStack for CentOS/RadHat 6+ Debian 6+ and Ubuntu 12+
  6. #
  7. # Project home page:
  8. # https://oneinstack.com
  9. # https://github.com/lj2007331/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. chown -R postgres.postgres ${pgsql_install_dir}
  21. /bin/cp ./contrib/start-scripts/linux /etc/init.d/postgresql
  22. sed -i "s@^prefix=.*@prefix=${pgsql_install_dir}@" /etc/init.d/postgresql
  23. sed -i "s@^PGDATA=.*@PGDATA=${pgsql_data_dir}@" /etc/init.d/postgresql
  24. chmod +x /etc/init.d/postgresql
  25. [ "${OS}" == "CentOS" ] && { chkconfig --add postgresql; chkconfig postgresql on; }
  26. [[ "${OS}" =~ ^Ubuntu$|^Debian$ ]] && update-rc.d postgresql defaults
  27. popd
  28. su - postgres -c "${pgsql_install_dir}/bin/initdb -D ${pgsql_data_dir}"
  29. service postgresql start
  30. sleep 5
  31. su - postgres -c "${pgsql_install_dir}/bin/psql -c \"alter user postgres with password '$dbpostgrespwd';\""
  32. sed -i 's@^host.*@#&@g' ${pgsql_data_dir}/pg_hba.conf
  33. sed -i 's@^local.*@#&@g' ${pgsql_data_dir}/pg_hba.conf
  34. echo 'local all all md5' >> ${pgsql_data_dir}/pg_hba.conf
  35. echo 'host all all 0.0.0.0/0 md5' >> ${pgsql_data_dir}/pg_hba.conf
  36. sed -i "s@^#listen_addresses.*@listen_addresses = '*'@" ${pgsql_data_dir}/postgresql.conf
  37. service postgresql reload
  38. if [ -e "${pgsql_install_dir}/bin/psql" ]; then
  39. sed -i "s+^dbpostgrespwd.*+dbpostgrespwd='$dbpostgrespwd'+" ../options.conf
  40. echo "${CSUCCESS}PostgreSQL installed successfully! ${CEND}"
  41. rm -rf postgresql-${pgsql_ver}
  42. else
  43. rm -rf ${pgsql_install_dir} ${pgsql_data_dir} postgresql-${pgsql_ver}
  44. echo "${CFAILURE}PostgreSQL install failed, Please contact the author! ${CEND}"
  45. kill -9 $$
  46. fi
  47. popd
  48. [ -z "$(grep ^'export PATH=' /etc/profile)" ] && echo "export PATH=${pgsql_install_dir}/bin:\$PATH" >> /etc/profile
  49. [ -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
  50. . /etc/profile
  51. }