How to Stop PostgreSQL Database Server using pg_ctl execution in Linux

Posted on

Introduction

This is an article where the content of the article is focusing on how to stop PostgreSQL database server. The proses for stopping the PostgreSQL database server is using ‘pg_ctl’. The execution of the ‘pg_ctl’ command exist in the command line. One of the reason for stopping the PostgreSQL database server using ‘pg_ctl’ is because it is also started using ‘pg_ctl’. So, it is obvious to stop the process using ‘pg_ctl’ that is started using ‘pg_ctl’.

Stopping PostgreSQL Database Server using pg_ctl in Linux

In order to stop PostgreSQL database server, it is very simple. Just execute the same command as starting the PostgreSQL database server. Before stopping PostgreSQL database server, check it first. The following is how to check the service if it is running or not :

  1. The simple one is checking the process by typing the command ‘ps’ as follows :

    [pgsql@10 ~]$ ps -ef | grep postgres
    pgsql      94341       1  0 06:34 ?        00:00:00 /opt/postgresql-12.0/app/bin/postgres -D /opt/postgresql-12.0/data
    pgsql      94343   94341  0 06:34 ?        00:00:00 postgres: checkpointer
    pgsql      94344   94341  0 06:34 ?        00:00:00 postgres: background writer
    pgsql      94345   94341  0 06:34 ?        00:00:00 postgres: walwriter
    pgsql      94346   94341  0 06:34 ?        00:00:00 postgres: autovacuum launcher
    pgsql      94347   94341  0 06:34 ?        00:00:00 postgres: stats collector
    pgsql      94348   94341  0 06:34 ?        00:00:00 postgres: logical replication launcher
    pgsql      94910   94307  0 09:16 pts/4    00:00:00 grep --color=auto postgres
    [pgsql@10 ~]$

    As in the above command output, there is a process of PostgreSQL database server currently running.

  2. The next one is to use the pg_ctl command with a different operation parameter. Besides starting and stopping the PostgreSQL database server, it is also possible to check the status of it. Just execute with the following command pattern :

    /app_folder_location_postgresql_database_server/bin/pg_ctl -D /data_directory_location_postgresql_database_server operation_parameter

    Just execute the above command pattern with the operation_parameter of ‘status’ as follows :

    [pgsql@10 ~]$ /opt/postgresql-12.0/app/bin/pg_ctl -D /opt/postgresql-12.0/data/ status
    pg_ctl: server is running (PID: 94341)
    /opt/postgresql-12.0/app/bin/postgres "-D" "/opt/postgresql-12.0/data"
    [pgsql@10 ~]$
    

    It is very clear according to the above command that the PostgreSQL database server is currently running.

  3. The last one is just to try to login to the PostgreSQL database server as follows :

    [pgsql@10 ~]$ psql -Upgsql postgres
    psql (12.0)
    Type "help" for help.
    
    postgres=#
    

If PostgreSQL database server is currently running, just execute the ‘pg_ctl’ as in the above command pattern below :

/app_folder_location_postgresql_database_server/bin/pg_ctl -D /data_directory_location_postgresql_database_server operation_parameter

The following is the actual command for stopping the PostgreSQL database server according to the above command pattern :

[pgsql@10 ~]$ /opt/postgresql-12.0/app/bin/pg_ctl -D /opt/postgresql-12.0/data/ stop
waiting for server to shut down....2021-06-18 09:26:26.303 EDT [94341] LOG:  received fast shutdown request
2021-06-18 09:26:26.308 EDT [94341] LOG:  aborting any active transactions
2021-06-18 09:26:26.309 EDT [94341] LOG:  background worker "logical replication launcher" (PID 94348) exited with exit code 1
2021-06-18 09:26:26.309 EDT [94343] LOG:  shutting down
2021-06-18 09:26:26.326 EDT [94341] LOG:  database system is shut down
 done
server stopped
[pgsql@10 ~]$

Leave a Reply