How to Restart the Postgres Service?

Find the name of the service
Check the current status of the service
Stop the service
Start the service
Restart the service

In this article, we will learn how to restart the PostgreSQL service on a Red Hat Linux system.
Linux provides a useful command called systemctl which helps us manage services on our Linux machine. With systemctl, we can easily start, stop, restart, and check the status of any service.

Find the name of the service

To begin, let's find the name of the service on your machine.

[root@dbdocs system]# cd /usr/lib/systemd/system
[root@dbdocs system]# ll postgresql*
-rw-r--r--. 1 root root 1546 Feb 12 16:07 postgresql.service
-rw-r--r--. 1 root root 1507 Feb 12 16:07 [email protected]

Check the current status of the service

To determine the current status of the PostgreSQL service ('postgresql.service'), we'll use the following command:

[postgres@dbdocs system]# sudo systemctl status postgresql.service
● postgresql.service - PostgreSQL database server
   Loaded: loaded (/usr/lib/systemd/system/postgresql.service; enabled; vendor preset: disabled)
   Active: 'active (running)' since Sun 2023-07-02 11:13:10 EDT; 9min ago
  Process: 6155 ExecStartPre=/usr/libexec/postgresql-check-db-dir postgresql (code=exited, status=0/SUCCESS)
 Main PID: 6158 (postmaster)
    Tasks: 7 (limit: 17051)
   Memory: 19.1M
   CGroup: /system.slice/postgresql.service
           ├─6158 /usr/bin/postmaster -D /var/lib/pgsql/data
           ├─6159 postgres: logger
           ├─6160 postgres: checkpointer
           ├─6161 postgres: background writer
           ├─6163 postgres: walwriter
           ├─6164 postgres: autovacuum launcher
           └─6165 postgres: logical replication launcher

Jul 02 11:13:10 dbdocs systemd[1]: Starting PostgreSQL database server...
Jul 02 11:13:10 dbdocs postmaster[6158]: 2023-07-02 11:13:10.935 EDT [6158] LOG:  redirecting log output to logging collector process
Jul 02 11:13:10 dbdocs postmaster[6158]: 2023-07-02 11:13:10.935 EDT [6158] HINT:  Future log output will appear in directory "log".
Jul 02 11:13:10 dbdocs systemd[1]: Started PostgreSQL database server.
The output indicates that the service is 'active and running'. Please take note of the timestamp displayed so we can compare it with the upcoming output.

Stop the service

Type the below command to stop the PostgreSQL services.

[postgres@dbdocs system]# sudo systemctl stop postgresql.service

[postgres@dbdocs system]# sudo systemctl status postgresql.service
● postgresql.service - PostgreSQL database server
   Loaded: loaded (/usr/lib/systemd/system/postgresql.service; enabled; vendor preset: disabled)
   Active: 'inactive (dead)' since Sun 2023-07-02 11:22:23 EDT; 2s ago
  Process: 6158 ExecStart=/usr/bin/postmaster -D ${PGDATA} (code=exited, status=0/SUCCESS)
  Process: 6155 ExecStartPre=/usr/libexec/postgresql-check-db-dir postgresql (code=exited, status=0/SUCCESS)
 Main PID: 6158 (code=exited, status=0/SUCCESS)

Jul 02 11:13:10 dbdocs systemd[1]: Starting PostgreSQL database server...
Jul 02 11:13:10 dbdocs postmaster[6158]: 2023-07-02 11:13:10.935 EDT [6158] LOG:  redirecting log output to logging collector process
Jul 02 11:13:10 dbdocs postmaster[6158]: 2023-07-02 11:13:10.935 EDT [6158] HINT:  Future log output will appear in directory "log".
Jul 02 11:13:10 dbdocs systemd[1]: Started PostgreSQL database server.
Jul 02 11:22:23 dbdocs systemd[1]: Stopping PostgreSQL database server...
Jul 02 11:22:23 dbdocs systemd[1]: postgresql.service: Killing process 6159 (postmaster) with signal SIGKILL.
Jul 02 11:22:23 dbdocs systemd[1]: postgresql.service: Succeeded.
Jul 02 11:22:23 dbdocs systemd[1]: Stopped PostgreSQL database server.

Start the service

Use the following command to start the service:

[root@dbdocs system]# sudo systemctl start postgresql.service

[postgres@dbdocs system]# sudo systemctl status postgresql.service
● postgresql.service - PostgreSQL database server
   Loaded: loaded (/usr/lib/systemd/system/postgresql.service; enabled; vendor preset: disabled)
   Active: 'active (running)' since Sun 2023-07-02 11:22:31 EDT; 1s ago
  Process: 6487 ExecStartPre=/usr/libexec/postgresql-check-db-dir postgresql (code=exited, status=0/SUCCESS)
 Main PID: 6490 (postmaster)
    Tasks: 7 (limit: 17051)
   Memory: 17.5M
   CGroup: /system.slice/postgresql.service
           ├─6490 /usr/bin/postmaster -D /var/lib/pgsql/data
           ├─6491 postgres: logger
           ├─6492 postgres: checkpointer
           ├─6493 postgres: background writer
           ├─6495 postgres: walwriter
           ├─6496 postgres: autovacuum launcher
           └─6497 postgres: logical replication launcher

Jul 02 11:22:31 dbdocs systemd[1]: Starting PostgreSQL database server...
Jul 02 11:22:31 dbdocs postmaster[6490]: 2023-07-02 11:22:31.222 EDT [6490] LOG:  redirecting log output to logging collector process
Jul 02 11:22:31 dbdocs postmaster[6490]: 2023-07-02 11:22:31.222 EDT [6490] HINT:  Future log output will appear in directory "log".
Jul 02 11:22:31 dbdocs systemd[1]: Started PostgreSQL database server.
[root@dbdocs system]#


You can observe that the PostgreSQL service has been restarted. The "time running" value indicates that it has been running for approximately 1 seconds, indicating that the service has just been started.

Restart the service


[postgres@dbdocs ~]# sudo  systemctl restart postgresql.service



Related content



Rate Your Experience

: 89 : 1


Last updated in December, 2024

Cloud Technology


Read more | Learn more

Oracle Database


Read more | Learn more

MSSQL Database


Read more | Learn more

PostGres Database


Read more | Learn more

Linux


Read more | Learn more

ASP/C#


Read more | Learn more

Online Tests


Read more | Learn more