PostgreSQL DBA – How to START/STOP PostgreSQL 13 ON RHEL 7

How to START/STOP PostgreSQL ON RHEL 7

Table of Contents



1. Overview

There are two ways to control the server: with systemctl (or service) as the root user, or with pg_ctl as the postgres user.

Task As root (systemd) As postgres (pg_ctl)
Status systemctl status postgresql-13.service pg_ctl status -D /var/lib/pgsql/13/data
Stop systemctl stop postgresql-13.service pg_ctl stop -D /var/lib/pgsql/13/data
Start systemctl start postgresql-13.service pg_ctl start -D /var/lib/pgsql/13/data
Restart systemctl restart postgresql-13.service pg_ctl restart -D /var/lib/pgsql/13/data
Reload systemctl reload postgresql-13.service pg_ctl reload -D /var/lib/pgsql/13/data

The service postgresql-13.service <action> form also works on RHEL 7. It simply redirects to systemctl. The full path of pg_ctl is /usr/pgsql-13/bin/pg_ctl.

Restart or reload? After you change postgresql.conf, one of them is required. Reload makes the running server re-read the configuration files without disconnecting anyone. Restart stops the whole instance and starts it again, so all sessions are disconnected. Some parameters (for example shared_buffers, wal_level, archive_mode) only change after a restart. To see which settings are waiting for a restart, run SELECT name, setting FROM pg_settings WHERE pending_restart;

Note: Use one method per instance. If you start the server with pg_ctl, systemctl status will show it as inactive (dead) even though it is running, and systemctl start will then fail because the port and data directory are already in use.

2. How to Check the PostgreSQL Server Status

Method 1: list the systemd units

[root@rac1 ~]# systemctl list-units | grep postgresql
  postgresql-13.service     loaded active running   PostgreSQL 13 database server

Method 2: service status

[root@rac1 ~]# service postgresql-13.service status
Redirecting to /bin/systemctl status postgresql-13.service
● postgresql-13.service - PostgreSQL 13 database server
   Loaded: loaded (/usr/lib/systemd/system/postgresql-13.service; enabled; vendor preset: disabled)
   Active: active (running) since Mon 2021-03-15 17:57:05 +08; 53min ago
     Docs: https://www.postgresql.org/docs/13/static/
  Process: 969 ExecStartPre=/usr/pgsql-13/bin/postgresql-13-check-db-dir ${PGDATA} (code=exited, status=0/SUCCESS)
 Main PID: 991 (postmaster)
   CGroup: /system.slice/postgresql-13.service
           β”œβ”€ 991 /usr/pgsql-13/bin/postmaster -D /var/lib/pgsql/13/data/
           β”œβ”€1053 postgres: logger
           β”œβ”€1178 postgres: checkpointer
           β”œβ”€1179 postgres: background writer
           β”œβ”€1180 postgres: walwriter
           β”œβ”€1181 postgres: autovacuum launcher
           β”œβ”€1182 postgres: stats collector
           β”œβ”€1183 postgres: logical replication launcher
           └─2282 postgres: postgres postgres [local] idle

Mar 15 17:57:03 rac1.rajasekhar.com systemd[1]: Starting PostgreSQL 13 database server...
Mar 15 17:57:04 rac1.rajasekhar.com postmaster[991]: 2021-03-15 17:57:04.155 +08 [991] LOG:  redirecting log output to logging collector process
Mar 15 17:57:04 rac1.rajasekhar.com postmaster[991]: 2021-03-15 17:57:04.155 +08 [991] HINT:  Future log output will appear in directory "log".
Mar 15 17:57:05 rac1.rajasekhar.com systemd[1]: Started PostgreSQL 13 database server.

Method 3: check the processes

[root@rac1 ~]# ps -ef | grep postgres
postgres   991     1  0 17:57 ?        00:00:00 /usr/pgsql-13/bin/postmaster -D /var/lib/pgsql/13/data/
postgres  1053   991  0 17:57 ?        00:00:00 postgres: logger
postgres  1178   991  0 17:57 ?        00:00:00 postgres: checkpointer
postgres  1179   991  0 17:57 ?        00:00:00 postgres: background writer
postgres  1180   991  0 17:57 ?        00:00:00 postgres: walwriter
postgres  1181   991  0 17:57 ?        00:00:00 postgres: autovacuum launcher
postgres  1182   991  0 17:57 ?        00:00:00 postgres: stats collector
postgres  1183   991  0 17:57 ?        00:00:00 postgres: logical replication launcher
root      2201  2143  0 17:58 pts/0    00:00:00 su - postgres
postgres  2202  2201  0 17:58 pts/0    00:00:00 -bash
postgres  2281  2202  0 17:58 pts/0    00:00:00 psql
postgres  2282   991  0 17:58 ?        00:00:00 postgres: postgres postgres [local] idle
root      5744  5275  0 18:55 pts/1    00:00:00 grep --color=auto postgres

Method 4: pg_ctl status

