postgresql开机自启的方式

目录

方法一:postgresql设置开机自启动

1.找到tar包解压后postgresql的linux文件

 2.将linux文件拷贝到/etc/init.d/目录下,并命名为postgresql

 3.修改/etc/init.d/postgresql文件,并赋予执行权限

 4.启动服务

 5.设置开机自启动

方法二: 使用rc.local


方法一:postgresql设置开机自启动

1.找到tar包解压后postgresql的linux文件

[root@master src]# cd /usr/src     

[root@master src]# cd postgresql-11.10/contrib/start-scripts/

[root@master start-scripts]# ll -h

总用量 8.0K

-rw-r–r– 1 1107 1107 1.5K 11月 10 2020 freebsd
-rw-r–r– 1 1107 1107 3.5K 11月 10 2020 linux

drwxrwxrwx 2 1107 1107   84 11月 10 2020 macos

[root@master start-scripts]# pwd

/usr/src/postgresql-11.10/contrib/start-scripts

 

 2.将linux文件拷贝到/etc/init.d/目录下,并命名为postgresql

[root@master start-scripts]# pwd

/usr/src/postgresql-11.10/contrib/start-scripts

[root@master start-scripts]# cp linux /etc/init.d/postgresql

 3.修改/etc/init.d/postgresql文件,并赋予执行权限

[root@master ~]# cd /etc/init.d

[root@master init.d]# vim postgresql

# Installation prefix
prefix=/usr/local/pgsql            #postgresql安装的路径,按自己的路径修改

# Data directory
PGDATA=”/data/pgdata/data”     #postgresql存放数据的路径,按自己的路径修改

# Who to run the postmaster as, usually “postgres”.  (NOT “root”)
PGUSER=postgres                    #postgresql的用户,按自己创建的用户修改

# Where to keep a log file
PGLOG=”/data/pgdata/logs/postgresql.log”     #postgresql存放的日志,按自己的路径修改

[root@master init.d]# chmod +x postgresql 

[root@master init.d]# ll -h postgresql 

-rwxr-xr-x 1 root root 3.5K 8月  11 10:39 postgresql

[root@master init.d]# pwd

/etc/init.d

 4.启动服务

 [root@master ~]# service postgresql stop 

Stopping PostgreSQL: ok

[root@master ~]# ps -ef|grep postgres

root       4672   1522  0 18:36 pts/0    00:00:00 grep –color=auto postgres

[root@master ~]# service postgresql start 

Starting PostgreSQL: ok

[root@master ~]# ps -ef|grep postgres

postgres   4745      1  1 18:37 ?        00:00:00 /usr/local/pgsql/bin/postmaster -D /data/pgdata/data

postgres   4746   4745  0 18:37 ?        00:00:00 postgres: logger   

postgres   4748   4745  0 18:37 ?        00:00:00 postgres: checkpointer   

postgres   4749   4745  0 18:37 ?        00:00:00 postgres: background writer   

postgres   4750   4745  0 18:37 ?        00:00:00 postgres: walwriter   

postgres   4751   4745  0 18:37 ?        00:00:00 postgres: autovacuum launcher   

postgres   4752   4745  0 18:37 ?        00:00:00 postgres: archiver   

postgres   4753   4745  0 18:37 ?        00:00:00 postgres: stats collector   

postgres   4754   4745  0 18:37 ?        00:00:00 postgres: logical replication launcher   

root       4756   1522  0 18:37 pts/0    00:00:00 grep –color=auto postgres

[root@master ~]# service postgresql restart 

Restarting PostgreSQL: ok

[root@master ~]# ps -ef|grep postgres

postgres   4814      1  3 18:37 ?        00:00:00 /usr/local/pgsql/bin/postmaster -D /data/pgdata/data

postgres   4815   4814  0 18:37 ?        00:00:00 postgres: logger   

postgres   4817   4814  0 18:37 ?        00:00:00 postgres: checkpointer   

postgres   4818   4814  0 18:37 ?        00:00:00 postgres: background writer   

postgres   4819   4814  0 18:37 ?        00:00:00 postgres: walwriter   

postgres   4820   4814  0 18:37 ?        00:00:00 postgres: autovacuum launcher   

postgres   4821   4814  0 18:37 ?        00:00:00 postgres: archiver   

postgres   4822   4814  0 18:37 ?        00:00:00 postgres: stats collector   

postgres   4823   4814  0 18:37 ?        00:00:00 postgres: logical replication launcher   

root       4825   1522  0 18:37 pts/0    00:00:00 grep –color=auto postgres

 

 5.设置开机自启动

[root@master ~]#  chkconfig –add postgresql

[root@master ~]# 

[root@master ~]# chkconfig –list postgresql

注:该输出结果只显示 SysV 服务,并不包含

原生 systemd 服务。SysV 配置数据

可能被原生 systemd 配置覆盖。 

      要列出 systemd 服务,请执行 ‘systemctl list-unit-files’。

      查看在具体 target 启用的服务请执行

      ‘systemctl list-dependencies [target]’。

postgresql         0:关    1:关    2:开    3:开    4:开    5:开    6:关

##查看当前pg的进程

[root@master ~]# ps -ef |grep postgres  

 

##重启服务器,验证pg是否开机自启动,重启服务器的命令如下,二选其一即可:

[root@master ~]# init 6 

或者

[root@master ~]# reboot

##重启之后再次查看pg进程,

[root@master ~]# ps -ef |grep postgres

postgres   1115      1  0 09:03 ?        00:00:00 /usr/local/pgsql/bin/postmaster -D /data/pgdata/data

postgres   1129   1115  0 09:03 ?        00:00:00 postgres: logger   

postgres   1301   1115  0 09:03 ?        00:00:00 postgres: checkpointer   

postgres   1302   1115  0 09:03 ?        00:00:00 postgres: background writer   

postgres   1303   1115  0 09:03 ?        00:00:00 postgres: walwriter   

postgres   1304   1115  0 09:03 ?        00:00:00 postgres: autovacuum launcher   

postgres   1305   1115  0 09:03 ?        00:00:00 postgres: archiver   

postgres   1306   1115  0 09:03 ?        00:00:00 postgres: stats collector   

postgres   1307   1115  0 09:03 ?        00:00:00 postgres: logical replication launcher   

root       1670   1630  0 09:08 pts/0    00:00:00 grep –color=auto postgres

方法二: 使用rc.local

1.打开终端并使用root权限登录系统

2.编辑 /etc/rc.local 文件:
sudo vi /etc/rc.local

在文件的最后一行添加以下内容:
su – postgres -c “/usr/local/pgsql/bin/pg_ctl start -D /data/pgdata/data -l /data/pgdata/logs/postgresql.log”

###这里假设你的PostgreSQL安装路径为 /usr/local/pgsql,数据存储路径为 /usr/local/pgsql/data,日志文件存储路径为 /data/pgdata/logs/postgresql.log。如果这些路径不同,请相应地修改命令参数。

保存文件并退出编辑器。

3.添加可执行权限以使该文件在系统启动时自动运行:
sudo chmod +x /etc/rc.local

4.重启系统,确保PostgreSQL服务器已自动启动。

注意:如果你的Linux系统使用了systemd管理服务,则可以创建一个systemd服务单元来代替rc.local脚本来实现PostgreSQL的自启动。具体实现方法可以参考相关文档或教程

本文来自网络,不代表协通编程立场,如若转载,请注明出处:https://www.net2asp.com/3c8aea0d25.html