To check the status of the PostgreSQL server on a Linux server, you can use the systemctl
command. This command is used to manage and query the status of system services, including the PostgreSQL server. To check the status of the PostgreSQL server, you can use the following systemctl
command:
systemctl status postgresql
This command will query the status of the postgresql
service, which is the default service name for the PostgreSQL server on most Linux distributions. If the PostgreSQL server is running, you should see output similar to this:
● postgresql.service - PostgreSQL RDBMS
Loaded: loaded (/usr/lib/systemd/system/postgresql.service; enabled; vendor preset: disabled)
Active: active (running) since Fri 2021-12-10 14:31:55 UTC; 1 day ago
Process: 23456 ExecStartPre=/usr/pgsql-12/bin/postgresql-12-check-db-dir ${PGDATA} (code=exited, status=0/SUCCESS)
Main PID: 23478 (postgres)
CGroup: /system.slice/postgresql.service
├─23478 /usr/pgsql-12/bin/postmaster -D /var/lib/pgsql/12/data
├─23479 postgres: logger process
└─24789 postgres: checkpointer process
Dec 11 14:31:55 localhost.localdomain systemd[1]: Starting PostgreSQL RDBMS...
Dec 11 14:31:55 localhost.localdomain systemd[1]: Started PostgreSQL RDBMS.
This output indicates that the postgresql
service is loaded, enabled, and currently active (running). It also shows the main process ID of the PostgreSQL server (23478 in this example), as well as the start time of the service.
To check the status of the PostgreSQL server using netstat
, you can use the following command:
● postgresql.service - PostgreSQL RDBMS
Loaded: loaded (/usr/lib/systemd/system/postgresql.service; enabled; vendor preset: disabled)
Active: active (running) since Fri 2021-12-10 14:31:55 UTC; 1 day ago
Process: 23456 ExecStartPre=/usr/pgsql-12/bin/postgresql-12-check-db-dir ${PGDATA} (code=exited, status=0/SUCCESS)
Main PID: 23478 (postgres)
CGroup: /system.slice/postgresql.service
├─23478 /usr/pgsql-12/bin/postmaster -D /var/lib/pgsql/12/data
├─23479 postgres: logger process
└─24789 postgres: checkpointer process
Dec 11 14:31:55 localhost.localdomain systemd[1]: Starting PostgreSQL RDBMS...
Dec 11 14:31:55 localhost.localdomain systemd[1]: Started PostgreSQL RDBMS.
This output indicates that the postgresql
service is loaded, enabled, and currently active (running). It also shows the main process ID of the PostgreSQL server (23478 in this example), as well as the start time of the service.
To check the status of the PostgreSQL server using netstat
, you can use the following command:
TCP 0.0.0.0:5432 0.0.0.0:0 LISTENING
This indicates that the PostgreSQL server is listening for connections on the default port, and is therefore running and available. If you do not see any output, or if the output indicates that the port is not being used, this may mean that the PostgreSQL server is not running.
Overall, using netstat
to check the status of the PostgreSQL server is a simple and effective way to determine whether the server is running and listening for connections. This can be useful for troubleshooting purposes, or for monitoring the status of the server over time.
To check the status of the PostgreSQL server using lsof
, you can use the following command:
lsof -i :5432
This command will use lsof
to list all open network connections on your computer, and will search for connections that are using the default PostgreSQL port (5432). If the PostgreSQL server is running and listening for connections on this port, you should see output similar to this:
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
postgres 10001 postgres 3u IPv4 45601 0t0 TCP *:postgresql (LISTEN)
This indicates that the PostgreSQL server is listening for connections on the default port, and is therefore running and available. If you do not see any output, or if the output indicates that the port is not being used, this may mean that the PostgreSQL server is not running.
Overall, using lsof
to check the status of the PostgreSQL server is a simple and effective way to determine whether the server is running and listening for connections. This can be useful for troubleshooting purposes, or for monitoring the status of the server over time.