[postgres@rac1 ~]$ cd /usr/pgsql-13/bin/
[postgres@rac1 bin]$ ./pg_ctl status -D /var/lib/pgsql/13/data/
pg_ctl: server is running (PID: 991)
/usr/pgsql-13/bin/postgres "-D" "/var/lib/pgsql/13/data/"

3. How to Stop the PostgreSQL Server

With pg_ctl (as the postgres user):

[postgres@rac1 ~]$ cd /usr/pgsql-13/bin/
[postgres@rac1 bin]$ ./pg_ctl stop -D /var/lib/pgsql/13/data/
waiting for server to shut down.... done
server stopped

[root@rac1 ~]# ps -ef | grep postgres
root      6718  2143  0 19:07 pts/0    00:00:00 grep --color=auto postgres

With systemd (as the root user):

[root@rac1 ~]# service postgresql-13.service stop
Redirecting to /bin/systemctl stop postgresql-13.service

[root@rac1 ~]# service postgresql-13.service status
Redirecting to /bin/systemctl status postgresql-13.service
● postgresql-13.service - PostgreSQL 13 database server
   Loaded: loaded (/usr/lib/systemd/system/postgresql-13.service; enabled; vendor preset: disabled)
   Active: inactive (dead) since Mon 2021-03-15 19:07:03 +08; 5min ago
     Docs: https://www.postgresql.org/docs/13/static/
  Process: 991 ExecStart=/usr/pgsql-13/bin/postmaster -D ${PGDATA} (code=exited, status=0/SUCCESS)
  Process: 969 ExecStartPre=/usr/pgsql-13/bin/postgresql-13-check-db-dir ${PGDATA} (code=exited, status=0/SUCCESS)
 Main PID: 991 (code=exited, status=0/SUCCESS)

Mar 15 17:57:03 rac1.rajasekhar.com systemd[1]: Starting PostgreSQL 13 database server...
Mar 15 17:57:04 rac1.rajasekhar.com postmaster[991]: 2021-03-15 17:57:04.155 +08 [991] LOG:  redirecting log output to logging collector process
Mar 15 17:57:04 rac1.rajasekhar.com postmaster[991]: 2021-03-15 17:57:04.155 +08 [991] HINT:  Future log output will appear in directory "log".
Mar 15 17:57:05 rac1.rajasekhar.com systemd[1]: Started PostgreSQL 13 database server.

4. How to Start the PostgreSQL Server

With pg_ctl (as the postgres user):

[postgres@rac1 ~]$ cd /usr/pgsql-13/bin/
[postgres@rac1 bin]$ ./pg_ctl start -D /var/lib/pgsql/13/data/
waiting for server to start....2021-03-15 19:14:17.815 +08 [7443] LOG:  redirecting log output to logging collector process
2021-03-15 19:14:17.815 +08 [7443] HINT:  Future log output will appear in directory "log".
 done
server started

With systemd (as the root user):

[root@rac1 ~]# service postgresql-13.service start
Redirecting to /bin/systemctl start postgresql-13.service

[root@rac1 ~]# service postgresql-13.service status
Redirecting to /bin/systemctl status postgresql-13.service
● postgresql-13.service - PostgreSQL 13 database server
   Loaded: loaded (/usr/lib/systemd/system/postgresql-13.service; enabled; vendor preset: disabled)
   Active: active (running) since Mon 2021-03-15 19:15:14 +08; 3s ago
     Docs: https://www.postgresql.org/docs/13/static/
  Process: 7523 ExecStartPre=/usr/pgsql-13/bin/postgresql-13-check-db-dir ${PGDATA} (code=exited, status=0/SUCCESS)
 Main PID: 7529 (postmaster)
   CGroup: /system.slice/postgresql-13.service
           β”œβ”€7529 /usr/pgsql-13/bin/postmaster -D /var/lib/pgsql/13/data/
           β”œβ”€7533 postgres: logger
           β”œβ”€7535 postgres: checkpointer
           β”œβ”€7536 postgres: background writer
           β”œβ”€7537 postgres: walwriter
           β”œβ”€7538 postgres: autovacuum launcher
           β”œβ”€7539 postgres: stats collector
           └─7540 postgres: logical replication launcher

Mar 15 19:15:14 rac1.rajasekhar.com systemd[1]: Starting PostgreSQL 13 database server...
Mar 15 19:15:14 rac1.rajasekhar.com postmaster[7529]: 2021-03-15 19:15:14.193 +08 [7529] LOG:  redirecting log output to logging collector process
Mar 15 19:15:14 rac1.rajasekhar.com postmaster[7529]: 2021-03-15 19:15:14.193 +08 [7529] HINT:  Future log output will appear in directory "log".
Mar 15 19:15:14 rac1.rajasekhar.com systemd[1]: Started PostgreSQL 13 database server.

5. How to Restart the PostgreSQL Server

Tip: -w (--wait) makes pg_ctl wait until the operation completes. This is the default.

With pg_ctl (as the postgres user):

