1、进入postgresql源码安装包主目录执行 :find ./* -name linux会出现:./contrib/start-scripts/linux上所示文件为pgsql自带的脚本(位于源码安装包中),例如: postgresql-10.10.tar.gz
2、将上述找到的文件拷贝到linux系统的 /etc/init.d/ 目录中在pgsql主目录执行:cp ./contrib/start-scripts/linux /etc/init.d/postgresql
3、对上述文件赋予执行权限,并添加到开机启动进入/etc/init.d/目录:cd/etc/init.d/赋予权限:chomd +x postgresql添加到开机自启动chkconfig postgresql on查看是否添加成功:chkconfig --list | grep postgresql(2,3,4,5都是打开了(on),0,1,6 是关(off))关闭开机自启动:chkconfig postgresql off
4、若以上步骤过于繁琐,可使用以下脚本进行自动配置该脚本文件需要放置于postgresql安装包根目录,例如:/opt/postgresql-10.10/(安装包主目录)#!/bin/bashPGSQL_HOME=$(cd `dirname $0`; pwd)for path in $(/usr/bin/find ./* -name linux) do if [ `ls $path | grep 'contrib'` ];then echo "检测到文件: ""$path" echo "开始配置........" cp $PGSQL_HOME/$path /etc/init.d/postgresql chmod +x /etc/init.d/postgresql cd /etc/init.d/ chkconfig postgresql on echo "配置结果" echo `chkconfig --list|grep postgresql` exit 0; fi done
5、以上步骤仅限于将数据库安装在默认路径的,若安装在其他目录,需要修改自启动脚本中相关文件的路径