Ubuntu 16.04设置PostgreSQL开机启动的方法

由于PostgreSQL是编译安装的,需要设置开机启动。对于Ubuntu系统来说,和Red Hat之类的系统稍微有一点不一样。

首先第一步,由于我们习惯于使用Red Hat这样的系统,所以我们需要弄一个chkconfig出来。这个命令是配置Red Hat服务启动停止的命令,但是在Ubuntu上没有。我们可以使用sysv-rc-conf。

  sudo apt install sysv-rc-conf

安装完成sysv-rc-conf之后,我们把他换成chkconfig。毕竟这个命令太难记住了。

  sudo cp /usr/sbin/sysv-rc-conf /usr/sbin/chkconfig

接下来我们可以尝试使用chkconfig命令来查看

Ubuntu 16.04设置PostgreSQL开机启动的方法 Linux 第1张

这个弄好之后,我们需要到PostgreSQL的安装压缩包的/postgresql-10.3/contrib/start-scripts目录下。目录下面有好几个系统的配置文件,我们这里使用的是Ununtu就选择linux,把这个文件复制到/etc/init.d/下面。并赋予可执行x权限。

  postgres@postgres-N65S01:~/postgresql-10.3/contrib/start-scripts$ ls  freebsd linux macos osx  postgres@postgres-N65S01:~/sudo cp linux /etc/init.d/  postgres@postgres-N65S01:/etc/init.d$sudo mv linux postgresql-10.0  postgres@postgres-N65S01:/etc/init.d$chmod a+x postgresql-10.0

做完上述操作之后需要修改该文件,主要修改下列两个位置

Ubuntu 16.04设置PostgreSQL开机启动的方法 Linux 第2张

prefix是postgreSQL安装目录,PGDATA是数据库文件所在目录。修改完成之后保存。执行下列命令,把服务设置成自动启动即可。

  postgres@postgres-N65S01:/etc/init.d$ sudo chkconfig postgresql-10.0 on  postgres@postgres-N65S01:/etc/init.d$ sudo chkconfig --list | grep postgres  postgresql-1 2:on 3:on 4:on 5:on

再一次重启主机之后,可以发现postgreSQL数据库可以自动启动了。

  postgres@postgres-N65S01:/etc/init.d$ ps -ef | grep pgdata  postgres 2460   1 0 19:19 ?    00:00:00 /opt/PostgreSQL-10/bin/postmaster -D /pgdata/data  postgres 4683 2853 0 19:43 pts/18  00:00:00 grep --color=auto pgdata

总结

以上所述是小编给大家介绍的Ubuntu 16.04设置PostgreSQL开机启动的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

参与评论