[postgres@rac1 ~]$ cd /usr/pgsql-13/bin/
[postgres@rac1 bin]$ ./pg_ctl -w restart -D /var/lib/pgsql/13/data/
waiting for server to shut down.... done
server stopped
waiting for server to start....2021-03-15 19:22:58.920 +08 [8017] LOG:  redirecting log output to logging collector process
2021-03-15 19:22:58.920 +08 [8017] HINT:  Future log output will appear in directory "log".
 done
server started

With systemd (as the root user):

[root@rac1 ~]# service postgresql-13.service restart
Redirecting to /bin/systemctl restart postgresql-13.service

[root@rac1 ~]# service postgresql-13.service status
Redirecting to /bin/systemctl status postgresql-13.service
● postgresql-13.service - PostgreSQL 13 database server
   Loaded: loaded (/usr/lib/systemd/system/postgresql-13.service; enabled; vendor preset: disabled)
   Active: active (running) since Mon 2021-03-15 19:52:26 +08; 3s ago
     Docs: https://www.postgresql.org/docs/13/static/
  Process: 10439 ExecStartPre=/usr/pgsql-13/bin/postgresql-13-check-db-dir ${PGDATA} (code=exited, status=0/SUCCESS)
 Main PID: 10445 (postmaster)
   CGroup: /system.slice/postgresql-13.service
           β”œβ”€10445 /usr/pgsql-13/bin/postmaster -D /var/lib/pgsql/13/data/
           β”œβ”€10449 postgres: logger
           β”œβ”€10451 postgres: checkpointer
           β”œβ”€10452 postgres: background writer
           β”œβ”€10453 postgres: walwriter
           β”œβ”€10454 postgres: autovacuum launcher
           β”œβ”€10455 postgres: stats collector
           └─10456 postgres: logical replication launcher

Mar 15 19:52:26 rac1.rajasekhar.com systemd[1]: Starting PostgreSQL 13 database server...
Mar 15 19:52:26 rac1.rajasekhar.com postmaster[10445]: 2021-03-15 19:52:26.856 +08 [10445] LOG:  redirecting log output to logging collector process
Mar 15 19:52:26 rac1.rajasekhar.com postmaster[10445]: 2021-03-15 19:52:26.856 +08 [10445] HINT:  Future log output will appear in directory "log".
Mar 15 19:52:26 rac1.rajasekhar.com systemd[1]: Started PostgreSQL 13 database server.

6. How to Reload the PostgreSQL Server

A reload does not disconnect users. reload sends a SIGHUP signal to the postmaster, which re-reads postgresql.conf and pg_hba.conf.

With pg_ctl (as the postgres user):

[postgres@rac1 bin]$ ./pg_ctl reload -D /var/lib/pgsql/13/data
server signaled

With systemd (as the root user):

[root@rac1 ~]# service postgresql-13.service reload
Redirecting to /bin/systemctl reload postgresql-13.service

[root@rac1 ~]# service postgresql-13.service status
Redirecting to /bin/systemctl status postgresql-13.service
● postgresql-13.service - PostgreSQL 13 database server
   Loaded: loaded (/usr/lib/systemd/system/postgresql-13.service; enabled; vendor preset: disabled)
   Active: active (running) since Mon 2021-03-15 19:52:26 +08; 11min ago
     Docs: https://www.postgresql.org/docs/13/static/
  Process: 11218 ExecReload=/bin/kill -HUP $MAINPID (code=exited, status=0/SUCCESS)
  Process: 10439 ExecStartPre=/usr/pgsql-13/bin/postgresql-13-check-db-dir ${PGDATA} (code=exited, status=0/SUCCESS)
 Main PID: 10445 (postmaster)
   CGroup: /system.slice/postgresql-13.service
           β”œβ”€10445 /usr/pgsql-13/bin/postmaster -D /var/lib/pgsql/13/data/
           β”œβ”€10449 postgres: logger
           β”œβ”€10451 postgres: checkpointer
           β”œβ”€10452 postgres: background writer
           β”œβ”€10453 postgres: walwriter
           β”œβ”€10454 postgres: autovacuum launcher
           β”œβ”€10455 postgres: stats collector
           └─10456 postgres: logical replication launcher

Mar 15 19:52:26 rac1.rajasekhar.com systemd[1]: Starting PostgreSQL 13 database server...
Mar 15 19:52:26 rac1.rajasekhar.com postmaster[10445]: 2021-03-15 19:52:26.856 +08 [10445] LOG:  redirecting log output to logging collector process
Mar 15 19:52:26 rac1.rajasekhar.com postmaster[10445]: 2021-03-15 19:52:26.856 +08 [10445] HINT:  Future log output will appear in directory "log".
Mar 15 19:52:26 rac1.rajasekhar.com systemd[1]: Started PostgreSQL 13 database server.
Mar 15 19:53:40 rac1.rajasekhar.com systemd[1]: Reloaded PostgreSQL 13 database server.
Mar 15 20:03:49 rac1.rajasekhar.com systemd[1]: Reloaded PostgreSQL 13 database server.
Caution: Your use of any information or materials on this website is entirely at your own risk. It is provided for educational purposes only. It has been tested internally, however, we do not guarantee that it will work for you. Ensure that you run it in your test environment before using.
Thank you
Rajasekhar Amudala