How to Activate PostgreSQL Database Service

Posted on

Regarding on the article created about PostgreSQL Database Server 10.4 installation on Linux Ubuntu 16.04 operating system titled  ‘How to Install PostgreSQL Database Server 10.4 on Ubuntu 16.04‘ in this link, this is the article written as an additional note about how to activate service of PostgreSQL Database Server 10.4 on Linux Ubuntu 16.04 operating system.

This article has a subject discussion which can be joined with the article mentioned before in this link, but in order to be more focus on the subject or material, it is separated in a different article.

After PostgreSQL Database Server 10.4 has already finished in the installation folder selected before, for an example the installation folder is located in  /opt/postgresql-10.4, then furthermore, the steps carried out at the first time after PostgreSQL Database Server has already been successfuly installed can be shown as follows  :

1. PostgreSQL Database data directory initialization by executing the following command  :

initdb -D /folder/data/directory/postgresql

If it can be set for an example, a folder which is selected to store the base data of PostgreSQL is located in  /opt/postgresql-10.4, then the execution of the above command will be shown as follows  :

initdb -D /opt/postgresql-10.4/data

The most important thing is that the above command execution is being executed by using  ‘postgres’ user account.

2. After the above command has been successfully carried out, the step taken after is creating an init script for running PostgreSQL service. This script itself will be called in order to run PostgreSQL service. Daemon which is called as a term for a program running as a background process as well as PostgreSQL can be executed by using the init script. For the Linux Ubuntu distribution itself, the init script location is specified in  /etc/init.d. As for an example, the script file name can be called as ‘postgresql’. So, in order to execute a command to make a bash script file which can be useful for administering service of PostgreSQL, below is the execution steps  :

cd /etc/init.d
root@hostname:~# cd /etc/init.d/

And after that, the following command can be executed  :

root@hostname:/etc/init.d# touch postgresql
root@hostname:/etc/init.d# vim postgresql

Or, it can be directly executed with the command shown as follows :

root@hostname:/etc/init.d# vim postgresql

And after creating the file, just fill the file with the following content :

su - pgsql -c "/opt/postgresql-10.4/apps/bin/pg_ctl -D /opt/postgresql-10.4/data -l /opt/postgresql-10.4/data/startup.log $1"

The actual point of the above command is for executing the utility or the program named pg_ctl with the additional -D parameter where the value is associated with the data directory of PostgreSQL Database Server and the -l additional parameter is pointing to the log file destination which can be called as an output target generated with the additional argument specified from the parameter $1.

3. Don’t forget to change the script mode so that it can be executed by running the following command :

chmod 755 mypostgres

This is the command execution display :

root@hostname:/etc/init.d# ls -al | grep postgresql
-rw-r--r--   1 root root     0 Jul 16 22:21 postgresql
root@hostname:/etc/init.d# chmod 755 postgresql 
root@hostname:/etc/init.d# ls -al | grep postgresql
-rwxr-xr-x   1 root root     0 Jul 16 22:21 postgresql
  1. And after that, save the file and execute it in the command line interface with the following command :
root@hostname:/opt/postgresql-10.4/data# /etc/init.d/postgresql start
waiting for server to start.... done
server started

One thought on “How to Activate PostgreSQL Database Service

Leave a Reply