PostgreSQL Database Cluster is the collection of databases that are managed by a single running PostgreSQL Instance. Before using the Database Cluster it should be initialized using the initdb command. Once the Database Cluster initializations are complete, the database can be used to perform basic/advanced operations. Postgres is the default database that gets created and uses by third-party applications, users, and utilities. The other default database created during the initialization phase is template1 which is not used for actual work.

The data will be stored in the data directory or data area of the database cluster and the location could be /usr/local/pgsql/data or /var/lib/pgsql/data on Linux Operating System and C:\Program Files\PostgreSQL\14\data on Windows Operating System although it does not default.

Note, that 14 is the PostgreSQL version number.

PostgreSQL Initdb Command

The PostgreSQL Initdb Command is used to create a new PostgreSQL cluster and initialize the database cluster manually. We can run the below command by passing the location of the database cluster using the -D option. As an alternative approach, we can use the pg_ctl program to perform the same activity. The commands are as mentioned below.

   postgres=# initdb -D /usr/local/pgsql/data

   postgres=# pg_ctl -D /usr/local/pgsql/data initdb


PostgreSQL Cluster Stop

We can use the following command to stop the PostgreSQL database cluster. First, we will log in using the postgres user and then run the below command to stop the PostgreSQL database cluster.


Command:

   cloudduggu@ubuntu:-$ sudo -i -u postgres

   cloudduggu@ubuntu:-$ systemctl stop postgresql@14-main

   cloudduggu@ubuntu:-$ ps -ef | grep postgres



Output:

In the below output we can see the PostgreSQL database cluster has been stopped.


postgresql stop database cluster

PostgreSQL Cluster Start

To start the PostgreSQL Database Cluster, use the following command.


Command:

   cloudduggu@ubuntu:-$ sudo -i -u postgres

   cloudduggu@ubuntu:-$ systemctl start postgresql@14-main


Output:

We can verify in the below output that the PostgreSQL Database Cluster has been started.


postgresql start database cluster


PostgreSQL Shutdown Types

PostgreSQL provides three different modes to shut down the database server which include Smart Shutdown mode, Fast Shutdown mode, and the Immediate Shutdown mode.

Let us see each PostgreSQL shutdown mode in detail.

PostgreSQL Smart Shutdown Mode:

In Smart Shutdown Mode, the server does not allow the new connection and waits to complete the existing connections. The PostgreSQL server shutdown only when all connections are closed. If there is an online backup running in that case it waits to get the backup complete. In case there is a server recovery during Smart Shutdown mode then the recovery will only be stopped when all sessions are terminated.

PostgreSQL Fast Shutdown Mode:

In Fast Shutdown mode, the server will not take any new connections and will notify all existing connections to abort the work and exit properly. Once all server processes are terminated and exit then the database server will shut down. If there is an online backup running then the backup job is terminated.

PostgreSQL Immediate Shutdown Mode:

In Immediate Shutdown mode, the server will send a notification to all child processes to abort the work, if they will not abort in 5 seconds then the server process will abort the child process. In Immediate shutdown mode, the recovery will take place on the next startup by applying the WAL logs. This mode is recommended only in emergencies.

PostgreSQL Kill -9 Command:

Using the Kill -9 we can directly send the signal to kill the PostgreSQL process. Once this command is submitted, all the existing connections to the Database cluster will be gone. The PID of PostgreSQL can be founded using the ps command can be used along with the Kill command to do so. It is recommended to avoid using the Kill -9 command.


PostgreSQL Database Cluster Reload and Restart

PostgreSQL Reload is used when we need to perform some changes to the server parameter in that case we can reload the configuration to take immediate effect. PostgreSQL Restart without restarting the PostgreSQL services. In certain cases the configuration change does not take effect, in that case, we need to restart the services graceful which is called PostgreSQL Restart.

PostgreSQL Reload Command:

Let us see the PostgreSQL Reload command with the below example. We will log in using the postgres user and run the reload command.


Command:

   cloudduggu@ubuntu:-$ sudo -i -u postgres

   cloudduggu@ubuntu:-$ systemctl reload postgresql@14-main

Output:

In the below output we can see the PostgreSQL database cluster has been reloaded.


postgresql reload database cluster


PostgreSQL Restart Command:

Let us run the PostgreSQL Restart command to restart the PostgreSQL database cluster services.


Command:

   cloudduggu@ubuntu:-$ sudo -i -u postgres

   cloudduggu@ubuntu:-$ systemctl restart postgresql@14-main

Output:

In the below output we can see the PostgreSQL database cluster has been restarted.


postgresql restart database cluster