Introduction
This article is focusing on the activity as in the title of the article. It is a script for starting, stopping, restarting, reloading and checking the status of the PostgreSQL database server. In this article, the execution is using ‘pg_ctl’ command for managing PostgreSQL database server. The usage of the ‘pg_ctl’ command will be available in the service script of PostgreSQL database server. The base for the PostgreSQL database server’s environment is in the article with the title of ‘How to Install PostgreSQL Database Server using compressed file in CentOS 8‘ in this link. Morever, the initialization of the data directory of the PostgreSQL database server exist in this link. It is an article with the title of ‘How to Initialize PostgreSQL Database in Linux‘.
Create a Service Script for PostgreSQL Database Server in Linux
Considering the environment and setting of the PostgreSQL database server as it is mentioned in the introduction part, the following is the step for creating a service script for PostgreSQL database server in linux :
-
First of all, switch to root account using the following command :
[admin@10 ~]$ sudo su - root [sudo] password for admin: Last login: Fri Jun 18 00:29:15 EDT 2021 on pts/4 [root@10 ~]#
-
Following after, create a new file in ‘/etc/init.d’ with the name of ‘postgresql-12.0.service’. Just choose any filename but consider it to be able to represent the actual function of that service script. Just execute the following command :
[root@10 ~]# touch /etc/init.d/postgresql-12.0.service
-
Next, check whether the file exist or not by typing the following command :
[root@10 ~]# ls -al /etc/init.d/ total 36 drwxr-xr-x. 2 root root 83 Jun 18 06:01 . drwxr-xr-x. 10 root root 127 Apr 7 16:55 .. -rw-r--r-- 1 root root 138 Jun 17 06:45 postgresql-12.0.service -rw-r--r--. 1 root root 1161 Apr 7 16:54 README [root@10 ~]#
-
Edit the ‘postgresql-12.0.service’ file and fill it with the following content :
#!/bin/bash su - pgsql -c "/opt/postgresql-12.0/app/bin/pg_ctl -D /opt/postgresql-12.0/data -l /opt/postgresql-12.0/data/startup.log $1"
Basically, the content of the file is the command pattern of switching to the ‘pgsql’ account and then execute the command ‘pg_ctl’ without specifying operation parameter. The following part is the part for switching to ‘pgsql’ account :
su - pgsql
The next part continuing the line configuration is the part for executing ‘pgsql’ command :
/opt/postgresql-12.0/app/bin/pg_ctl -D /opt/postgresql-12.0/data -l /opt/postgresql-12.0/data/startup.log $1
- Last but not least, add and modify the file to have the executable privilege. It is an important step so that it will be able to be executed. Do it by typing the following command :
chmod +x postgresql-12.0.service
- Finally, check the privilege of the file to confirm that the file can be executed. Do it just by typing the following command :
[root@10 ~]# ls -al /etc/init.d/ total 36 drwxr-xr-x. 2 root root 83 Jun 18 11:05 . drwxr-xr-x. 10 root root 127 Apr 7 16:55 .. -rw-r--r-- 1 root root 18434 Jul 24 2020 functions -rwxr-xr-x 1 root root 6734 Jun 17 2020 jenkins -rwxr-xr-x 1 root root 138 Jun 17 06:45 postgresql-12.0.service -rw-r--r--. 1 root root 1161 Apr 7 16:54 README [root@10 ~]#
2 thoughts on “How to Create a Service Script for PostgreSQL Database Server in Linux”